Apple Push Notifications的卷曲请求过多

时间:2018-07-28 03:11:35

标签: php curl apple-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;
    }

}

工作正常。我还可以检测到无效令牌。

我的问题是,当我需要发送1000多个推送通知时,这花费了太多时间。

有没有办法使curl连接保持活动状态并更快地发送通知而不会被苹果服务器阻止?

3 个答案:

答案 0 :(得分:2)

为此,您必须在后台放置通知代码。

您可以引用以下网址:https://coderexample.com/how-to-run-a-php-script-in-background/

因此,您的代码将在1秒钟内执行,并且通知将从后台发送,因此,您不必等到通知完成后才等待响应。

OR

您可以使用第三方通知工具

FCM:

  

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

OneSignal:

  

https://documentation.onesignal.com/reference

它只会管理自己。

答案 1 :(得分:0)

PHP curl库默认情况下应重用HTTP连接,您只需要重用curl句柄。 因此,解决方案是在此方法之外执行一次 $ ch = curl_init($ url); ,然后在处理/发送通知的循环中将$ ch作为参数添加到SendPush()中

这样,您的HTTP连接将保持不变,并节省大量的连接建立时间。免费,无额外的复杂性,您就能获得排队效果。

答案 2 :(得分:-1)

有多个卷曲

<?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
    }
}
}