IOS10 遠程推送適配
iOS10推送新增了UserNotifications Framework,使用起來其實很簡單。
建議看看極光推送的Demo,裡面寫的更詳細。
只是在iOS10以上系統上點擊通知欄,回調方法不再走原來的這兩個方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {} - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {}
而是在前台的時候回調
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^) (UNNotificationPresentationOptions))completionHandler
從後台進入的時候回調
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
直接說怎麼用吧:
1,導入頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif
2,注冊通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中
if (iOS10) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error) { NSLog(@"succeeded!"); } }]; } else if (iOS8_9){//iOS8-iOS9 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:settings]; [application registerForRemoteNotifications]; } else {//iOS8以下 [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; }
3,回調方法中,獲取通知數據
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; //消息處理 if([request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { //判斷為遠程推送 }else { // 判斷為本地通知 } }
4,對於本地通知沒有什麼變化依然會回調
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!