Max number of devices to send to APNS socket sever

2019-04-10 10:57发布

Our push notification script that has worked for almost a year has suddenly stopped working. The script does the following:

  1. Queries a DB for a list of iPhone device tokens

  2. 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);
    
  3. creates a payload with a 255 byte sized message

    $payload = '{
      "aps": {
         "alert": "' . $message . '",
         "badge": 1,
         "sound": "default"
      }
    }';
    
  4. 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);
    
  5. 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?

3条回答
萌系小妹纸
2楼-- · 2019-04-10 11:06

Same problem.. It looks like a limit of 2000 devices is the maximum. So, 2000 (or less) tokens by socket opened. Try and see !

查看更多
趁早两清
3楼-- · 2019-04-10 11:09

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.

查看更多
闹够了就滚
4楼-- · 2019-04-10 11:31

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.

查看更多
登录 后发表回答