在激光推送推出新版SDK后,iOS适配iOS10的同时仍要兼容之前的版本,在这里我们要对iOS的消息进行一下处理。具体代码实现如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction];
//2.1.9版本新增获取registration id block接口。
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0){
NSLog(@"registrationID获取成功:%@",registrationID);
}
else{
NSLog(@"registrationID获取失败,code:%d",resCode);
}
}];
// App 是用户点击推送消息启动
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self performSelector:@selector(next) withObject:nil afterDelay:0.1];
}
}
-(void)next{
//iOS10以后的系统需要单独处理页面的跳转
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
NoticeListViewController *vc=[[NoticeListViewController alloc]init];
[[NSNotificationCenter defaultCenter]postNotificationName:@"tabbar" object:vc];
}
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS6及以下系统,收到通知:%@", [self logDic:userInfo]);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]);
if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 || application.applicationState>0) {
NSLog(@"ios7========");
// 收到通知点击通知内容的操作 前台收到通知
if (application.applicationState==0) {
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"您收到了一条消息" message:@"是否前去查看" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"返回", nil];
alertView.tag=36;
[alertView show];
}
if(application.applicationState == UIApplicationStateInactive)
{
// 收到通知点击通知内容的操作 应用进入后台或者关闭的时候
NoticeListViewController *vc=[[NoticeListViewController alloc]init];
[[NSNotificationCenter defaultCenter]postNotificationName:@"tabbar" object:vc];
}
}
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
// [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
[JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}
#pragma mark- JPUSHRegisterDelegate
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10---1 前台收到远程通知:%@", [self logDic:userInfo]);
UIAlertView *alertView =[[UIAlertView alloc]initWithTitle:@"您收到了一条消息" message:@"是否前去查看" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"返回", nil];
alertView.tag=36;
[alertView show];
}
else {
// 判断为本地通知
NSLog(@"iOS10----1 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10----2 收到远程通知:%@", [self logDic:userInfo]);
// 收到通知点击通知内容的操作
NoticeListViewController *vc=[[NoticeListViewController alloc]init];
[[NSNotificationCenter defaultCenter]postNotificationName:@"tabbar" object:vc];
}
else {
// 判断为本地通知
NSLog(@"iOS10----2 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(); // 系统要求执行这个方法
}
#endif