作者:闲鱼技术
链接:https://juejin.im/post/5ae194adf265da0b9d77eb87
来源:掘金
iOS8中苹果新引入了PushKit的框架和一种新的push通知类型:VoIP push,提供区别于普通APNS push的能力,通过这种push方式收到消息时会直接将已经杀掉的APP激活。
PushKit主要有3步操作:
1,通过PKPushRegistry注册VoIP服务(一般在APP启动代码里添加)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate = self;
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
return YES;
}
2,实现PKPushRegistryDelegate获取token方法
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
NSString *str = [NSString stringWithFormat:@"%@",credentials.token];
NSString *tokenStr = [[[str stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
//上传token处理
}
3,实现PKPushRegistryDelegate接收VoIP消息方法
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
NSDictionary *alert = [payload.dictionaryPayload[@"aps"] objectForKey:@"alert"];
//调用CallKit处理
}
==========
在做VoIP方案时可能会遇到的问题:
Q:锁屏时收不到VoIP消息的问题
A:开发时遇到一个非锁屏下能正常收到VoIP push,但锁屏时经常收不到的问题,经排查,是锁屏下收到VoIP时APP发生了crash,crash日志里显示的原因是Termination Reason: Namespace SPRINGBOARD,Code 0x8badf00d,这个错误是因为watchdog超时引起,程序启动时,超过了5-6秒APP会被系统杀掉,而系统在锁屏的状态下启动要比激活状态慢很多,很容易触发watchdog的crash。解决的方法就是优化APP启动时的代码,把可以延后的操作尽量延后执行,同时我对设备的cpu也做的了判断,armv7的低端设备启动慢容易超时不使用VoIP,保留APNS发送。
Q:APP启动时收不到VoIP token问题
A:要接收VoIP token 除了要引入PushKit库,注册并实现代理外,还要在工程的Capabilities中打开3个backmode:Background fetch、Remote nofications、Voice over IP,以及Push Notifications(在工程里打开设置,和手机里设置的接收通知权限没有关系,即使用户将设置里的APNS关闭也能收到VoIP消息)。
网友评论