我正在使用这个函数发送推送通知
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
一个信号:
它只会管理自己。
关于php - Apple 推送通知的 curl 请求太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51567689/