title: APNS: Invalid token causes all subsequent push notifications to fail
body: <p>I'm using the <a href="https://github.com/Ph3nol/NotificationPusher" rel="nofollow">Sly PHP Push Notification library</a>, but the same thing happens whether I send all my push notifications completely manually, without the library (like in the script below). If there is an invalid token in my SQL database and I attempt to send a push to that token, I am disconnected from <code>ssl://gateway.push.apple.com:2195</code> and all the subsequent tokens do not receive the push. How can I either spot invalid tokens and remove them from my database, or continue to send out pushes after I come across an invalid token? A php script I have below (though not the Library above) suffers from the same bug:</p>
  
  <pre><code>// Put your device token here (without spaces):
  $iosTokens = array(xxxx, xxxx, xxxx);
  
  // Put your private key's passphrase here:
  $passphrase = '';
  
  $message = "Message";
  $url = "URL";
  
  if (!$message || !$url)
      exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n");
  
  ////////////////////////////////////////////////////////////////////////////////
  
  $ctx = stream_context_create();
  stream_context_set_option($ctx, 'ssl', 'local_cert', '../../../PEMs/siouxFallsStampede.pem');
  stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  
  // Open a connection to the APNS server
  $fp = stream_socket_client(
    'ssl://gateway.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  
  if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
  
  echo 'Connected to APNS' . PHP_EOL;
  
  // Create the payload body
  $body['aps'] = array(
    'alert' =&gt; $message,
    'sound' =&gt; 'default',
    'link_url' =&gt; $url,
    );
  
  // Encode the payload as JSON
  $payload = json_encode($body);
  
  foreach($iosTokens as $devicetoken) {
  
      // Build the binary notification
      $msg = chr(0) . pack('n', 32) . pack('H*', $devicetoken) . pack('n', strlen($payload)) . $payload;
      // Send it to the server
      $result = fwrite($fp, $msg, strlen($msg));
  
      var_dump($result);
      if (!$result)
        echo 'Message not delivered' . PHP_EOL;
      else
        echo 'Message successfully delivered' . PHP_EOL;
  
  }
  
  // Close the connection to the server
  fclose($fp);
  </code></pre>