在更新了iOS10之后發(fā)現(xiàn)在通知部分出現(xiàn)了一些問題,原先的處理是:鎖屏狀態(tài)下接收到評論的推送通知之后滑動打開應用可以進入該評論對應的帖子內容。但在iOS10之下再做這個操作就只能進入應用,而無法定位到特定的某個頁面。經過調查后發(fā)現(xiàn)是iOS10在更新推送通知后的適配問題,iOS10開始增加了UNUserNotificationCenter,并且推送通知的處理要在代理方法userNotificationCenter: didReceiveNotificationResponse(后臺)和userNotificationCenter: willPresentNotification(前臺)中進行處理。下面總結一下處理方法:1,導入頭文件#import112,定義一下判斷系統(tǒng)版本的宏#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,實現(xiàn)接收推送消息的回調方法,iOS10之前使用application: didReceiveRemoteNotification 來進行回調處理,而在iOS10里則要實現(xiàn)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狀態(tài)受到推送通知
// 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狀態(tài)受到推送通知
// TODO
}
completionHandler(UIBackgroundFetchResultNewData);
}
5,對于本地通知沒有什么變化依然會回調
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notific