title: ios push notification performance
body: <p>This is my php push notification code for ios below:</p>
  
  <pre><code>&lt;?php
  // Put your device token here (without spaces):
  $deviceToken = "********************************************";
  
  // Put your private key's passphrase here:
  $passphrase = "********";
  
  function pushIOSNotification($message) 
  {
     GLOBAL $deviceToken;
     GLOBAL $passphrase;
  
     $ctx = stream_context_create();
  
     $path = dirname(__FILE__).'/ck.pem';
     //echo $path;
  
     stream_context_set_option($ctx, 'ssl', 'local_cert', $path);
     stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  
     // Open a connection to the APNS server
     $fp = stream_socket_client(
          'ssl://gateway.sandbox.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);
  }
  </code></pre>
  
  <p>This code is only for one device,</p>
  
  <p>if I need send to everyone and support localization,</p>
  
  <p>this code will be like these below.</p>
  
  <pre><code>foreach($deviceTokens as $deviceToken){
     $deviceOS = "query this device os from  memcache";
     if($deviceOS == "ios")
     {
         $lang = "query this device langauge from  memcache";
         $message = $messageData[$lang];
         pushIOSNotification($deviceToken,$message);
  
     }
  } 
  </code></pre>
  
  <p>but I doubt this performance of this code,
  because deviceTokens length is the number of all members.</p>