我已经通过从 URL 运行来检查我的代码,当我从浏览器尝试时它工作正常。但它不适用于 curl 代码,我找不到解决方案。 curl_error
什么也没给出。我读过“设备不会总是连接到相同的公共(public) IP 地址以获取通知。”但是解决方案是什么请建议 curl 代码有什么问题/其他问题的可能性。
这是curl代码
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, BASE_URL .'api-v2/admin_notification_cron.php');
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
$result = curl_exec($ch );
curl_close( $ch );
和admin_notification_cron.php
文件从数据库中获取数据,每1分钟从数据库中获取20个数据后调用sendNotification函数
下面是sendNotification
函数代码:
function sendToIphone($deviceToken, $data)
{
$data['push_image'] = (($data['push_image']!='')?PHOTO_URL.'push/'.$data['push_image']:'');
$passphrase = '';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns-dev-cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', '');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$type = ($data['type']!='')?$data['type']:'0';
if ($data['message']=='add_job' || $data['message']=='process') {
//$sound = 'newrequest.mp3';
$sound = 'default';
} else {
$sound = 'default';
}
$body['aps'] = array(
'alert' => stripslashes($final_message),
'title' => APP_TITLE,
'job_id' => $data['job_id'],
'user_id' => $data['user_id'],
'created'=> $data['created'],
'type'=> $type,
'sound' => $sound,
"mutable-content"=> 1,
"category"=> "rich-apns",
"image-url"=> $data['push_image'],
'badge' => intval($badge)
);
$body['att'] = array('id' => $data['push_image']);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
return 0;
} else {
return 1;
}
}
提前致谢
最佳答案
实现 APNS
有一些经验法则。
Curl 连接应在发送
通知
后关闭。您必须等到您之前的
通知
没有 交付。
If you are executing all code in loop then some notification delivered and rest of notification will be discarded.
为了解决您的问题,您在两次触发的通知
之间设置了一些延迟,或者在之前的notification
发送之后触发了notification
。
或
您可以使用第三方通知工具
它会自动管理。
关于php - iOS 推送通知问题 它工作了一段时间,但有时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571828/