适配 iOS 10,极光推送用户要作这 6 处更改

Change 1:升级至 Xcode 8

建议尽快升级。使用 iOS 10 SDK 须要 Xcode 8 的支持。iOS 10 推出两周内,安装率就已经达到 48.16%,不升级 Xcode 8 并适配 iOS 10 意味着你如今可能已经损失了 50% 的高端客户,并且在将来的几个月内或许会陆续损失 90% 以上的客户。ios

Change 2:Xcode 8 推送基本配置

  1. 首先跟之前版本的 Xcode 没什么区别。下载本身在 Apple Developer 官网申请好的证书、描述文件(iOS 证书 设置指南 )。填写 Bundle Identifier、选择开发者,正确配置后,这里不会有任何异常警告:
  2. Target - your target - Capabilities - 开启 Push Notifications
    证书若是配置正确,这里会自动打勾。系统会在工程目录里生成一个 projectName.entitlements 文件,请不要随意删除、修改:
  3. Target - your target - Capabilities - 开启 Background Modes - 勾选最后一项 Remote Notifications(这是 iOS 7 之后支持的 App 在后台收到推送时可以让开发者执行一段代码的功能,建议开启 [iOS 7 Background Remote Notification][iOS 推送全解析 - Tip5:后台推送/静默推送]

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

  1. 资源下载
  2. 替换工程中原有的 JPush SDK 文件为 JPUSHService.hjpush-ios-2.1.9.a
  3. Target - your target - Build Phases - Link Binary With Libraries - 引入一个新的库 UserNotifications.framework

Change 4:更改注册推送代码

原先向系统注册并请求推送权限的代码是酱紫的,根据 iOS 8 之后及之前分两步:xcode

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 的注册方法:ide

//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:实现代理 方法

在 Change 4 中的该行代码处会报警告:ui

[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];复制代码

其中的 self 类必须实现 ,其是对 iOS 10 接收推送并处理的代理 的封装。 spa

实现 的两个方法: 3d

- (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); // 须要执行这个方法,选择是否提醒用户,有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();  // 系统要求执行这个方法
}复制代码

其中:代理

  • willPresentNotification 在展现推送以前触发,能够在此替换推送内容,更改展现效果:内容、声音、角标。
  • didReceiveNotificationResponse 在收到推送后触发,你原先写在 didReceiveRemoteNotification 方法里接收推送并处理相关逻辑的代码,如今须要在这个方法里也写一份:
    • App 处于后台收到推送触发
    • 点击推送条目或横幅后,App 进入前台或 App 启动触发
    • App 处于前台时触发

Change 6:Notification Service Extension & Notification Content

这两个 iOS 10 的新特性,暂未包含在 JPush SDK 中,须要用户手动建立相应的 Target 并实现。code

Notification Service Extension

主要负责修改推送内容、增长图片、gif、audio、video 展现。
收到推送小图 - 下拉 - 展现大图
cdn

Notification Content

用于彻底自定义推送展现 UI,响应用户操做事件,并即时更新推送展现 UI。
注意下图中点击 Accept 后,推送 UI 里日程表 UI 发生了刷新。
blog

关于 Change 6 的详细教程可参照

玩转 iOS 10 推送 —— UserNotifications Framework(下)

相关文章
相关标签/搜索