发送多个 iPhone 通知
当我需要发送一个通知时,我的代码工作正常,但每次当我需要发送多个通知时,它只发送第一个通知。这是代码:
<?php
$device_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'none');
$payload = json_encode($payload);
for($i=0; $i<5; $i++)
{
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
}?>
我做错了什么?
提前谢谢, 姆拉乔
My code works ok when I need to send one notification, but each time when I need to send more than one, it only sends the first one. Here is the code:
<?php
$device_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'none');
$payload = json_encode($payload);
for($i=0; $i<5; $i++)
{
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
}?>
What am I doing wrong?
Thx in advance,
Mladjo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该只打开一次与 apns 的连接。现在你正在循环中打开它,这是错误的。我还使用稍微不同的方案来构建我的消息。您应该这样做:
另请注意,苹果建议使用相同的连接来发送所有推送通知,因此您不应在每次有推送通知要发送时都进行连接。
You should open the connection to apns only once. Right now you are opening it in the loop which is wrong. I'm also using a slightly different scheme to build my messages. You should instead do it in this way:
Also note that apple recommends using the same connection to send all your push notifications so you shouldn't connect every time you have a push notification to send.
看看下面的文档:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3
它表示应使用 TCP/IP Nagle 算法在一次传输中发送多个通知。您可以在这里了解 Nagle 算法是什么:
http://en.wikipedia.org/wiki/Nagle%27s_algorithm
所以我相信创建消息的代码应如下所示:
现在打开 TCP 连接并发送消息......
Have a look at the following document:
http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3
It says that multiple notifications should be sent in a single transmission using the TCP/IP Nagle algorithm. You can find out what the Nagle algorithm is here:
http://en.wikipedia.org/wiki/Nagle%27s_algorithm
so I believe the code to create the message should look like:
And now open the TCP connection and send the Message....
在这里黑暗中拍摄。看看你的 for 循环。
看起来您打开了连接并推送了消息...但是该连接会自行关闭吗?您是否需要为每次推送启动一个新连接,从而需要在 while 循环结束时关闭第一个连接,然后才能重新启动另一个连接?
Taking a shot in the dark here. Looking at your for loop.
It looks like you open the connection and push the message... but does that connection close itself? Do you need to initiate a new connection for each push thereby making it necessary to close the first connection at the end of the while loop before re-initiating another?