iOS10關于通知的適配

在更新了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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容