iOS Voip push部分笔记

Voip push与普通push区分开的部分

引入PushKit头文件 

#import

遵守协议

声明属性

@property(nonatomic,strong)PKPushRegistry *voipRegistry;

AppDelegate中:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

[self registerVoipNotifications];

returnYES;

}

注册Voip push

- (void)registerVoipNotifications{

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

voipRegistry.delegate=self;

voipRegistry.desiredPushTypes= [NSSetsetWithObject:PKPushTypeVoIP];

UIUserNotificationType types = (UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert);

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

[[UIApplication sharedApplication]registerUserNotificationSettings:notificationSettings];

}

#pragma mark - PKPushRegistryDelegate

收到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);

[YFRInfoToast showInfo:_tokenStr bgColor:[UIColor blackColor].CGColor inView:self.window vertical:0.7];//弹toast

}

收到Voip消息回调

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

//此时进行voip注册

// write your voip related codes here

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

if(theType ==UIUserNotificationTypeNone){

UIUserNotificationSettings*userNotifySetting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];

[[UIApplication sharedApplication]registerUserNotificationSettings:userNotifySetting];

}

UILocalNotification *backgroudMsg = [[UILocalNotification alloc]init];

backgroudMsg.alertBody=@"You receive a new call";

[[UIApplication sharedApplication]presentLocalNotificationNow:backgroudMsg];

}

你可能感兴趣的:(iOS Voip push部分笔记)