Apple Push notifications working sometimes but not always
<p>I am using php in the backend to send apple push notification.
Sometimes i am receiving the notifications sometimes i am not.</p>

<p>i know this is a simple code to use, if there any other ways to send the notifications. cant understand why its not delivering always and sometimes it does.</p>

<p><em>ps: this is i am using for production build</em></p>

<pre><code>function sendPush($user_id,$message,$video_id,$not_type,$deviceToken)

{

$passphrase = 'xxxxx';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);


$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);


$body['aps'] = array(
        'alert' =&gt; $message,
        'badge' =&gt; '1',
        'sound' =&gt; 'default');
$body['payload'] = array(
        'type' =&gt; $not_type,
        'user_id' =&gt; $user_id,
        'video_id' =&gt; $video_id
    );



    $payload = json_encode($body);


    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;


    $result = fwrite($fp, $msg, strlen($msg));


    if (!$result)
        echo 'Message not delivered' . PHP_EOL;
        else
            echo 'Message successfully delivered' . PHP_EOL;

            fclose($fp);

}
</code></pre>