使用 PHP 脚本的 Apple 推送通知服务

发布于 2024-09-26 20:07:57 字数 1203 浏览 1 评论 0原文

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

现在我有这个 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 没有收到推送通知...

您有主意吗?

多谢 !

I'm trying so send push notifications ton my iPhone (APNS). I read this post and try to implement it. So all my certificates are good (normaly).

Now I have this php script :

$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);

When I run this script nothing happen. no error just an empty page but my iPhone doesn't receive the push notification ...

Have you got an idea?

Thanks a lot !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瑾夏年华 2024-10-03 20:07:57

设备令牌中没有空格,将其删除

no space in device token, strip them

木有鱼丸 2024-10-03 20:07:57

对于开发证书,您需要 url

'ssl://gateway.sandbox.push.apple.com:2195'

but for production

'ssl://gateway.push.apple.com:2195'

For development certificate you need url

'ssl://gateway.sandbox.push.apple.com:2195'

but for production

'ssl://gateway.push.apple.com:2195'

迷你仙 2024-10-03 20:07:57

从令牌中删除空格,即使如此,它也不起作用,然后

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

从该网址中删除“沙箱”,它将运行。

Strip the spaces from token and even then it's not work then

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

Remove "sandbox" from this url and it will run.

感悟人生的甜 2024-10-03 20:07:57

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

供参考

If you have more than one device and you loop through them I found that you have to create a new stream_context_create for each fwrite to prevent apple from closing the connection for a bad token.

FYI

我三岁 2024-10-03 20:07:57

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

$output = json_encode($payload); 替换

$payload = json_encode($payload);

To send notifications success fully you have to do 1 more thing.

Replace $output = json_encode($payload);

to $payload = json_encode($payload);

浅暮の光 2024-10-03 20:07:57

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

You are missing to provice certificate private key. Add this row:
stream_context_set_option($streamContext, 'ssl', 'passphrase', $certificatePrivateKey);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文