title: Batch / multiple iOS Push Notification code - works for 2 devices, but not for 100
body: <p>The following code works fine if the number of devices I send to is 2 - i.e., they both receive the push notifications. But if I raise that limit to 100, no push notifications are received.</p>
  
  <p>I have read up on this and it looks like I am sending the batch notifications correctly (i.e., multiple requests via a single connection); the timeout of the connection is set nice and high (60 seconds); the output of the code all looks in order; nothing in the apache error log, so I don't see where the problem is.</p>
  
  <p>My customer's getting really hacked off. Can anyone help??</p>
  
  <p>function sendIosPushes() {</p>
  
  <pre><code>$payload['aps'] = array('alert' =&gt; pushMessage, 'badge' =&gt; badgeNumber, 'sound' =&gt; 'default');
  $payload = json_encode($payload);
  
  //$statement = "SELECT * FROM push_ios WHERE device_token = 'device token of my iphone" OR device_token = 'device token of my ipod'; //works selecting my two devices from table
  $statement = "SELECT * FROM push_ios"; //doesn't work when selecting all devices from table
  
  
  $streamContext = stream_context_create();
  stream_context_set_option($streamContext, 'ssl', 'local_cert', apnsCert);
  
  $connectTimeout = 60;
  $apns = stream_socket_client('ssl://' . apnsHost . ':' . apnsPort, $error, $errorString, $connectTimeout, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext);
  
  $numDevices = 0;
  $numRequestsSent = 0;
  $result = mysql_query($statement);
  while ($row = mysql_fetch_assoc($result)) {
      $numDevices++;
      try {
          $deviceToken = $row['device_token'];
  
          //$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', $deviceToken) . chr(0) . chr(strlen($payload)) . $payload;
          $apnsMessage = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; //from http://stackoverflow.com/questions/1642411/sending-multiple-iphone-notifications
  
          $fwriteRes = fwrite($apns, $apnsMessage, strlen($apnsMessage));
          echo "Push sent to " . $deviceToken . "&lt;br /&gt;\n";
  
          $numRequestsSent++;
      }
      catch(Exception $e) {
          echo "1. Exception: " . $e-&gt;getMessage() . "&lt;br /&gt;\n";
      }
  }
  
  fclose($apns);
  
  
  if ($error != 0 || (isset($errorString) &amp;&amp; strlen($errorString) &gt; 0 )) {
      echo "ERROR: " . $error . " - ". $errorString . "&lt;br /&gt;\n";
  }
  
  return $numDevices . " iOS devices. " . $numRequestsSent . " requests sent.";
  </code></pre>
  
  <p>}</p>