Correct way of sending IOS Bulk push notifications in PHP

泄露秘密 提交于 2019-12-11 16:08:26

问题


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:

  1. Is it a correct way to send bulk notification?
  2. 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?
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!