Sending multiple iPhone notifications
My code works ok when I need to send one notification, but each time when I need to send more than one, it only sends the first one. Here is the code:
<?php
$device_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'non开发者_StackOverflow中文版e');
$payload = json_encode($payload);
for($i=0; $i<5; $i++)
{
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
}?>
What am I doing wrong?
Thx in advance, Mladjo
You should open the connection to apns only once. Right now you are opening it in the loop which is wrong. I'm also using a slightly different scheme to build my messages. You should instead do it in this way:
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
for($i=0; $i<5; $i++)
{
$apns_message = chr(0).pack('n', 32).pack('H*', $device_token).pack('n', strlen($payload)).$payload;
fwrite($apns, $apnsMessage);
}?>
Also note that apple recommends using the same connection to send all your push notifications so you shouldn't connect every time you have a push notification to send.
Have a look at the following document: http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3
It says that multiple notifications should be sent in a single transmission using the TCP/IP Nagle algorithm. You can find out what the Nagle algorithm is here: http://en.wikipedia.org/wiki/Nagle%27s_algorithm
so I believe the code to create the message should look like:
// Create the payload body
$body['aps'] = array(
'alert' => "My App Message",
'badge' => 1);
// Encode the payload as JSON
$payload = json_encode($body);
// Loop through the token file and create the message
$msg = "";
$token_file = fopen("mytokens.txt","r");
if ($token_file) {
while ($line = fgets($token_file)) {
if (preg_match("/,/",$line)) {
list ($deviceToken,$active) = explode (",",$line);
if (strlen($deviceToken) == 64 && intval($active) == 1) {
// Build the binary notification
$msg .= chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
}
}
}
fclose ($token_file);
}
if ($msg == "") {
echo "No phone registered for push notification";
exit;
}
And now open the TCP connection and send the Message....
Taking a shot in the dark here. Looking at your for loop.
It looks like you open the connection and push the message... but does that connection close itself? Do you need to initiate a new connection for each push thereby making it necessary to close the first connection at the end of the while loop before re-initiating another?
精彩评论