Search code examples
push-notificationapple-push-notificationsapns-php

iOS Push Notifications error after sending wrong token


I've been implementing a small PHP script which will send Push Notifications to Apple's APN servers. I've got a database with all device tokens that registered for push notifications within my App. My script will query the database for the tokens and send them to Apple. Everything works great with a big but... I manually inserted a wrong token into my table and executed the script again.

I've realized that after that wrong token is sent to Apple, all notifications after that won't reach the devices. Apple seems not to return any OK if everything worked fine but it does send a KO code if something went wrong. I get an error from Apple for that wrong token but I don't get any response afterwards for all the other notifications.

I open only one connection for all the tokens inside the database.

I prepare a socket context with this line:

$streamContext = stream_context_create(array(
   'ssl' => array(
    'local_cert' => $this->signFile
   )
  ));

Then start a communication channel with the APN servers:

$this->hFile = stream_socket_client(self::$serverUrl[$environment], $err, $errstr, 60, STREAM_CLIENT_CONNECT, $streamContext);

Finally I send the message:

fwrite($this->hFile, $command);

I thought of setting a new connection for every notification but I wanted to ask SO opinion first...

By the way, I know that PHP isn't the best choice for this but it came as a requirement from somewhere else and we are forced to set the system this way.

Thank you and have a nice day,

Alex.


Solution

  • I finally found a solution: As soon as I detect that a token is invalid, I close the connection and open it again to start sending tokens right after the wrong one.