苹果消息推送机制

推送目前使用过个推,极光推送,友盟推送等,现在根据这些第三方的推送,总结一下推送的原理。第一阶段: 服务端程序把要发送的消息、目的iPhone的标识打包,发给APNS。 第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。第三阶段:iPhone把发来的消息传递给相应的应用程序, 并且按照设定弹出Push通知。

具体步骤如下

 1、首先是应用程序注册消息推送(当你在第一次启动app的时候,你允许推送就是在注册消息推送,也就是说在settings中对通知的控制就是在注册消息推送)。

   2、 iOS跟APNS Server要deviceToken。应用程序接受deviceToken。

   3、应用程序将deviceToken发送给PUSH服务端程序。

   4、 服务端程序向APNS服务发送消息。

   5、APNS服务将消息发送给iPhone应用程序。

 

正常情况下可以根据device token将消息成功推送到客户端设备中,但是也不排除用户卸载程序的情况,此时推送消息失败,APNs会将这个错误消息通知服务器端以避免资源浪费(服务器端此时可以根据错误删除已经存储的device token,下次不再发送)。

下面是官网的一些有用的介绍 https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html

If APNs attempts to deliver a notification and the destination device is offline, APNs stores the notification for a limited period of time and delivers it when the device becomes available again. This component stores only the most recent notification per device and per app. If a device is offline, sending a new notification causes the previous notification to be discarded. If a device remains offline for a long time, all stored notifications are discarded.

如果APNs想要传递一条消息,但是目的设备没有联网,APNs将会存储消息一段时间并且当设备联网后再次传递消息。

To protect user privacy, do not use device tokens to identify user devices. Device tokens change when the user updates the operating system and when a device’s data and settings are erased. As a result, apps should always request the current device token at launch time. For more information, see Device Token Generation and Dispersal.

Device token可能会改变

 

posted @ 2016-05-30 13:46  秋棠枫眠  阅读(956)  评论(0编辑  收藏  举报