title: how to properly send push notifications
body: <p>This is not my first app sending push notifications but it is my first app where I send a notification to all my users at the same time.</p>
  
  <p>What Im experiencing is that not all my users receive the notification, only some of them, even though they have the settings correct(ie notifications for my app enabled), also the code is correct since it is sent to some of them, so my guess is that the code "misses" some executions, since the conection with the APNS is asyncronous so somehow( you tell me if im wrong) that it messes the queue of sending notifications.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>function sendNotification(){
  
  $sql = "SELECT * FROM users WHERE phone = 'iPhone' AND pushID != ''";
  try {
      $db = getConnection();
      $stmt = $db-&gt;prepare($sql);  
      $stmt-&gt;execute();
      $users = $stmt-&gt;fetchAll(PDO::FETCH_OBJ);
  
      $request = Slim::getInstance()-&gt;request();
      $content = json_decode($request-&gt;getBody());
      $message = $content-&gt;message;
  
      foreach($users as $user){
  
              // Put your device token here (without spaces):
              $deviceToken = $user-&gt;pushID;
  
  
              // Put your private key's passphrase here:
              $passphrase = 'xxxxxxx';
  
              ////////////////////////////////////////////////////////////////////////////////
  
              $ctx = stream_context_create();
              stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxxxx.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'
                  );
  
              // Encode the payload as JSON
              $payload = json_encode($body);
  
              // 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));
  
              if (!$result)
                  echo 'Message not delivered' . PHP_EOL;
              else
                  echo 'Message successfully delivered' . PHP_EOL;
  
              // Close the connection to the server
              fclose($fp);
  
      }
      $db = null;
  } catch(PDOException $e) {
      echo '{"error":{"text":'. $e-&gt;getMessage() .'}}'; 
  }
  }
  </code></pre>
  
  <p>As you can see I fetch all my users with an iPhone and I send them the notification. My phone is the last user in that list and I didnt get it, another user that I know is one of the first ones and she got it.
  It either misses some of the users in the array or it just does the first half, no error is shown.</p>
  
  <p>Im using Slim for this.</p>
  
  <p>hope you can help me, thanks!</p>