I'm using the Sly PHP Push Notification library, but the same thing happens whether I send all my push notifications completely manually, without the library (like in the script below). If there is an invalid token in my SQL database and I attempt to send a push to that token, I am disconnected from ssl://gateway.push.apple.com:2195
and all the subsequent tokens do not receive the push. How can I either spot invalid tokens and remove them from my database, or continue to send out pushes after I come across an invalid token? A php script I have below (though not the Library above) suffers from the same bug:
// Put your device token here (without spaces):
$iosTokens = array(xxxx, xxxx, xxxx);
// Put your private key's passphrase here:
$passphrase = '';
$message = "Message";
$url = "URL";
if (!$message || !$url)
exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n");
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '../../../PEMs/siouxFallsStampede.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.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',
'link_url' => $url,
);
// Encode the payload as JSON
$payload = json_encode($body);
foreach($iosTokens as $devicetoken) {
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $devicetoken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
var_dump($result);
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
}
// Close the connection to the server
fclose($fp);
This was solved here. What I did was ordered the tokens by ID, when a token was invalid, I removed it from my SQL database and continued sending a push to just those tokens with an ID higher than the invalid one (so ordering by ID is crucial).