iOS APNS:使用 PHP 创建推送通知队列
简短的问题:如何在 PHP 中构建多个推送通知的传递?具体来说,如何将多个推送通知消息“打包”到单个 fwrite() 调用中?
我可以简单地继续向 $apnsMessage 字符串附加更多消息/有效负载吗?:
// [connect to service]
// Packing the payload (for a single message)
$apnsMessage = chr(0) . chr(0) . chr(32);
$apnsMessage .= pack('H*', str_replace(' ', '', $recipientToken));
$apnsMessage .= chr(0) . chr(strlen($payload)) . $payload;
// Write the payload to the APNS
fwrite($apns, $apnsMessage);
// [close connection to service]
长版本:
因为 Apple 要求应用程序批量处理推送通知(最大限度地减少与其 APNS 的多个连续连接),所以我尝试用 PHP 构建一些东西可以完成这项工作(不使用 php-apns lib 和 memcache)。因为我能找到的 99% 的资源都与单个推送通知有关,所以我希望能在这里找到一些指导。我只是将每条消息添加到 mysql 表(队列)中,然后每隔 x 分钟,迭代它们并发送所有未发送的消息。
有谁有任何示例/链接可能有助于此方法?
提前致谢。
The short question: How do I structure the delivery of multiple push notifications in PHP? Specifically, how do I "pack" multiple push notification messages into a single fwrite() call?
Can I simply continue to append more messages/payloads to the $apnsMessage string?:
// [connect to service]
// Packing the payload (for a single message)
$apnsMessage = chr(0) . chr(0) . chr(32);
$apnsMessage .= pack('H*', str_replace(' ', '', $recipientToken));
$apnsMessage .= chr(0) . chr(strlen($payload)) . $payload;
// Write the payload to the APNS
fwrite($apns, $apnsMessage);
// [close connection to service]
The long version:
Because Apple requires applications to batch-process push notifications (minimizing several successive connections to their APNS), I'm trying to build something in PHP that can do the job (without using the php-apns lib & memcache). Because 99% of the resources I can find are concerning a single push notification, I was hoping I could find some guidance here. I'm simply adding each message into a mysql table (queue), then every x minutes, iterating through them and sending all the unsent messages.
Does anyone have any examples / links that might help with this approach?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以简单地继续附加消息。二进制协议格式特别要求严格的消息长度,以便可以将一条消息与下一条消息区分开来。理想情况下,您会整天将一个长二进制字符串推送到 Apple 的服务器。
批处理并不理想,您通常希望将其实现为守护程序,以保持与 APNs 服务器的开放连接,并根据需要将新消息写入连接。
Yes, you can simply keep appending messages. The binary protocol format specifically requires strict message lengths so one message can be distinguished from the next in line. Ideally you'd be pushing one long binary string to Apple's servers all day long.
Batch processing is not ideal, you'd usually want to implement this as a daemon that keeps an open connection to the APNs servers and writes new messages to the connection as required.