I'm trying to send a push notification to iOS using PHP, there are 300,000 device tokens
I wrote a small subroutine, each time the procedure sends a notification to 100 devices, and after the 1-second delay procedure is performed again and the notification is sent to the next 100 devices, the procedure is as follows
$body['aps'] = array('alert' => 'Energy is full', 'sound' => 'default', 'badge' => '0' ); $url = 'ssl://gateway.push.apple.com:2195'; $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', 'ProductionCertificate.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', 'my pass phrase'); stream_context_set_option($ctx, 'ssl', 'cafile', 'trusted_ca.cer'); $fp = stream_socket_client($url, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) { return -1;} foreach($OneHundredTokens as $deviceToken) { $payload = json_encode($body, 256); $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; $result = fwrite($fp, $msg, strlen($msg)); } fclose($fp);
For a while, this procedure worked fine, and I saw sent notifications on devices.
However, several times it gives the following error
Warning: fwrite (): SSL: the established connection was interrupted by the software on your host machine.
My questions:
- Are you sending mass notification correctly?
- I open the connection once and write the data (notification) to 100 devices, and then close the connection, or is it better to open the connection for each device token and then close it?
- What causes the error above?
Conclusions Sent when approaching 90,000 push notifications (100 in open connection are open and the next 100 after 1 second delay) Approximately 60,000 failed ( fwrite failed) with the error exceeded and 30,000 were sent successfully by pressing the button (fwrite returned positive integers indicating the number recorded bytes).