【发布时间】:2016-01-21 09:14:00
【问题描述】:
我在后端使用 php 来发送苹果推送通知。 有时我会收到通知,有时我不会。
如果有任何其他方式发送通知,我知道这是一个简单的代码。无法理解为什么它不总是提供,有时却提供。
ps:这是我用于生产构建的
function sendPush($user_id,$message,$video_id,$not_type,$deviceToken)
{
$passphrase = 'xxxxx';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
$body['aps'] = array(
'alert' => $message,
'badge' => '1',
'sound' => 'default');
$body['payload'] = array(
'type' => $not_type,
'user_id' => $user_id,
'video_id' => $video_id
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
fclose($fp);
}
【问题讨论】:
-
我相信这个问题对于 iOS 开发者来说并不新鲜。请参考这个answer
标签: php ios web-services apple-push-notifications