iOS-VoIP Push Notification

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

普通推送

远程推送和本地推送

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

VoIP Push Notification

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

证书设置上的区别

iOS-VoIP Push Notification_第1张图片

Xcode 上的设置

勾选功能

iOS-VoIP Push Notification_第2张图片

引入开发包

iOS-VoIP Push Notification_第3张图片

注册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]registerUserNotificationSettings: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

你可能感兴趣的:(Objective-C)