1.PushKit的認識
(1)概念
ios8蘋果新引入了名為pushkit的框架和一種新的push通知類型,被稱作voip push.該push方式旨在提供區別於普通apns push的能力,通過這種push方式可以使app執行制定的代碼(在彈出通知給用戶之前);而該通知的默認行為和apns通知有所區別,它的默認行為里面是不會彈出通知的
(2)作用
pushkit中的voippush,可以幫助我們提升voip應用的體驗,優化voip應用的開發實現,降低voip應用的電量消耗,它需要我們重新規划和設計我們的voip應用,從而得到更好的體驗(voip push可以說是准實時的,實側延時1秒左右);蘋果的目的是提供這樣一種能力,可以讓我們拋棄后台長連接的方案,也就是說應用程序通常不用維持和voip服務器的連接,在呼叫或者收到呼叫時,完成voip服務器的注冊;當程序被殺死或者手機重啟動時,都可以收到對方的來電,正常開展voip的業務。也就是說,我們當前可以利用它來優化voip的體驗,增加接通率;
2.PushKit的使用教程
(1)創建指定APP ID(非通配)
與遠程推送類似,App ID不能使用通配ID必須使用指定APP ID並且生成配置文件中選擇Push Notifications服務,一般的開發配置文件無法完成注冊;應用程序的Bundle Identifier必須和生成配置文件使用的APP ID完全一致。
(2)創建voip服務的證書
跟apns push類似,pushkit的voippush也需要申請證書,voip push的證書申請步驟截圖如下:





(3)導出證書為.pem格式(證書和私鑰)
詳細方法見:http://www.cnblogs.com/cy568searchx/
(4)把.pem文件提供給服務端
(5)在appDelegate中注冊PushKit的服務(與注冊通知相同),ios8.0方式如下:
UIUserNotificationSettings *userNotifySetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:userNotifySetting]; [[UIApplication sharedApplication] registerForRemoteNotifications];
3 代碼中集成
(1)在應用啟動(appdelegate的didfinishlaunchwithoptions)后或根控制器的初始化等方法內調用如下代碼:
1 PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()]; 2 pushRegistry.delegate = self; 3 pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
(2) 在appdelegate或框架viewcontroller類中實現voip push的代理:
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type { }
上面的代理方法是設備從蘋果服務器獲取到了voip token,然后傳遞給應用程序;我們需要把這個token傳遞到push服務器(和apns push類似,我們也是要傳遞apns token到push服務器,但是這兩個token的獲取方式不同,分別在不同的代理方法中回調給應用,且這兩個token的內容也是不同的)。
(3)在一切正常的情況下,push server在獲取到用戶的voip token之后,如下回調會在push server下發消息到對應token的設備時被觸發
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type { }
上面的回調代碼里僅僅打印了日志,觸發了一個本地通知;這個代理方法是收到voip push通知時觸發的;
如果一切正常,該通知在手機重啟、應用被系統回收、手動kill程序的情況下,依然能夠被觸發,且可以有一段時間用來執行自己的代碼(比如voip注冊等)
注:應用申請一個精確ID的mobile provision打包;
為了測試方便:可以通過一個網上的一個MAC應用Demo:PushMeBaby
下面是比較官方的一份說明文檔:
What PushKit does and why you should use it.
In iOS 8 Apple introduced PushKit as part of their effort to improve battery life, performance, and stability for VoIP applications such as Skype, WhatsApp, and LINE.
Previously, VoIP apps needed to maintain a persistent connection in order to receive calls. Keeping a connection open in the background, drains the battery as well as causes all kinds of problems when the app crashes or is terminated by the user.
PushKit is meant to solve these problems by offering a high-priorty push notification with a large payload. The VoIP app receives the notification in the background, sets up the connection and displays a local notification to the user. When the user swipes the notification, the call is ready to connect.
This guide will walk you through the steps to setup a VoIP application. We'll be using Swift to implement this example. Source files from this example are available on Github.
Differences from regular APNS Push Notifications
VoIP push notifications are different than regular APNS notifications mainly in how they are setup from the iOS app. Instead of using application.registerForRemoteNotifications() and handling the received notifications in application:didReceiveRemoteNotification, we use PushKit and thePKPushRegistryDelegate to request a device token and handle the delegate methods.
Unlike regular push notifications, PushKit does not prompt the user to accept VoIP pushes. PushKit will always grant a device token to apps that have the VoIP entitlements without asking for approval. Further, VoIP pushes do not have any UI and do not show an alert. They act more like content-available pushes, and you must handle the received notification and present a local notification.
| Regular Push | VoIP Push | |
|---|---|---|
| Getting Device Token | application.registerForRemoteNotifications() |
set PKPushRegistry.desiredPushTypes |
| Handle Registration | application:didRegisterForRemoteNotificationsWithDeviceToken: |
pushRegistry:didUpdatePushCredentials |
| Handle Received Notification | application:didReceiveRemoteNotification |
pushRegistry:didReceiveIncomingPushWithPayload |
| Payload Size | 2048 bytes | 4096 bytes |
| Certificate Type | iOS Push Services | VoIP Services |
| Requires User Consent | Yes | No* |
* The user must agree to receive local notifications.
