ios voip 推送-倾城孤月

  1. 什么是voip推送和普通的ios推送有什么区别呢?
    普通推送分为:远程推送和本地推送,区别网上资料太多了,简单说一下,好比网易新闻,有什么大的新闻会在手机端接收到推送,这个就是远程推送,是把相关信息推送到苹果推送服务器-APNS,太简单了,不说了!

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

曾经有人说普通推送能够作到以上功能,我能够明确的给出回复:普通推送是没法作到的,必须用voip push才能作到。xcode

注意事项:
一、证书的制做过程当中也须要选中voip认证。服务器

voip_cer.jpg

二、在xcode开发中也须要进行相关的设置,设置以下:app

setting_xcode.png

三、引入开发包ui

 


push_kit.png

xcode端代码以下:spa

一、首先注册为voip推送:code

-(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]registerUserNotificationSettings:notificationSettings]; } 

在应用初始化方法中调用:orm

[self registerVoipNotifications]; 

二、获取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,就取消命令blog

- (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]; } } 

四、注意当进入应用时,角标清除:

- (void)applicationDidBecomeActive:(UIApplication *)application { [UIApplication sharedApplication].applicationIconBadgeNumber = 0; } 
相关文章
相关标签/搜索