如何减少在php中发送批量APNS的时间

时间:2016-02-13 08:21:06

标签: php apple-push-notifications

我使用以下代码发送APNS。它可以在一台设备上正常工作,当设备更多(例如1000)时,它需要花费太多时间。它如何优化。请帮帮我。

我的代码

function apnsNotification($deviceIds, $msg )
{
$message = json_encode($msg);
$passphrase = '1234';
$payload = json_encode([
'aps' => [
'alert' => $message,
'module'=> $message,
'sound' => 'default',
'badge' => 1
]
]);
$result = 'Start' . '<br />';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'sampleapp_apns_12.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
echo count($deviceIds) . ' devices will receive notifications.<br />';
foreach ($deviceIds as $item)
{
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
exit("Failed to connect: $err $errstr" . '<br />');
} else {
echo 'Apple service is online.' . '<br />';
}
$data = chr(0) . pack('n', 32) . pack('H*', $item) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $data, strlen($data));
if (!$result)
{
echo 'Undelivered message count: ' . $item . '<br />';
}
else 
{
echo 'Delivered message count: ' . $item . '<br />';
}
if ($fp) 
{
fclose($fp);
echo 'The connection has been closed by the client' . '<br />';
}
}
echo count($deviceIds) . ' devices have received notifications.<br />';
}

0 个答案:

没有答案