APNS 那些事!
? ? ? ? 之前在消息推送中間件APush里實(shí)現(xiàn)了對(duì)APNS的橋接。并利用業(yè)余時(shí)間閱讀了官方指南Local and Push?Notification?Programming Guide。蠻有心得的。稍作總結(jié)。分享給大家,希望可以喜歡。歡迎留言討論!
1. ?APNS 通道環(huán)境
? ? ? 作為一個(gè)黑盒的消息推送服務(wù)。APNS為我們提供了開(kāi)發(fā)和產(chǎn)品兩套環(huán)境。這兩套環(huán)境除了Host name不同外,授權(quán)證書也不近同樣(證書里面會(huì)包括APP相關(guān)信息。如application bundle ID,在你創(chuàng)建不同的profile的時(shí)候,這些信息會(huì)自己主動(dòng)加入進(jìn)去)。當(dāng)然Device Token也不同。以下的英文或許能更好地描寫敘述這兩套環(huán)境的不同:
Development: Use the development environment for initial development and testing of the provider?application. It provides the same set of services as the production environment, although with a smaller?number of server units. The development environment also acts as a virtual device, enabling simulated?end-to-end testing.You access the development environment atgateway.sandbox.push.apple.com , outbound TCP port?2195.
Production: Use the production environment when building the production version of the provider?application. Applications using the production environment must meet Apple’s reliability requirements.You access the production environment atgateway.push.apple.com , outbound TCP port 2195.
2. ?APNS 消息格式
? ? ?APNS 採(cǎi)用二進(jìn)制消息協(xié)議,例如以下:
? ? ?看官方的解釋,蠻清晰的:
? ? ?
? ? ?注意:
? ? ? ?假設(shè)APNS成功接收到你的消息,它將什么也不返回;
? ? ? ?假設(shè)你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. 這點(diǎn)要格外重要,所以非常多開(kāi)源工具包,例如說(shuō)pushy提供了一個(gè)FailedConnectionListener,你能夠通過(guò)實(shí)現(xiàn)該接口,方便的實(shí)現(xiàn)resent,代碼片段例如以下:
private class MyFailedConnectionListener implements FailedConnectionListener<SimpleApnsPushNotification> {public void handleFailedConnection(final PushManager<? extends SimpleApnsPushNotification> pushManager,final Throwable cause) {if (cause instanceof SSLHandshakeException) {// This is probably a permanent failure, and we should shut down// the PushManager.}} }// ...pushManager.registerFailedConnectionListener(new MyFailedConnectionListener());? ? ? ? ?error-response packet 結(jié)構(gòu)例如以下:
? ? ? ? ?Status code 解釋例如以下:
? ???
? ? ? A status code of 10 indicates that the APNs server closed the connection (for example, to perform maintenance).The notification identifier in the error response indicates the last notification that was successfully sent. 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.
3. ?APNS 消息接收
? ? ?An application must register with Apple Push Notification service for the operating systems on a device and?on a computer to receive remote notifications sent by the application’s provider. Registration has three stages:
?3.1 The app registers for remote notifications.
?3.2 The system sets up remote notifications for the app and, if registration is successful, passes a device token to the app delegate.
?3.3 The app sends its device token to the push provider.
?3.4 Device tokens can change. Your app needs to reregister every time it is launched—in iOS by calling the registerForRemoteNotificationTypes: method of UIApplication, and in OS X by calling the registerForRemoteNotificationTypes: method of NSApplication.
?3.5 Because the only notification type supported for non-running applications is icon-badging,pass NSRemoteNotificationTypeBadge as the parameter of registerForRemoteNotificationTypes:.
?3.6 If registration is successful, APNs returns a device token to the device and iOS passes the token to the app delegate in the application:didRegisterForRemoteNotificationsWithDeviceToken: method. If there is a problem in obtaining the token, the operating system informs the delegate by calling the application:didFailToRegisterForRemoteNotificationsWithError: method.
? ? ? 用一幅圖來(lái)說(shuō)明這個(gè)流程。例如以下:
4. ?APNS Qos
? ? Apple Push Notification service includes a default Quality of Service (QoS) component that performs a?store-and-forward function.
? ? 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.
? ? 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.
? ? If the device remains offline for a long time, any notifications that were being stored for it are discarded.
? ? 注意我字體標(biāo)黑的這個(gè)APNS的特性!
5. ?APNS ?FAQ
? ? a. APNS feedback 服務(wù)是用來(lái)做什么的?
? ? ? The Apple Push Notification Service includes a feedback service to give you information about failed push notifications. 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. Push notifications that expire before being delivered are not considered a failed delivery and don’t impact the feedback service. By using this information to stop sending push notifications that will fail to be delivered, you reduce unnecessary message overhead and improve overall system performance.
? ? ? Query the feedback service daily to get the list of device tokens. Use the timestamp to verify that the device tokens haven’t been reregistered since the feedback entry was generated. For each device that has not been reregistered, stop sending notifications. APNs monitors providers for their diligence in checking the feedback service and refraining from sending push notifications to nonexistent applications on devices. ? ?
? ? ? The feedback service maintains a separate list for each push topic. If you have multiple apps, you must connect to the feedback service once for each app, using the corresponding certificate, in order to receive all feedback.
? ? ? The feedback service has a binary interface similar to the interface used for sending push notifications. You access the production feedback service via feedback.push.apple.com on port 2196 and the development feedback service via feedback.sandbox.push.apple.com on port 2196. As with the binary interface for?push notifications, use TLS (or SSL) to establish a secured communications channel. You use the same SSL certificate for connecting to the feedback service as you use for sending notifications. To establish a trusted provider identity, present this certificate to APNs at connection time using peer-to-peer authentication.
? ? ? Once you are connected, transmission begins immediately; you do not need to send any command to APNs.Read the stream from the feedback service until there is no more data to read.
? ? ? The feedback service’s list is cleared after you read it. Each time you connect to the feedback service, the information it returns lists only the failures that have happened since you last connected.
? ? b. Best Practices for Managing Connections
? ? ? ?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.
? ? ? ?Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close?connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave?a connection open unless you know it will be idle for an extended period of time—for example, if you only?send notifications to your users once a day it is ok to use a new connection each day.
? ?c. 接收到APNS消息后。我一般怎么處理呢?
? ??If your app is frontmost, the application:didReceiveRemoteNotification: or?application:didReceiveLocalNotification:method is called on its app delegate;
? ? If your app is not frontmost or not running, you handle the notifications by checking the options dictionary passed to the?application:didFinishLaunchingWithOptions: of your app delegate for either the?UIApplicationLaunchOptionsLocalNotificationKey or?UIApplicationLaunchOptionsRemoteNotificationKey key.
? ? 實(shí)例代碼例如以下:
d. ?消息發(fā)送過(guò)程中,突然出現(xiàn)Write failed: Broken pipe
? ?可能由于某種原因,如發(fā)送了錯(cuò)誤的token,導(dǎo)致Apple強(qiáng)行關(guān)閉了SSL連接。跳過(guò)最后一次發(fā)送的信息,又一次連接,繼續(xù)發(fā)送錯(cuò)誤信息之后的信息。
e. ?由于APNS發(fā)送成功沒(méi)有不論什么返回,會(huì)不會(huì)出現(xiàn)是異??墒钱惓P畔⑦€沒(méi)返回的現(xiàn)象
? ?有可能,這個(gè)時(shí)候能夠採(cǎi)取批量發(fā)送,等候一段時(shí)間。例如說(shuō)1秒。通過(guò)Feedback來(lái)檢查發(fā)送狀態(tài)。
f. ? 一些你可能會(huì)忽視的細(xì)節(jié):
? ? ? Each application on a device is limited to 64 scheduled local notifications. The system discards scheduled notifications in excess of this limit, keeping only the 64 notifications that will fire the soonest. Recurring notifications are treated as a single notification.
? ? ? Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.
參考文獻(xiàn):
? ? ?1.?Local and Push?Notification?Programming Guide
? ? ?2.?http://www.easyapns.com/apple-delegate
? ? ?3.?http://blog.csdn.net/tlq1988/article/details/9612237
? ? ?4.?http://www.cocoachina.com/bbs/read.php?tid=98797
總結(jié)
- 上一篇: java并发编程与线程安全
- 下一篇: 《解释的工具:生活中的经济学原理 读书笔