iOS10下的推送增加了一些新的东西,废话不多说直接上代码
1.首先肯定是要注册通知,由于各个系统的注册通知方法有不同,所以注册也是要多做些判断,兼容更多系统
/** 注册远程通知 */
- (void)registerRemoteNotification {
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 10.0) {
#if __ipHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8编译会调用
UNUserNotificationCenter *center = [UNUserNotificationCentercurrentNotificationCenter];
center.delegate =self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge |UNAuthorizationOptionSound |UNAuthorizationOptionAlert |UNAuthorizationOptionCarPlay)completionHandler:^(BOOL granted,NSError *_Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
[[UIapplicationsharedApplication] registerForRemoteNotifications];
#else // Xcode 7编译会调用
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
} elseif ([[[UIDevicecurrentDevice] systemVersion]floatValue] >= 8.0) {
UIUserNotificationType types = (UIUserNotificationTypeAlert |UIUserNotificationTypeSound |UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:typescategories:nil];
[[UIApplicationsharedApplication] registerUserNotificationSettings:settings];
[[UIApplicationsharedApplication] registerForRemoteNotifications];
} else {
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound |UIRemoteNotificationTypeBadge);
[[UIApplicationsharedApplication] registerForRemoteNotificationTypes:apn_type];
}
}
注意!!!!!:xcode8需要打开通知开关
按照上图所示
2.上一步是注册通知,下面就是接受注册成功的deviceToken了,这里的deviceToken是NSData,所以要拿到字符串要做一些处理
// 注册成功的代理方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token = [[deviceTokendescription] stringByTrimmingCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" "withString:@""];
NSLog(@"/nDeviceToken:%@/n/n", token);
}
// 注册失败的代理方法- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"/n>>>[DeviceToken Error]:%@/n/n", error.description);
}
3.注册是搞定了,下面就要处理通知了。在处理通知方面,iOS10又增加了新的方法,不过老方法还是会调用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// [ GTSdk ]:将收到的APNs信息传给个推统计
[GeTuiSdkhandleRemoteNotification:userInfo];
//应用的当前状态判断
if(application.applicationState ==UIApplicationStateInactive){
//当你的应用在后台时,当你点击通知栏消息时,会进入此条件
}
// 控制台打印接收APNs信息
HWLog(@"/n>>>[Receive RemoteNotification:%@/n/n", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
注意:这个方法里最好加上应用状态的判断,这个枚举值有三个
typedefNS_ENUM(NSInteger, UIApplicationState) {
UIApplicationStateActive, //应用激活状态
UIApplicationStateInactive,//应用待激活状态或将要激活
UIApplicationStateBackground//应用在后台
} NS_ENUM_AVAILABLE_IOS(4_0);
下面是iOS10新增加的两个方法(这两个方法就够用)#PRagma mark - iOS 10中收到推送消息
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// iOS 10: App在前台获取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
HWLog(@"willPresentNotification:%@", notification.request.content.userInfo);
// 根据APP需要,判断是否要提示用户Badge、Sound、Alert
completionHandler(UNNotificationPresentationOptionBadge |UNNotificationPresentationOptionSound |UNNotificationPresentationOptionAlert);
}
// iOS 10: 点击通知进入App时触发
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
HWLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);
completionHandler();
}
#endif
其中,在第二个方法里做事就可以了,这个方法可以拿到APNS的数据,只是自己做好了状态判断,这些方法都是需要点击通知栏进入app才会调用新闻热点
疑难解答