【发布时间】:2023-03-25 13:45:01
【问题描述】:
此脚本用于在 Apple 切换到 new APNs Provider API 之前向测试设备发送测试推送通知,我很惊讶更多人没有谈论它。
现在,当我从终端输入 php simplepush.php 运行脚本时,输出显示
虽然没有收到任何消息。
//simplepush.php
<?php
// Put your device token here (without spaces):
$deviceToken = 'EXAMPLETOKEN';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'Hello!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://api.sandbox.push.apple.com:443', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
自从 Apple 于去年 3 月做出转换以来,我已经:
- 创建了一个新的Certificate Signing Request
- Registered a new App ID
- Established a Certificate-Based Connection to APNs
- Obtained a new Provider Certificate from Apple
- Installed the Certificate and Private Key
- Established Trust with APNs
- Sent Push Notifications Using Command-Line Tools
当我关注Sending Push Notifications Using Command-Line Tools 时,我能够收到测试通知。
curl -v --header 'apns-topic: com.your.site' --header apns-push-type: alert --cert aps.cer --cert-type DER --key PushChatKey.pem --key-type PEM --data '{"aps":{"alert":"Test"}}' --http2 https://api.sandbox.push.apple.com/3/device/DEVICETOKEN
自从苹果对新的 APNs Provider API 进行了更改后,是否仍然可以使用这个 php 脚本来发送测试推送通知?我知道 Apple 开发人员文档中提到了 required header request fields,但老实说,我不知道这些是从终端实现还是直接在脚本中实现。
【问题讨论】:
-
$result = fwrite($fp, $msg, strlen($msg)); -
您那里的代码看起来很古老,与您链接的文档中描述的过程完全不同。这似乎不太可能是您需要自己编写的代码。肯定有具有此功能的库。
-
另外来自您链接到的文档:“自 2021 年 3 月 31 日起,APN 将不再支持旧的二进制协议。我们建议尽快从此页面更新到基于 HTTP/2 的 API 。”
-
miken32提到,推荐使用http/2 API。有什么不使用图书馆的特殊理由吗?喜欢github.com/gepo/apns-http2
-
@PauloH。只是熟悉。这是 2013 年教程的第一部分,该教程在相同的证书和配置文件用于 API 之前发送测试通知,该 API 使用 iOS 的 APNs 发送带有 UI 更新的用户消息。
标签: php terminal apple-push-notifications apns-php