Search code examples
ios6push-notificationpushapple-push-notificationspassbook

Send push to many devices for pass


I am doing pushnotification for pass in passbook. Push works, but nothing shows for pushnotification and if update statically by toggling the back of pass, I can get the updated pass. This is the code I use :

<?php


    // Provide the Certificate and Key Data.
    $cert = '../certificates/Certificates.pem';

    $payload = json_encode (array("aps" => ""));
    error_log('payload :'.$payload,0);


    // Create the and config the socket context. 
    $streamContext = stream_context_create ();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $cert);
    $password = ''; 

    if (strlen($password))
        stream_context_set_option($tContext, 'ssl', 'passphrase', $password);


    // Open the Connection to the APNS Server.
    $ConnectAPNS = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $streamContext);

$query1 = mysql_query("select deviceID from registration");
$row1 = mysql_fetch_array($query1);
$deviceID = $row1['deviceID'];

if(!empty($deviceID)){
$query2 = mysql_query("select pushToken from device ");

while($row2 = mysql_fetch_array($query2)){

    $pushToken= $row2['pushToken']; 

    // Compose and pack the push message
    $apns_message = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $pushToken)) . chr(0) . chr(mb_strlen($payload)) . $payload;

    $success = fwrite($ConnectAPNS, $apns_message);

    // Check if we were able to open a socket.
    if ($success)
        error_log(date('d-m-Y hh:ii:ss', time()) . ': APNS Message successfully sent to push token ' . $pushToken, 0);
    else
        error_log(date('d-m-Y hh:ii:ss', time()) . ': Push error:' . $err . ': ' . $errstr);

    error_log('Stream Response: ' . print_r($success, true), 0); 
}
}
    // Close the Connection to the Server.
@socket_close($ConnectAPNS);
fclose ($ConnectAPNS);
include("feedback.php");
?>
`


By using this code, may be 30 mns that I can get pushnotification for updated pass ! This is error I get: `May 7 13:33:21 CamMobs-iPod4 passd[21865] : Too many pushes too fast for pass.cam-mob.passbookpasstest -- severe rate limiting will apply.


How to push without this error ?


Solution

  • Your code is opening a new connection every time you iterate through your while loop.

    Try opening the connection before the loop, write the requests to the socket within the loop, then close the socket at the end of the script. That will stop you rapidly battering the gateway with several requests in the space of a second.