重點說下:voip的重點功能。
就是打視頻或者語音電話的時候推送功能,一個典型的應用場景,A用戶撥打B用戶視頻或者語音電話,A撥打,B鈴聲響起,此時A掛斷,B鈴聲消失,就至關於控制對方手機同樣。國外的語音視頻電話都是採用voip push,好比whats app,line等語音視頻通信軟件。ios
曾經有人說普通推送能夠作到以上功能,我能夠明確的給出回覆:普通推送是沒法作到的,必須用voip push才能作到。xcode
注意事項:
一、證書的製做過程當中也須要選中voip認證。服務器
二、在xcode開發中也須要進行相關的設置,設置以下:app
三、引入開發包ui
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; }