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 推送基本配置
- 首先跟以前版本的 Xcode 沒什么區(qū)別。下載自己在 Apple Developer 官網(wǎng)申請(qǐng)好的證書、描述文件(iOS 證書 設(shè)置指南
)。填寫 Bundle Identifier、選擇開發(fā)者,正確配置后,這里不會(huì)有任何異常警告:
- Target - your target - Capabilities - 開啟 Push Notifications
證書如果配置正確,這里會(huì)自動(dòng)打勾。系統(tǒng)會(huì)在工程目錄里生成一個(gè)projectName.entitlements文件,請(qǐng)不要隨意刪除、修改:
- 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
- 資源下載
- 替換工程中原有的 JPush SDK 文件為
JPUSHService.h、jpush-ios-2.1.9.a。 - 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ā)生了刷新。



