推送目前使用過個推,極光推送,友盟推送等,如今根據這些第三方的推送,總結一下推送的原理。第一階段: 服務端程序把要發送的消息、目的iPhone的標識打包,發給APNS。 第二階段:APNS在自身的已註冊Push服務的iPhone列表中,查找有相應標識的iPhone,並把消息發到iPhone。第三階段:iPhone把發來的消息傳遞給相應的應用程序, 而且按照設定彈出Push通知。html
具體步驟以下服務器
一、首先是應用程序註冊消息推送(當你在第一次啓動app的時候,你容許推送就是在註冊消息推送,也就是說在settings中對通知的控制就是在註冊消息推送)。app
二、 iOS跟APNS Server要deviceToken。應用程序接受deviceToken。ide
三、應用程序將deviceToken發送給PUSH服務端程序。.net
四、 服務端程序向APNS服務發送消息。component
五、APNS服務將消息發送給iPhone應用程序。orm
正常狀況下能夠根據device token將消息成功推送到客戶端設備中,可是也不排除用戶卸載程序的狀況,此時推送消息失敗,APNs會將這個錯誤消息通知服務器端以免資源浪費(服務器端此時能夠根據錯誤刪除已經存儲的device token,下次再也不發送)。htm
下面是官網的一些有用的介紹 https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.htmltoken
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可能會改變