Arshad Zackeriya
Arshad Zackeriya

Reputation: 163

Apple Push notifications working sometimes but not always

I am using php in the backend to send apple push notification. Sometimes i am receiving the notifications sometimes i am not.

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.

ps: this is i am using for production build

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' => $message,
        'badge' => '1',
        'sound' => 'default');
$body['payload'] = array(
        'type' => $not_type,
        'user_id' => $user_id,
        'video_id' => $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);

}

Upvotes: 0

Views: 1307

Answers (2)

RJV Kumar
RJV Kumar

Reputation: 2408

check this link Troubleshooting Push notifications from apple documentation.

There are several issues involved in not getting push notification. Based on your scenario test your code.

Upvotes: 0

Justlike
Justlike

Reputation: 1504

Apple:

Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.

Upvotes: 2

Related Questions