Shefali
Shefali

Reputation: 492

iOS push notification issue It's working some time and not working sometime

I have checked my code by running from URL it's working fine while I am trying from the browser. but it's not working with the curl code and I can not find the solution. curl_error gives nothing. I have read that "devices won't always connect to the same public IP address for notifications. " but what is the solution for that Please suggest what is wrong with the curl code/ other issues possibilities.

Here is the curl code

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, BASE_URL .'api-v2/admin_notification_cron.php');
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
$result = curl_exec($ch );
curl_close( $ch );

And admin_notification_cron.php file fetch data from the database and call the sendNotification function after fetching 20 data from database every 1 Minuit

Below is the sendNotification function code:

    function sendToIphone($deviceToken, $data)
    {

    $data['push_image'] = (($data['push_image']!='')?PHOTO_URL.'push/'.$data['push_image']:'');   

    $passphrase = '';
    $ctx = stream_context_create();

    stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev-cert.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', '');
    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    $type = ($data['type']!='')?$data['type']:'0';

    if ($data['message']=='add_job' || $data['message']=='process') {
        //$sound = 'newrequest.mp3';
        $sound = 'default';
    } else {
        $sound = 'default';
    }


    $body['aps'] = array(
        'alert' => stripslashes($final_message),
        'title' => APP_TITLE,
        'job_id' => $data['job_id'],
        'user_id' => $data['user_id'],
        'created'=> $data['created'],
        'type'=> $type,
        'sound' => $sound,
        "mutable-content"=> 1,
        "category"=> "rich-apns",
        "image-url"=> $data['push_image'],
        'badge' => intval($badge)

    );
    $body['att'] = array('id' => $data['push_image']);

    $payload = json_encode($body);

    $msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;
    $result = fwrite($fp, $msg, strlen($msg));
    if (!$result) {
        return 0;
    } else {
        return 1;
    }
}

Thanks in advance

Upvotes: 1

Views: 222

Answers (1)

Hitesh Surani
Hitesh Surani

Reputation: 13577

There are some thumb rules to implement APNS.

  • Curl connection should be closed after delivered notification.

  • You have to wait until your previous notification was not delivered.

If you are executing all code in loop then some notification delivered and rest of notification will be discarded.

To fix your issue you have put some delay between two notification triggered or fire notification after previous notification delivered.

OR

You can use Third party Notification tools

FCM

OneSignal

It will manage automatically.

Upvotes: 1

Related Questions