前段時(shí)間,仔細(xì)研究了APNS的文檔,把一些關(guān)鍵的地方記錄了下來(lái),弄懂這些對(duì)于理解APNS的規(guī)則,至關(guān)重要。
1. If APNs attempts to deliver a notification but the device is offline, the notification is stored for a limited period of time, and delivered to the device when it becomes available.
假如用戶手機(jī)不在線,可能沒有信號(hào)或者關(guān)機(jī)吧,APNs會(huì)存儲(chǔ)轉(zhuǎn)發(fā),等用戶在線時(shí)再發(fā)送
2.Only one recent notification for a particular application is stored. If multiple notifications are sent while the device is offline, each new notification causes the prior notification to be discarded. This behavior of keeping only the newest notification is referred to as coalescing notifications.
如果用戶不在線,通知會(huì)合并,只會(huì)保留最新的一條。假如你給用戶發(fā)了兩條通知,但用戶反饋說(shuō),只收到了一條,那么很可能是用戶當(dāng)時(shí)不在線,APNs的合并策略生效,只保留了最近一條
3.If the device remains offline for a long time, any notifications that were being stored for it are discarded
4.The maximum size allowed for a notification payload is 256 bytes; Apple Push Notification Service refuses any notification that exceeds this limit.
這個(gè)很重要,payload,就是最后生成的那段Json,不得超過256字節(jié)。如果超過了,建議去掉一些不需要的參數(shù),把a(bǔ)lert,就是提示信息的字?jǐn)?shù)減少
5.don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack.
6.If you send a notification that is accepted by APNs, nothing is returned.
發(fā)送成功的木有返回,只有發(fā)送失敗的才會(huì)返回
7.If you send a notification that is malformed or otherwise unintelligible, APNs returns an error-response packet and closes the connection. Any notifications that you sent after the malformed notification using the same connection are discarded, and must be resent.
這條非常重要,如果有error-response,那么這條之后的通知都需要重發(fā)。有很多開源的庫(kù),在發(fā)蘋果通知時(shí)都沒有檢測(cè)error-response,如果你不小心用了,那么用戶很可能反饋“怎么沒有通知啊”
8.The notification identifier in the error response indicates the last notification that was successfully sent(實(shí)際情況不是,實(shí)際上返回的是出錯(cuò)的那條通知的ID). Any notifications you sent after it have been discarded and must be resent.When you receive this status code, stop using this connection and open a new connection.
這是對(duì)上一條的補(bǔ)充,如果出錯(cuò)了,需要關(guān)閉當(dāng)前的連接,并且重新連接再發(fā)。error-response中返回的通知ID,可以幫助我們找出哪條出錯(cuò)了,這樣就能知道哪些需要重發(fā)了
9.When a push notification cannot be delivered because the intended app does not exist on the device, the feedback service adds that device’s token to its list.
APNS的feedback service會(huì)返回那些已經(jīng)卸載的設(shè)備的token--device_token。存儲(chǔ)這些token,下次就不用再給他們發(fā)了,可以節(jié)省點(diǎn)資源。需要注意的是:feedback的接口讀取一次,APNS就會(huì)清空它的列表,下次再讀取時(shí),返回的就是這兩次讀取之間這段時(shí)間新產(chǎn)生的device_token。
只有把這些搞清楚,才方便我們理解蘋果推送的規(guī)則,知道自己推送上的一些不足之處。搞懂這些規(guī)則后,我自己封裝了個(gè)Java的類庫(kù),已經(jīng)開源并放到Github上了,下一篇文章奉上詳情。
更多信息請(qǐng)查看IT技術(shù)專欄