Notification Push Push Push via PHP

I have a list of push tokens tokens (about 10k) for my application, and I do not track the status of active / remote tokens. When a new user is added to the system, he is added with his token

(on one device, one release notification token can be stored, in case of a change in the token - there wasn’t any application life cycle for the last 6 months), it is modified with a new token)

To send push notifications for one user, I use 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 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 that if I support $ fp (connecting to the APNS server), I can send several push notifications between them (am I not sure about this?). But I read that if the token is in a stand-alone / remote application, the connection is closed by the side on the apple side, which means that the call notification cycle will not succeed.

Any tips on converting this unidirectional notification script to a broadcast notification? I am not advised to check this, since the first test will be live broadcast, and I only have 1 shot for that.

EDIT: I missed the most important part . If I simply switch to this php script in a loop for all device tokens, which means that he will try to call it for remote applications and assets, will Apple block / deactivate / deny my certificate or my ability to send push notifications?

  • For others looking for broadcast, it seems like Apple has no restrictions on push notifications.

Greetings

+4
source share
1 answer

You must keep the connection open and send notifications using the same connection while it remains open. This is what Apple says you should do.

If the application is deleted from the device, the connection will not be closed when sending to this device. It will be closed only if the token was not valid for the current environment (which usually happens when trying to send a sandbox device token to the production environment or vice versa), or if the message has other errors (for example, the payload is too long).

When you send a notification to the device that deleted the application, APNS finds out that the device has deleted the application, and later, when you call the feedback service, you get this device token. Apple asks you to call the feedback service periodically and remove the devices that were returned to them from your database. Apple may block you if you do not use the feedback service and continue to send notifications to devices that have uninstalled the application several times.

I suggest you read Troubleshooting Push Notifications , especially the "Messaging and Push Error Checking" section. It is very useful.

+3
source

All Articles