问题
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?
回答1:
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.
回答2:
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.
回答3:
Same problem.. It looks like a limit of 2000 devices is the maximum. So, 2000 (or less) tokens by socket opened. Try and see !
来源:https://stackoverflow.com/questions/6819315/max-number-of-devices-to-send-to-apns-socket-sever