php - iOS 推送通知问题 它工作了一段时间,但有时不工作

标签 php ios curl push-notification apple-push-notifications

我已经通过从 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

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

FCM

OneSignal

它会自动管理。

关于php - iOS 推送通知问题 它工作了一段时间,但有时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571828/

相关文章:

javascript - 需要用 js/php 库插入正确的方向

ios - 如何让我的应用程序显示另一个横向屏幕和横向屏幕,iPhone X iOS

ios - Swift - 试图获取当前位置

php - Paypal IPN : how get the POSTs from this class?

java - JSON语法错误的php函数?

PHP - 检查文件夹中是否存在文件

ios - 选择 HealthKit HKActivitySummary -> "NSInvalidArgumentException", "startDateComponents: Date components require a calendar."

bash - curl: 没有为 restful api 指定 URL

curl - 是否可以使用 curl 工具对 RTSP 流进行简单的健康检查?

javascript - JS中的哈希密码