【问题标题】:Apple Push notifications working sometimes but not alwaysApple推送通知有时但并非总是有效
【发布时间】:2016-01-21 09:14:00
【问题描述】:

我在后端使用 php 来发送苹果推送通知。 有时我会收到通知,有时我不会。

如果有任何其他方式发送通知,我知道这是一个简单的代码。无法理解为什么它不总是提供,有时却提供。

ps:这是我用于生产构建的

function sendPush($user_id,$message,$video_id,$not_type,$deviceToken)

{

$passphrase = 'xxxxx';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);


$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);


$body['aps'] = array(
        'alert' => $message,
        'badge' => '1',
        'sound' => 'default');
$body['payload'] = array(
        'type' => $not_type,
        'user_id' => $user_id,
        'video_id' => $video_id
    );



    $payload = json_encode($body);


    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;


    $result = fwrite($fp, $msg, strlen($msg));


    if (!$result)
        echo 'Message not delivered' . PHP_EOL;
        else
            echo 'Message successfully delivered' . PHP_EOL;

            fclose($fp);

}

【问题讨论】:

  • 我相信这个问题对于 iOS 开发者来说并不新鲜。请参考这个answer

标签: php ios web-services apple-push-notifications


【解决方案1】:

Apple

保持与 APN 的连接在多个 通知; don’t repeatedly open and close connections。 APNs 将快速连接和断开连接视为拒绝服务 攻击。你应该保持连接打开,除非你知道它会 长时间闲置——例如,如果您只发送 每天向您的用户发送一次通知,可以使用新的 每天连接。

【讨论】:

    【解决方案2】:

    从苹果文档中查看此链接Troubleshooting Push notifications

    没有收到推送通知涉及几个问题。根据您的场景测试您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 2014-12-01
      • 2012-09-06
      • 2019-08-08
      相关资源
      最近更新 更多