在更新了iOS10之后发现在通知部分出现了一些问题,原先的处理是:锁屏状态下接收到评论的推送通知之后滑动打开应用可以进入该评论对应的帖子内容。但在iOS10之下再做这个操作就只能进入应用,而无法定位到特定的某个页面。经过调查后发现是iOS10在更新推送通知后的适配问题,iOS10开始增加了UNUserNotificationCenter,并且推送通知的处理要在代理方法userNotificationCenter: didReceiveNotificationResponse(后台)和userNotificationCenter: willPresentNotification(前台)中进行处理。下面总结一下处理方法:1,导入头文件#import112,定义一下判断系统版本的宏#define IS_IOS7_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)#define IS_IOS8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#define IS_IOS10_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0)1231233,遵守UNUserNotificationCenterDelegate代理appDelegate.m:@interface AppDelegate ()@end
并且在application: didFinishLaunchingWithOptions方法中注册一下推送通知。
if (IS_IOS10_OR_LATER) { //IOS10 之后采用UNUserNotificationCenter注册通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
NSLog(@"succeeded!");
}
}];
}else{ //IOS10 之前注册通知
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert)
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
4,实现接收推送消息的回调方法,iOS10之前使用application: didReceiveRemoteNotification 来进行回调处理,而在iOS10里则要实现UNUserNotificationCenterDelegate的两个代理方法:
userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification和
userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse。
#pragma mark - 推送消息接收
//IOS10之前 推送消息接收
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
Log(@"userInfo: %@", userInfo.description);
if ( application.applicationState == UIApplicationStateActive) {// 程序在运行过程中受到推送通知
// TODO
} else { //在background状态受到推送通知
// TODO
}
completionHandler(UIBackgroundFetchResultNewData);
}
//IOS10之后 推送消息接收
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary *userInfo = response.notification.request.content.userInfo;
if ( [UIApplication sharedApplication].applicationState == UIApplicationStateActive) {// 程序在运行过程中受到推送通知
// TODO
} else { //在background状态受到推送通知
// TODO
}
completionHandler(UIBackgroundFetchResultNewData);
}
5,对于本地通知没有什么变化依然会回调
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notific