简体   繁体   English

Apple推送通知的卷曲请求太多

[英]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. 我的问题是当我需要发送超过1000个推送通知时需要花费太多时间。

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/ 您可以参考以下网址: 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. 因此,您的代码将在1秒后执行,您的通知将从后台发送,因此,您不会等待响应,直到通知完成。

OR 要么

You can use Third party Notification tools 您可以使用第三方通知工具

FCM: FCM:

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

OneSignal: OneSignal:

https://documentation.onesignal.com/reference 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. PHP curl库默认情况下应该重用HTTP连接,你只需要重用curl句柄。 So, solution is to do $ch = curl_init($url); 所以,解决方案是做$ 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. 一次,在此方法之外,然后将$ ch作为参数添加到您处理/发送通知的循环中的SendPush()。

This way, your HTTP connection will be persistent and lots of connection establishment time will be saved. 这样,您的HTTP连接将是持久的,并且将节省大量的连接建立时间。 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
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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