Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max number of devices to send to APNS socket sever

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?

like image 522
Miriam P. Raphael Avatar asked Feb 24 '23 11:02

Miriam P. Raphael


1 Answers

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.

like image 114
Miriam P. Raphael Avatar answered Apr 07 '23 13:04

Miriam P. Raphael