I have a list of push notification tokens (around 10k) for my app, and I am not tracking the states of active/deleted tokens. When a new user is added to the system, it is added with its push token
(one device can hold one push notification token, in case a token is changed -not happened in the last 6 months of my app's life cycle- it is modified with the new token)
For sending push notification to a single user I am using the following php script
<?php
// connecting to sql
sql_connect();
// get user token from sql
$token = sql_gettoken();
// Put your device token here (without spaces):
$deviceToken = $token;
// Put your private key's passphrase here:
$passphrase = 'my_passphrase';
// Put your alert message here:
if ($_GET["message"]) $message = $_GET["message"];
else $message = 'This is a default message in case no message is defined';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'production_cerfiticate.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'
);
// 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;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
?>
I believe if I keep $fp (connection to APNS Server) open I can send multiple push notifications in between (I am not sure of that?). But I have read that if a token is offline/deleted app then the connection is closed by apple's side, which means push notification cycle will fail.
Any tips for converting this single push notification script into broadcast notification ? I am discouraged to test this since the first test will be a live broadcast and I have only 1 shot at this.
EDIT: I have missed the most important part, If I just go and call this php script in a loop for all device tokens, which means it will try to call this for deleted apps and actives, will Apple block/deactivate/ban my certificate or my ability to send push notifications ?
- For others out there looking for broadcast, it seems Apple does not have any limits for push notifications.
Cheers