简介
PushKit是苹果在iOS8之后推出的新框架,iOS10之后,苹果更是禁止VOIP应用在后台使用socket长链接,PushKit可以说是为了VOIP而生,满足实时性的同时,还能达到省电的效果,搭配苹果自己的CallKit,可以呈现出类似原生电话通话的效果。
远程推送和本地推送:
区别网上资料太多了,简单说一下,比如今日头条,有什么大的新闻会在手机端接收到推送,这个就是远程推送,是把相关信息推送到苹果推送服务器-APNS。本地推送就是在本地设定一个时间,其实就是一个类似闹钟的功能,当到了设定的时间,就会收到一条推送,这个是和推送服务器没有关系的。
VOIP可以搭配本地推送完美代替APNS推送功能,同时更加强大,在app未开启状态就可以执行代码,具体测试可以唤醒近30秒,而且无需授权,我们在回调里直接加上本地推送完全可以是实现APNS功能,这样的黑科技想象就很激动,让我们赶紧来试一下吧!
VoIP Push Notification
1.注册推送证书
该证书只要选择对应的bundleID,勾选创建对应的 VoIP选项即可;注意该证书不分生产测试,后端只需更换证书流程和接入APNS一样,即刻调起:
2.项目配置
3.plistInfo配置
4.代码配置
导入需要的头文件
#import <PushKit/PushKit.h>
#import <UserNotifications/UserNotifications.h>
#import <AudioToolbox/AudioToolbox.h>
注册本地通知设置代理
PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushRegistry.delegate = self;
pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
//ios10注册本地通知
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
//iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@",settings);
}];
}
代理实现
在代理方法didReceiveIncomingPushWithPayload里可以实现你要在30秒内完成的任务(注意该任务可以在点击你本地通知唤醒后继续执行,类似微信视频呼叫)
#pragma mark -pushkitDelegate
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
if([credentials.token length] == 0) {
NSLog(@"voip token NULL");
return;
}
//应用启动获取token,并上传服务器
token = [[[[credentials.token description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
//token上传服务器
//[self uploadToken];
}
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.body =[NSString localizedUserNotificationStringForKey:[NSString
stringWithFormat:@"%@%@", CallerName,
@"邀请你进行通话。。。。"] arguments:nil];;
UNNotificationSound *customSound = [UNNotificationSound soundNamed:@"voip_call.caf"];
content.sound = customSound;
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:1 repeats:NO];
request = [UNNotificationRequest requestWithIdentifier:@"Voip_Push"
content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"结束");
}];
}else {
callNotification = [[UILocalNotification alloc] init];
callNotification.alertBody = [NSString
stringWithFormat:@"%@%@", CallerName,
@"邀请你进行通话。。。。"];
callNotification.soundName = @"voip_call.caf";
[[UIApplication sharedApplication]
presentLocalNotificationNow:callNotification];
}
/**
初始化计时器 每一秒振动一次
@param playkSystemSound 振动方法
@return
*/
if(_vibrationTimer){
[_vibrationTimer invalidate];
_vibrationTimer = nil;
}else{
_vibrationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playkSystemSound) userInfo:nil repeats:YES];
}
}
}
//振动
- (void)playkSystemSound{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
结语
近期实现VoIP功能做一些记录,希望能帮到大家,有问题可以积极交流,谢谢!