適配 iOS 10,極光推送用戶要做這 6 處更改

Change 1:升級(jí)至 Xcode 8

建議盡快升級(jí)。使用 iOS 10 SDK 需要 Xcode 8 的支持。iOS 10 推出兩周內(nèi),安裝率就已經(jīng)達(dá)到 48.16%,不升級(jí) Xcode 8 并適配 iOS 10 意味著你現(xiàn)在可能已經(jīng)損失了 50% 的高端客戶,而且在未來的幾個(gè)月內(nèi)或許會(huì)陸續(xù)損失 90% 以上的客戶。

Change 2:Xcode 8 推送基本配置

  1. 首先跟以前版本的 Xcode 沒什么區(qū)別。下載自己在 Apple Developer 官網(wǎng)申請(qǐng)好的證書、描述文件(iOS 證書 設(shè)置指南
    )。填寫 Bundle Identifier、選擇開發(fā)者,正確配置后,這里不會(huì)有任何異常警告:
  2. Target - your target - Capabilities - 開啟 Push Notifications
    證書如果配置正確,這里會(huì)自動(dòng)打勾。系統(tǒng)會(huì)在工程目錄里生成一個(gè) projectName.entitlements 文件,請(qǐng)不要隨意刪除、修改:
  3. Target - your target - Capabilities - 開啟 Background Modes - 勾選最后一項(xiàng) Remote Notifications(這是 iOS 7 以后支持的 App 在后臺(tái)收到推送時(shí)能夠讓開發(fā)者執(zhí)行一段代碼的功能,建議開啟 [iOS 7 Background Remote Notification]、[iOS 推送全解析 - Tip5:后臺(tái)推送/靜默推送]

Change 3:更新 JPush iOS SDK >= v2.1.9

  1. 資源下載
  2. 替換工程中原有的 JPush SDK 文件為 JPUSHService.h、jpush-ios-2.1.9.a。
  3. Target - your target - Build Phases - Link Binary With Libraries - 引入一個(gè)新的庫 UserNotifications.framework

Change 4:更改注冊(cè)推送代碼

原先向系統(tǒng)注冊(cè)并請(qǐng)求推送權(quán)限的代碼是醬紫的,根據(jù) iOS 8 以后及以前分兩步:

if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定義categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    }
else {
        //categories 必須為nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
}

現(xiàn)在在前面加了一段 iOS 10 的注冊(cè)方法:

//Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    }
else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定義categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
} else {
        //categories 必須為nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
}

Change 5:實(shí)現(xiàn)代理 <JPUSHRegisterDelegate> 方法

在 Change 4 中的該行代碼處會(huì)報(bào)警告:

[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

其中的 self 類必須實(shí)現(xiàn) <JPUSHRegisterDelegate>,其是對(duì) iOS 10 接收推送并處理的代理 <UNUserNotificationCenterDelegate> 的封裝。

實(shí)現(xiàn) <JPUSHRegisterDelegate> 的兩個(gè)方法:

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  // Required
  NSDictionary * userInfo = notification.request.content.userInfo;
  if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler(UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設(shè)置
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  // Required
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler();  // 系統(tǒng)要求執(zhí)行這個(gè)方法
}

其中:

  • willPresentNotification 在展示推送之前觸發(fā),可以在此替換推送內(nèi)容,更改展示效果:內(nèi)容、聲音、角標(biāo)。
  • didReceiveNotificationResponse 在收到推送后觸發(fā),你原先寫在 didReceiveRemoteNotification 方法里接收推送并處理相關(guān)邏輯的代碼,現(xiàn)在需要在這個(gè)方法里也寫一份:
  • App 處于后臺(tái)收到推送觸發(fā)
  • 點(diǎn)擊推送條目或橫幅后,App 進(jìn)入前臺(tái)或 App 啟動(dòng)觸發(fā)
  • App 處于前臺(tái)時(shí)觸發(fā)

Change 6:Notification Service Extension & Notification Content

這兩個(gè) iOS 10 的新特性,暫未包含在 JPush SDK 中,需要用戶手動(dòng)創(chuàng)建相應(yīng)的 Target 并實(shí)現(xiàn)。

Notification Service Extension

主要負(fù)責(zé)修改推送內(nèi)容、增加圖片、gif、audio、video 展示。
收到推送小圖 - 下拉 - 展示大圖


Notification Content

用于完全自定義推送展示 UI,響應(yīng)用戶操作事件,并即時(shí)更新推送展示 UI。
注意下圖中點(diǎn)擊 Accept 后,推送 UI 里日程表 UI 發(fā)生了刷新。


關(guān)于 Change 6 的詳細(xì)教程可參照

玩轉(zhuǎn) iOS 10 推送 —— UserNotifications Framework(下)

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容