iOS10適配遠程推送

iOS10正式版發佈以後,網上各類適配XCode8以及iOS10的文章滿天飛。但對於iOS10適配遠程推送的文章卻很少。iOS10對於推送的修改仍是很是大的,新增了UserNotifications Framework,今天就結合本身的項目,說一說實際適配的狀況。html

 

1、Capabilities中打開Push Notifications 開關app

在XCode7中這裏的開關不打卡,推送也是能夠正常使用的,可是在XCode8中,這裏的開關必需要打開,否則會報錯:ui

Error Domain=NSCocoaErrorDomain Code=3000 "未找到應用程序的「aps-environment」的受權字符串" UserInfo={NSLocalizedDescription=未找到應用程序的「aps-environment」的受權字符串}代理

打開後會生成entitlements文件,在這裏能夠設置APS Environment,asp-environment的值爲development或者production,表明開發環境或者生產環境。參考官方文檔:https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/EntitlementKeyReference/Chapters/EnablingLocalAndPushNotifications.html#//apple_ref/doc/uid/TP40011195-CH3-SW2htm

 

2、推送的註冊ip

首先引入UserNotifications Framework,開發

 

  1. import <UserNotifications/UserNotifications.h>

iOS10修改了註冊推送的方法,這裏咱們又要對不一樣版本分別進行設置了。在application didFinishLaunchingWithOptions方法中修改之前的推送設置(我只實現了iOS8以上的設置)文檔

 

  1. if (IOS_VERSION >= 10.0) {
  2.         UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  3.         center.delegate = self;
  4.         [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  5.             if (!error) {
  6.                 DLog(@"request authorization succeeded!");
  7.             }
  8.         }];
  9.     } else {
  10.         if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
  11.             //IOS8,建立UIUserNotificationSettings,並設置消息的顯示類類型
  12.             UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];
  13.  
  14.             [application registerUserNotificationSettings:notiSettings];
  15.         }
  16.     }

 

3、UNUserNotificationCenterDelegate代理實現字符串

在iOS10中處理推送消息須要實現UNUserNotificationCenterDelegate的兩個方法:get

 

  1. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
  2.  
  3.  
  4. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler 

其中第一個方法爲App在前臺的時候收到推送執行的回調方法,第二個爲App在後臺的時候,點擊推送信息,進入App後執行的 回調方法。

之前處理推送,信息是在userInfo參數中,而新方法中代表上看並無這個參數,其實咱們同樣能夠獲取到userInfo,以下:

 

  1. /// App在前臺時候回調
  2. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  3.     NSDictionary *userInfo = notification.request.content.userInfo;
  4.     [self handleRemoteNotificationForcegroundWithUserInfo:userInfo];
  5. }
  6.  
  7. /// App在後臺時候點擊推送調用
  8. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  9.     NSDictionary *userInfo = response.notification.request.content.userInfo;
  10.     [self handleRemoteNotificationBackgroundWithUserInfo:userInfo];
  11. }

完成上面三個步驟的設置,對於iOS10的推送設置基本就適配了。要想自定義Notification Content或者實現其餘NotificationAction請參考其餘文章。這裏只是作了對iOS10的適配。

相關文章
相關標籤/搜索