发送到 APNS 套接字服务器的最大设备数
我们工作了近一年的推送通知脚本突然停止工作了。该脚本执行以下操作:
在数据库中查询 iPhone 设备令牌列表
打开与 Apple 实时 APNS 服务器的 SSL 套接字连接
$ctx = stream_context_create(); Stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert); Stream_context_set_option($ctx, 'ssl', 'passphrase', $pass); $fp =stream_socket_client($apnsHost, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
创建包含 255 字节大小的消息的有效负载
$payload = '{ “aps”:{ "alert": "'.$message.'", “徽章”:1, “声音”:“默认” } }';
循环每个设备并将有效负载写入打开的连接。
$msg = chr(0) 。包(“n”,32)。 pack('H*', str_replace(' ', '', $deviceToken)) 。 pack("n",strlen($payload)) 。 $有效负载; fwrite($fp, $msg);
然后关闭连接。
fclose($fp);
所以我的问题是——脚本中没有任何改变,但改变的是数据库的大小。我创建了一个 Web 界面,允许用户向所有 iPhone 设备发送有效负载,并且当它运行时,发送/加载只需要几秒钟。数据库中的设备数量(大约 3500 个)是否有可能造成问题?
当我写入套接字时,最多可以向多少个设备发送推送通知?是否存在最大值或限制?
Our push notification script that has worked for almost a year has suddenly stopped working. The script does the following:
Queries a DB for a list of iPhone device tokens
Opens an SSL socket connection to Apple's live APNS server
$ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $apnsCert); stream_context_set_option($ctx, 'ssl', 'passphrase', $pass); $fp = stream_socket_client($apnsHost, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
creates a payload with a 255 byte sized message
$payload = '{ "aps": { "alert": "' . $message . '", "badge": 1, "sound": "default" } }';
Loops through each device and writes the payload to the open connection.
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload; fwrite($fp, $msg);
The connection is then closed.
fclose($fp);
So my question is this-- nothing in the script has changed, but what HAS changed is the size of the database. I created a web interface that allows a user to send a payload to all iphone devices and when it runs it only takes a few seconds to send/load. Is it possible though that the number of devices in the DB (around 3500) is creating the problem?
What is the maximum number of devices that I can I send a push notification to when I write to the socket? Does a max or limit exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题不在于发送到 APNS 的设备数量。问题是苹果改变了他们的API。您现在需要检查每个设备以查看其是否仍然有效(即它们是否拒绝推送通知、设备是否删除了应用程序等)。如果设备不再接受来自您的应用程序的推送通知,而您仍然向其发送通知,Apple 会立即断开与您的 APNS 套接字的连接。我现在有一个 cronjob,每天运行一个程序,检查并删除我的数据库中不再接受推送通知的所有设备(Apple 有此列表)。但要小心 - 一旦您从 Apple 提取禁用设备 ID 列表,Apple 就会将其从其服务器中删除,并且您永远无法再次提取它。
您还需要更新推送通知代码以检查连接是否曾经断开。当连接断开时,程序需要重新建立连接并再次尝试推送。
The problem wasn't the number of devices to sent to APNS. The problem turned out to be that Apple changed their API. You now need to check every single device to see if it's still valid (ie. if they are denying push notificaitons, if the device deleted the app, etc.). If the device no longer accepts push notifications from your app and you send one to it anyways, Apple immediately drops the connection to your APNS socket. I now have a cronjob that runs a program once a day that checks and deletes any devices from my database that no longer accept push notifications (Apple has this list). But be careful -- once you pull the list of disabled device ids from Apple, Apple deletes it from their server and you can never pull it again.
You also need to update your push notification code to check if the connection is ever dropped. When the connection is dropped, the program needs to reestablish the connection and try to push again.
事实上,根据经验,在通过 APNS 推送一定数量的通知后,与 APNS 的连接似乎会失败。在我们自己的APNS库(http://code.google.com/p/javapns/)中,库中包含的多线程传输引擎在推送200个通知后自动重新启动连接(似乎是经过反复试验的神奇数字)。自从我们引入该功能(以及其他一些次要的通信链接恢复选项)以来,大量通知的失败通知率降为零。
Indeed, based on experience, it seems that connections to APNS will fail after some number of notifications have been pushed through it. In our own APNS library (http://code.google.com/p/javapns/), the multithreaded transmission engine included in the library automatically restarts connections after pushing 200 notifications (seems to be a magic number after trial and error). Since we introduced that feature (along with some other minor comlink recovery options), the failed notification rate for large amounts of notifications went to zero.
同样的问题..
看起来 2000 台设备的限制是最大的。
因此,套接字打开了 2000 个(或更少)令牌。尝试看看!
Same problem..
It looks like a limit of 2000 devices is the maximum.
So, 2000 (or less) tokens by socket opened. Try and see !