您的位置:首页 > 移动开发 > IOS开发

iOS-VoIP Push Notification

2017-12-23 11:48 218 查看
最近看了一篇文章,说的是微信实现收款到账语音提醒功能实现。原文章 文中提到了VoIP Push Notification,本文针对推送的种类以及如何利用推送唤醒 app 进行总结。

普通推送

远程推送和本地推送

区别网上资料太多了,简单说一下,比如今日头条,有什么大的新闻会在手机端接收到推送,这个就是远程推送,是把相关信息推送到苹果推送服务器-APNS。本地推送就是在本地设定一个时间,其实就是一个类似闹钟的功能,当到了设定的时间,就会收到一条推送,这个是和推送服务器没有关系的。

VoIP Push Notification

打视频或者语音电话的时候推送功能,一个典型的应用场景,A用户拨打B用户视频或者语音电话,A拨打,B铃声响起,此时A挂断,B铃声消失,就相当于控制对方手机一样。国外的语音视频电话都是采用voip push,比如whats app,line等语音视频通讯软件。

证书设置上的区别



Xcode 上的设置

勾选功能



引入开发包



注册VoIP 推送,该方法需要在初始化 app 的时候进行调用

-(void)registerVoipNotifications{

PKPushRegistry * voipRegistry = [[PKPushRegistry alloc]initWithQueue:dispatch_get_main_queue()];

voipRegistry.delegate = self;
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

UIUserNotificationType types = (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert);

UIUserNotificationSettings * notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication]registerUs
b992
erNotificationSettings:notificationSettings];
}


获取token,用于发送到服务器端,服务器端发送推送消息时,会发送这个token到APNS服务器,APNS通过这个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:@""];
NSLog(@"device_token is %@" , _tokenStr);
}


对推送过来的消息进行解析,可以在推送的时候设置标记,下面的代码是获取到标志以后,根据标志命令调用不同的方法进行处理,进而实现功能,注意这里接收到消息定义时,如果传过来的命令是call就创建一个本地推送,提示某人正在呼叫你,如果传过来的命令为cancel,就取消命令

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type{

UIUserNotificationType theType = [UIApplication sharedApplication].currentUserNotificationSettings.types;

if (theType == UIUserNotificationTypeNone)
{
UIUserNotificationSettings *userNotifySetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:userNotifySetting];
}

NSDictionary * dic = payload.dictionaryPayload;

NSLog(@"dic  %@",dic);

if ([dic[@"cmd"] isEqualToString:@"call"]) {
UILocalNotification *backgroudMsg = [[UILocalNotification alloc] init];
backgroudMsg.alertBody= @"You receive a new call";
backgroudMsg.soundName = @"ring.caf";
backgroudMsg.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:backgroudMsg];
}else if(([dic[@"cmd"] isEqualToString:@"cancel"]){

[[UIApplication sharedApplication] cancelAllLocalNotifications];

UILocalNotification * wei = [[UILocalNotification alloc] init];
wei.alertBody= [NSString stringWithFormat:@"%ld 个未接来电",[[UIApplication sharedApplication]applicationIconBadgeNumber]];
wei.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber];
[[UIApplication sharedApplication] presentLocalNotificationNow:wei];
}
}


参考1

参考2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: