push notification question

Is about php script

how can I handle with many device tokens?

I have tried to use an array but didn’t work :S

someone knows a way to handle with many device tokens?
Thanks [import]uid: 23063 topic_id: 19561 reply_id: 319561[/import]

Hi, this is some php that works on my local server sending to all the device tokens I have scraped from devices. Lets assume you have a mysql database called ‘tokens’ full of all your tokens.

They are stored in a table in that database, again called ‘tokens’. The field the tokens are stored in is called ‘token’. So you have tokens(database) => tokens(table) => token(field).

Here is the php to select all the tokens and send each one a message you defined earlier on, under the variable “$message” which you got using GET.

[php]

<?php
$database = mysql\_connect('localhost', 'youruser', 'yourpassword'); $selector = mysql\_select\_db('tokens',$database); $sql = "SELECT token FROM tokens"; $arr = mysql\_query($sql); // Put your private key's passphrase here: $passphrase = 'passphrase'; // Message if(isset($\_GET)) { $message = $\_GET["message"]; } //////////////////////////////////////////////////////////////////////////////// $ctx = stream\_context\_create(); stream\_context\_set\_option($ctx, 'ssl', 'local\_cert', 'ck.pem'); stream\_context\_set\_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server $fp = stream\_socket\_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM\_CLIENT\_CONNECT|STREAM\_CLIENT\_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP\_EOL); echo 'Connected to APNS' . PHP\_EOL; // Create the payload body $body['aps'] = array( 'alert' =\> $message, 'sound' =\> 'default' ); // Encode the payload as JSON $payload = json\_encode($body); while($row = mysql\_fetch\_array($arr, MYSQL\_ASSOC)) { $msg = chr(0) . pack('n', 32) . pack('H\*', $row['token']) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $msg, strlen($msg)); } if (!$result) echo 'Message not delivered' . PHP\_EOL; else echo 'Message successfully delivered' . PHP\_EOL; fclose($fp); ?\> [/php] I hope this helps [import]uid: 24641 topic\_id: 19561 reply\_id: 75545[/import]

thank you so much i will try your code

Thanks [import]uid: 23063 topic_id: 19561 reply_id: 75621[/import]

another question… how can I fire this php? To work

in terminal we can do this by
“php sendpush.php”

but in webserver, how can we “fire” this php?
Thanks [import]uid: 23063 topic_id: 19561 reply_id: 75641[/import]

if you have xammp or similiar running, just get to it via your browser, i.e

localhost/test.php
[import]uid: 24641 topic_id: 19561 reply_id: 75642[/import]

If you have on a server somewhere, just hit it with your browser:

http://yourwebsite.com/sendpush.php

[import]uid: 19626 topic_id: 19561 reply_id: 75662[/import]

http://universopositivo.com.br/iphone/sendpush.php

didn’t work :S

I put my ck.pem into the same folder where sendpush.php are

but it don’t work :frowning: [import]uid: 23063 topic_id: 19561 reply_id: 75668[/import]

more often that not localhost won’t work on a remote server, look up on your Cpanel the mysql details [import]uid: 24641 topic_id: 19561 reply_id: 75684[/import]

but I’m not using your script yet

[php]

<?php
$deviceIDs = array ( 'c450a0055a5742b00149af64759100d5bc98ac9181e43ca1718b6fcfe6cde902', '717b1389ce4558213a932ebf08746a1ef4d07517261cf9d5ce1147876fb38279' ); // Put your device token here (without spaces): $deviceToken = 'c450a0055a5742b00149af64759100d5bc98ac9181e43ca1718b6fcfe6cde902'; $deviceToken2 = 'c450a0055a5742b00149af64759100d5bc98ac9181e43ca1718b6fcfe6cde902'; // Put your private key's passphrase here: $passphrase = 'larila'; // Put your alert message here: $message = "testeee"; //////////////////////////////////////////////////////////////////////////////// $ctx = stream\_context\_create(); stream\_context\_set\_option($ctx, 'ssl', 'local\_cert', 'ck.pem'); stream\_context\_set\_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server $fp = stream\_socket\_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM\_CLIENT\_CONNECT|STREAM\_CLIENT\_PERSISTENT, $ctx); if (!$fp) exit("Failed to connect: $err $errstr" . PHP\_EOL); echo 'Connected to APNS' . PHP\_EOL; // Create the payload body $body['aps'] = array( 'alert' =\> $message, 'sound' =\> 'default', 'badge' =\> '1' ); // Encode the payload as JSON $payload = json\_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H\*', $deviceToken ) . pack('n', strlen($payload)) . $payload; $msg2 = chr(0) . pack('n', 32) . pack('H\*', $deviceToken2 ) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); $result2 = fwrite($fp, $msg2, strlen($msg2)); if (!$result) echo 'Message not delivered' . PHP\_EOL; else echo 'Message successfully delivered' . PHP\_EOL; if (!$result2) echo 'Message not delivered' . PHP\_EOL; else echo 'Message successfully delivered' . PHP\_EOL; // Close the connection to the server fclose($fp); ?\> [/php] I'm using the script from ansca to test, after that I will use your code [import]uid: 23063 topic\_id: 19561 reply\_id: 75699[/import]

anyone? [import]uid: 23063 topic_id: 19561 reply_id: 77300[/import]

Put some “echo” statements in your PHP script to print what its doing.

Look into using EasyAPNS
[import]uid: 19626 topic_id: 19561 reply_id: 77311[/import]