douxi2011 2014-09-11 21:17
浏览 4

发送APNS消息数组而不是发送单个消息

foreach ($users AS $userToken) {
    $msg = chr(0) . pack('n', 32) . pack('H*', $userToken) . 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;
}

For reference, $fp is a 'PHP Stream Context'. $users is an array of user tokens that have registered for push notifications.

Is there a better way of writing the above code so that fwrite can somehow send an array of messages to APNS rather than writing to the socket for each individual user?

  • 写回答

1条回答 默认 最新

  • douyan2680 2014-09-11 21:36
    关注

    Notification payload

    Push notifications are not intended to contain the actual payload (in your case the list of messages) but to notify the subscriber about new content. When receiving the notification the application retrieves the actual content from a web service.

    From the docs:

    Important: Delivery of notifications is a “best effort”, not guaranteed. It is not intended to deliver data to your app, only to notify the user that there is new data available.

    (Source: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html)

    Additionally, the size of the notification payload is limited to 256 bytes.

    The maximum size allowed for a notification payload is 256 bytes

    (Source: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html)

    As a result you are supposed to provide a web service for your app to retrieve the actual list of messages. The notification payload only contains the number of new messages.

    Communitcation with APNS

    Your concept of sending individual requests for every user is correct, this is the way the APNS is designed. To increase the overall performance use multiple connections (using multiple threads) and ensure the underlying TCP connection is reused.

    From the docs:

    You may establish multiple connections to the same gateway or to multiple gateway instances. If you need to send a large number of push notifications, spread them out over connections to several different gateways. This improves performance compared to using a single connection: it lets you send the push notifications faster, and it lets APNs deliver them faster.

    (Source: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW6)

    Keep in mind that it is your job to use the Feedback Service to remove unsubscribed device IDs from your internal list of subscribers. This also helps you in increasing the overall performance.

    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表