4
votes

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

1
+1 For your edit I can suggest you to look at Feedback Service, you can look around the web for sample code in PHP, if you get valid feedback i.e. user has not uninstalled application you can send push notification. - Janak Nirmal
@Jennis thanks for feedback service, I will look into it. Using it I can check and send push notification if available, and I can use this script, but that means 1 connection to APNS, 1 check, 1 push (if available) and 1 connection close times all tokens. It will happen in minutes and that is like 40k actions to APNS servers. Will it be considered as spam? - Bartu
Shouldn't be a case and there are no documented limit available or I can search as of now, Check This, keep connection open till all the push notifications sent. - Janak Nirmal
OK so there is no limit for daily pushes, I will implement that script you suggested and let you know if it works properly - Bartu

1 Answers

3
votes

You should keep the connection open and send notifications using the same connection as long as it stays open. That's what Apple says you should do.

If the app is uninstalled from a device, the connection won't be closed when sending to that device. It will be closed only if the token was never valid for the current environment (which usually happens when you try to send a sandbox device token to the production environment or vice versa), or if the message has other errors (such as too long payload).

When you send a notification to a device that uninstalled the app, APNS figures out that the device uninstalled the app, and later, when you call the feedback service, you get that device token. Apple asks that you call the feedback service periodically and remove the devices that are returned by it from your DB. Apple might block you if you don't use the feedback service and keep sending notifications to devices that uninstalled the app multiple times.

I suggest you read about Troubleshooting Push Notifications, especially the Push Notification Throughput and Error Checking section. It's very helpful.