关于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.

相关文章
相关标签/搜索