Utk*_*maz 5 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;
}
}
Run Code Online (Sandbox Code Playgroud)
它运行良好。我还可以检测到无效的令牌。
我的问题是当我需要发送超过 1000 条推送通知时需要太多时间。
有没有办法让 curl 连接保持活动状态并更快地发送通知而不会被苹果服务器阻止?
Din*_*and -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
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1186 次 |
| 最近记录: |