title: Apple push server not working, caused by single token
body: <p>I created a push notification server for my apps. This server works fine on test and production. I created an API which can recieve a serialized array of device tokens.
  This API is created in PHP, and the call to the API is PHP as well.
  This is the array:</p>
  
  <pre><code>  'devices' =&gt; serialize(array(
          //'aaaaaa',
          'bbbbb',
          'ccccc'
     )),
  </code></pre>
  
  <p>The real tokens are replaced by placeholders for security reasons, ofcourse.
  I got one token that screws up the push notifications for the other tokens. When I disable this token in the array (aaaaa in this case), the other 2 devices receive the token. When I enable the aaaa token, no push notification is delivered at all.</p>
  
  <p>The API side to send the actual push:</p>
  
  <pre><code> foreach($devices as $device) {
        $msg = chr(0) . pack('n', 32) . pack('H*', $device) . pack('n', strlen($payload)) . $payload;
        $result = fwrite($fp, $msg, strlen($msg));
   }
  </code></pre>
  
  <p>Ofcourse there is some variable explaination missing in the code above, but this works fine in the case without token aaa, so that shouldnt be the problem.</p>
  
  <p>Anyone knows why one faulty token can screw up pushes for valid tokens?</p>
  
  <hr>
  
  <p>EDIT:
  I now use the enhanced method for APNS, using this code:</p>
  
  <pre><code>$error = false;
  foreach($devices as $deviceToken) {
      $msg = pack("C", 1) . pack("N", $deviceToken) . pack("N",  time() + 86400) . pack("n", 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n", strlen($payload)) . $payload;
      fwrite($fp, $msg);
      $error = $this-&gt;checkAppleErrorResponse($fp);
  
      debug($error);
  }
  </code></pre>
  
  <p>And the checkAppleErrorResponse function:</p>
  
  <pre><code>function checkAppleErrorResponse($fp) {
  
      $apple_error_response = fread($fp, 6);
  
      if ($apple_error_response) {
  
          $error_response = unpack('Ccommand/Cstatus_code/Nidentifier', $apple_error_response);
  
          if ($error_response['status_code'] == '0') {
              $error_response['status_code'] = '0-No errors encountered';
          } else if ($error_response['status_code'] == '1') {
              $error_response['status_code'] = '1-Processing error';
          } else if ($error_response['status_code'] == '2') {
              $error_response['status_code'] = '2-Missing device token';
          } else if ($error_response['status_code'] == '3') {
              $error_response['status_code'] = '3-Missing topic';
          } else if ($error_response['status_code'] == '4') {
              $error_response['status_code'] = '4-Missing payload';
          } else if ($error_response['status_code'] == '5') {
              $error_response['status_code'] = '5-Invalid token size';
          } else if ($error_response['status_code'] == '6') {
              $error_response['status_code'] = '6-Invalid topic size';
          } else if ($error_response['status_code'] == '7') {
              $error_response['status_code'] = '7-Invalid payload size';
          } else if ($error_response['status_code'] == '8') {
              $error_response['status_code'] = '8-Invalid token';
          } else if ($error_response['status_code'] == '255') {
              $error_response['status_code'] = '255-None (unknown)';
          } else {
              $error_response['status_code'] = $error_response['status_code'].'-Not listed';
          }
  
          echo 'Response Command:&lt;b&gt;' . $error_response['command'] . '&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Identifier:&lt;b&gt;' . $error_response['identifier'] . '&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Status:&lt;b&gt;' . $error_response['status_code'] . '&lt;/b&gt;&lt;br&gt;';
  
          echo 'Identifier is the rowID (index) in the database that caused the problem, and Apple will disconnect you from server. To continue sending Push Notifications, just start at the next rowID after this Identifier.&lt;br&gt;';
  
          return true;
      }
  
      return false;
  }
  </code></pre>
  
  <p>Sending only the valid token, $numchanged is 0. Sending the invalid token and then the valid token, $numchanged is 1 for both cases.</p>