【问题标题】:Apple Push Notification Service with PHP Script使用 PHP 脚本的 Apple 推送通知服务
【发布时间】:2010-10-08 13:42:30
【问题描述】:

我正在尝试向我的 iPhone (APNS) 发送推送通知。我读了这个post 并尝试实现它。所以我所有的证书都很好(正常)。

现在我有了这个 php 脚本:

$device = '4f30e047 c8c05db9 3fa87e7d ca5325f7 738cb2c0 0b4a02d4 d4329a42 a7128173'; // My iphone deviceToken
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');
$payload['server'] = array('serverId' => $serverId, 'name' => $name);
$output = json_encode($payload);

$apnsCert = 'apple_push_notification_production.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

//socket_close($apns); seems to be wrong here ...
fclose($apns);

当我运行这个脚本时,什么都没有发生。没有错误只是一个空白页面,但我的 iPhone 没有收到推送通知...

你有想法吗?

非常感谢!

【问题讨论】:

    标签: php iphone notifications push


    【解决方案1】:

    设备令牌中没有空格,删除它们

    【讨论】:

      【解决方案2】:

      对于开发证书,您需要 url

      'ssl://gateway.sandbox.push.apple.com:2195'
      但对于生产
      'ssl://gateway.push.apple.com:2195'

      【讨论】:

        【解决方案3】:

        从令牌中去除空格,即使那样它也不起作用

        $apnsHost = 'gateway.sandbox.push.apple.com';
        

        从此网址中删除“沙盒”,它将运行。

        【讨论】:

          【解决方案4】:

          如果您有多个设备并且循环遍历它们,我发现您必须为每个 fwrite 创建一个新的 stream_context_create 以防止苹果关闭连接以获取错误令牌。

          仅供参考

          【讨论】:

            【解决方案5】:

            要完全成功发送通知,您还需要做 1 件事。

            替换$output = json_encode($payload);

            $payload = json_encode($payload);

            【讨论】:

              【解决方案6】:

              您缺少提供证书私钥。添加这一行: stream_context_set_option($streamContext, 'ssl', 'passphrase', $certificatePrivateKey);

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-11-18
                • 2010-11-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多