關於PushKit的使用總結

1.PushKit的認識ios

(1)概念git

ios8蘋果新引入了名爲pushkit的框架和一種新的push通知類型,被稱做voip push.該push方式旨在提供區別於普通apns push的能力,經過這種push方式可使app執行制定的代碼(在彈出通知給用戶以前);而該通知的默認行爲和apns通知有所區別,它的默認行爲裏面是不會彈出通知的github

(2)做用服務器

pushkit中的voippush,能夠幫助咱們提高voip應用的體驗,優化voip應用的開發實現,下降voip應用的電量消耗,它須要咱們從新規劃和設計咱們的voip應用,從而獲得更好的體驗(voip push能夠說是準實時的,實側延時1秒左右);蘋果的目的是提供這樣一種能力,可讓咱們拋棄後臺長鏈接的方案,也就是說應用程序一般不用維持和voip服務器的鏈接,在呼叫或者收到呼叫時,完成voip服務器的註冊;當程序被殺死或者手機重啓動時,均可以收到對方的來電,正常開展voip的業務。也就是說,咱們當前能夠利用它來優化voip的體驗,增長接通率;app

2.PushKit的使用教程框架

(1)建立指定APP ID(非通配)ide

  與遠程推送相似,App ID不能使用通配ID必須使用指定APP ID而且生成配置文件中選擇Push Notifications服務,通常的開發配置文件沒法完成註冊;應用程序的Bundle Identifier必須和生成配置文件使用的APP ID徹底一致。測試

(2)建立voip服務的證書優化

跟apns push相似,pushkit的voippush也須要申請證書,voip push的證書申請步驟截圖以下:ui

(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.

Summary of differences:
  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.

相關文章
相關標籤/搜索