簡介
PushKit是蘋果在iOS8之后推出的新框架,iOS10之后榄檬,蘋果更是禁止VOIP應用在后臺使用socket長鏈接,PushKit可以說是為了VOIP而生,滿足實時性的同時奠骄,還能達到省電的效果,搭配蘋果自己的CallKit番刊,可以呈現(xiàn)出類似原生電話通話的效果含鳞。
遠程推送和本地推送:
區(qū)別網(wǎng)上資料太多了,簡單說一下芹务,比如今日頭條蝉绷,有什么大的新聞會在手機端接收到推送,這個就是遠程推送枣抱,是把相關信息推送到蘋果推送服務器-APNS熔吗。本地推送就是在本地設定一個時間,其實就是一個類似鬧鐘的功能佳晶,當?shù)搅嗽O定的時間桅狠,就會收到一條推送,這個是和推送服務器沒有關系的轿秧。
VOIP可以搭配本地推送完美代替APNS推送功能中跌,同時更加強大,在app未開啟狀態(tài)就可以執(zhí)行代碼菇篡,具體測試可以喚醒近30秒漩符,而且無需授權,我們在回調里直接加上本地推送完全可以是實現(xiàn)APNS功能驱还,這樣的黑科技想象就很激動嗜暴,讓我們趕緊來試一下吧!
VoIP Push Notification
1.注冊推送證書
該證書只要選擇對應的bundleID,勾選創(chuàng)建對應的 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);
}];
}
代理實現(xiàn)
在代理方法didReceiveIncomingPushWithPayload里可以實現(xiàn)你要在30秒內完成的任務(注意該任務可以在點擊你本地通知喚醒后繼續(xù)執(zhí)行咪鲜,類似微信視頻呼叫)
#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);
}
結語
近期實現(xiàn)VoIP功能做一些記錄展蒂,希望能幫到大家又活,有問題可以積極交流,謝謝锰悼!