简体   繁体   中英

Too many curl request for Apple Push Notifications

I am using this function to send push notifications

function SendPush($token,$message,$badge,$eventid) {

    $device_token   = $token;
    $pem_file       = '../pushcert.pem';
    $pem_secret     = 'pass';
    $apns_topic     = 'com.topic';


    $sample_alert = '{"aps":{"alert":"'. $message .'","sound":"default","badge":'. $badge .'}, "type":"attend", "eventID":"'.$eventid.'"}';
    $url = "https://api.push.apple.com/3/device/$device_token";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
    curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $response = curl_exec($ch);
    $sonuc = json_decode($response,true);

    if ($sonuc['reason'] == "BadDeviceToken" || $sonuc['reason'] == "Unregistered" ) {
        return false;
    } else {
        return true;
    }

}

It is working good. I can also detect the invalid tokens.

My problem is when I need to send over 1000 push notifications It takes too much time.

Is there a way to keep curl connection alive and send notifications faster without getting blocked by apple servers?

For that, you have to put notification code in the background.

You can refer this URL: https://coderexample.com/how-to-run-a-php-script-in-background/

So, Your code will execute in 1 second and your notification will send from a background so, you do not wait for response till notification will be done.

OR

You can use Third party Notification tools

FCM:

https://gist.github.com/rolinger/d6500d65128db95f004041c2b636753a

OneSignal:

https://documentation.onesignal.com/reference

It will manage themselves only.

PHP curl library should reuse HTTP connections by default, you only need to reuse curl handle. So, solution is to do $ch = curl_init($url); once, outside of this method, and then add $ch as an argument to SendPush() in your loop where you process/send notifications.

This way, your HTTP connection will be persistent and lots of connection establishment time will be saved. At no cost and additional complexity, you get queuing effect.

there is curl multi

<?php
//$message should be an array with the details
function SendPush($messages) {
$mh = curl_multi_init();
$ch = array();
foreach($messages as $key=>$mess)
{
    $device_token   = $mess['token'];
    $pem_file       = '../pushcert.pem';
    $pem_secret     = 'pass';
    $apns_topic     = 'com.topic';
    $sample_alert = '{"aps":{"alert":"'. $mess['message'] .'","sound":"default","badge":'. $mess['badge'] .'}, "type":"attend", "eventID":"'.$mess['eventid'].'"}';
    $url = "https://api.push.apple.com/3/device/$device_token";
    $ch[$key]=curl_init($url);
    curl_setopt($ch[$key], CURLOPT_POSTFIELDS, $sample_alert);
    curl_setopt($ch[$key], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_setopt($ch[$key],CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch[$key], CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
    curl_setopt($ch[$key], CURLOPT_SSLCERT, $pem_file);
    curl_setopt($ch[$key], CURLOPT_SSLCERTPASSWD, $pem_secret);
    curl_setopt($ch[$key], CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch[$key], CURLOPT_TIMEOUT, 15);
    curl_multi_add_handle($mh,$ch[$key]);

}
$running = null;
do {
  curl_multi_exec($mh, $running);
} while ($running);

$sonuc = json_decode($response,true);
foreach($ch as $curl)
{
    $response = curl_multi_getcontent($curl);
    if ($sonuc['reason'] == "BadDeviceToken" || $sonuc['reason'] == "Unregistered" ) {
        //return false;
        //handle the bad device
    } else {
        //return true;
        //device ok
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM