问题
I am trying to send push notification on IOS using PHP there are 300,000 device tokens
I have written a small routine, on each execution the routine send notification to 100 devices, and after 1 second delay routine is executed again and notification is sent to next 100 devices, routine 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);
Some time this routine works perfectly fine, and i have seen sent notifications on devices.
However some times it gives following error
Warning: fwrite(): SSL: An established connection was aborted by the software in your host machine.
My Questions are:
- Is it a correct way to send bulk notification?
- I open a connection once and write data(notification) for 100 devices, and then close the connection, or is it better to open connection for each device token and then close?
- What is causing above error?
Findings
Sent approx 90,000
push notifications (100 in single connection open and next 100 after 1 second delay)
Approx 60,000
failed (fwrite
failed) with above error and approx 30,000
push were successfully sent (fwrite returned positive integers indicating number of bytes written).
来源:https://stackoverflow.com/questions/35210258/correct-way-of-sending-ios-bulk-push-notifications-in-php