Change 1:升級至 Xcode 8
建議盡快升級。使用 iOS 10 SDK 需要 Xcode 8 的支持疟羹。iOS 10 推出兩周內主守,安裝率就已經達到 48.16%,不升級 Xcode 8 并適配 iOS 10 意味著你現在可能已經損失了 50% 的高端客戶榄融,而且在未來的幾個月內或許會陸續(xù)損失 90% 以上的客戶参淫。
Change 2:Xcode 8 推送基本配置
- 首先跟以前版本的 Xcode 沒什么區(qū)別。下載自己在 Apple Developer 官網申請好的證書愧杯、描述文件(iOS 證書 設置指南
)涎才。填寫 Bundle Identifier、選擇開發(fā)者力九,正確配置后耍铜,這里不會有任何異常警告:
- Target - your target - Capabilities - 開啟 Push Notifications
證書如果配置正確,這里會自動打勾跌前。系統(tǒng)會在工程目錄里生成一個projectName.entitlements
文件棕兼,請不要隨意刪除、修改:
- Target - your target - Capabilities - 開啟 Background Modes - 勾選最后一項 Remote Notifications(這是 iOS 7 以后支持的 App 在后臺收到推送時能夠讓開發(fā)者執(zhí)行一段代碼的功能抵乓,建議開啟 [iOS 7 Background Remote Notification]伴挚、[iOS 推送全解析 - Tip5:后臺推送/靜默推送])
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 - 引入一個新的庫
UserNotifications.framework
Change 4:更改注冊推送代碼
原先向系統(tǒng)注冊并請求推送權限的代碼是醬紫的茎芋,根據 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];
}
現在在前面加了一段 iOS 10 的注冊方法:
//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:實現代理 <JPUSHRegisterDelegate> 方法
在 Change 4 中的該行代碼處會報警告:
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
其中的 self 類必須實現 <JPUSHRegisterDelegate>,其是對 iOS 10 接收推送并處理的代理 <UNUserNotificationCenterDelegate> 的封裝咆贬。
實現 <JPUSHRegisterDelegate> 的兩個方法:
- (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í)行這個方法败徊,選擇是否提醒用戶,有Badge掏缎、Sound皱蹦、Alert三種類型可以選擇設置
}
- (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í)行這個方法
}
其中:
-
willPresentNotification
在展示推送之前觸發(fā)煤杀,可以在此替換推送內容,更改展示效果:內容沪哺、聲音沈自、角標。 -
didReceiveNotificationResponse
在收到推送后觸發(fā)辜妓,你原先寫在didReceiveRemoteNotification
方法里接收推送并處理相關邏輯的代碼枯途,現在需要在這個方法里也寫一份: - App 處于后臺收到推送觸發(fā)
- 點擊推送條目或橫幅后,App 進入前臺或 App 啟動觸發(fā)
- App 處于前臺時觸發(fā)
Change 6:Notification Service Extension & Notification Content
這兩個 iOS 10 的新特性籍滴,暫未包含在 JPush SDK 中酪夷,需要用戶手動創(chuàng)建相應的 Target 并實現。
Notification Service Extension
主要負責修改推送內容孽惰、增加圖片晚岭、gif、audio勋功、video 展示坦报。
收到推送小圖 - 下拉 - 展示大圖
Notification Content
用于完全自定義推送展示 UI,響應用戶操作事件狂鞋,并即時更新推送展示 UI片择。
注意下圖中點擊 Accept 后,推送 UI 里日程表 UI 發(fā)生了刷新骚揍。