title: Send push notification to a group
body: <p>I am not sure how to send a push notification to multiple tokens. With my script it seems to only send a push notification to one... as per Apple it would be better to have one open connection and keep it open and send messages when needed. Not sure how to do it on my php server...</p>
  
  <pre><code>$payload['aps'] = array('alert' =&gt; "New Cave report for ".$caveName,'badge' =&gt; 1, 'sound' =&gt; 'default');
  $payload['condition'] = array('conditionID' =&gt; $ccID, 'caveName' =&gt; $caveName);
  $payload = json_encode($payload);
  
  // Connection Part
  $apnsHost = 'gateway.sandbox.push.apple.com';
  $apnsPort = 2195;
  $apnsCert = '/var/www/web543/files/apns-dev.pem';
  
  if (!file_exists($apnsCert) )
  {
      echo "Certification file not found!";
  } else
  {
      $streamContext = stream_context_create();
      stream_context_set_option($streamContext,'ssl','local_cert',$apnsCert);
  
      $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort,$error,$errorString,2,STREAM_CLIENT_CONNECT,$streamContext);
  
      if ( !$apns )
      {
          echo "Connection Failed!".$errorString;
      } else
      {
          $db-&gt;query("SELECT DISTINCT token FROM notifications WHERE userID != '{$userID}'");
          if ($db-&gt;num_rows()&gt;0) 
          {
              while ($db-&gt;next_record())
              {
                  $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $db-&gt;f('token'))) . chr(0) . chr(strlen($payload)) . $payload;
                  if ( fwrite($apns, $apnsMessage) === FALSE )
                  {
                      echo "Can not write";
                  }
              }
          }
      }
  
      fclose($apns);
  }
  </code></pre>
  
  <p>some output of $apnsMessage</p>
  
  <pre><code>Token: 5a984922e19eab54f78fd54e24d5b02a3d30ccdbbeee34aadbdacaa687ee1261 Message: Z˜I"áž«T÷ÕN$Õ°*=0ÌÛ¾î4ªÛÚÊ¦‡îa†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
  Token: c607acc70bd4885bf56f3b4827523023bf93a1d644626768ab0304bb3b4414dc Message: Æ¬ÇÔˆ[õo;H'R0#¿“¡ÖDbgh«»;DÜ†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
  Token: 785ec3128972bd3d4c3e6fa1eeead97b73b0696e2361339a2467e6ba775b83ea Message: x^Ã‰r½=L&gt;o¡îêÙ{s°in#a3š$gæºw[ƒê†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
  Token: c592487e3c71e921d0b7a825b66ed5e58070fee709131535ac391f14febbcfdc Message: Å’H~Token: 061bc20ba3a0fc17c689e052b42b5789f502a52d43180ea114e3212077045315 Message: Â£ üÆ‰àR´+W‰õ¥-C¡ã! wS†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
  Token: 26fb66fef67a122ca456f106363115285d4d7156e7c8ab6e51bd5bfa9bab2d03 Message: &amp;ûfþöz,¤Vñ61(]MqVçÈ«nQ½[ú›«-†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
  </code></pre>