php - Apple 推送通知的 curl 请求太多

标签 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;
    }

}

它运行良好。我还可以检测到无效的 token 。

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

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

最佳答案

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

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

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

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

FCM:

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

一个信号:

https://documentation.onesignal.com/reference

它只会管理自己。

关于php - Apple 推送通知的 curl 请求太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51567689/

相关文章:

php - 从 Linux Windows Azure SDK 为 PHP 项目创建 cspkg 和 cscfg

php - 一个仅将参数传递给另一个函数的函数是否应该对该参数进行类型检查?

php - 如何使用php cURL发送Request Payload?

php - 与 5 个第三方盒子对话的单一证书

swift - 没有开发者账号的苹果推送通知

iOS 推送通知 - 我可以在一分钟内从我的服务器发送多少?

php - Paypal:如何在 payflow 中创建定期配置文件

javascript - IE9 的 AJAX 响应包含无效字符

ruby - Webrick 代理 + cURL 返回错误请求

ios - 解析 Apns 负载