iOS 10 极光推送的一些整理

810 阅读6分钟

###首先用cocoapods导入 pod 'JPush' ###然后在AppDelegate引用 // 引入JPush功能所需头文件 #import "JPUSHService.h" // iOS10注册APNs所需头文件 #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif // 如果需要使用idfa功能所需要引入的头文件(可选) #import <AdSupport/AdSupport.h>

###设置代理<JPUSHRegisterDelegate>

###然后注册JPush - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//程序杀死状态下从外部进入app 获取到远程推送消息 跳转指定页面的做法 if (launchOptions) { // 获取推送通知定义的userinfo NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotification) { roomid = remoteNotification[@"roomId"]; nickName = remoteNotification[@"senderNickName"]; [self setupMainViewController]; }else{ [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; } //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式 JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; entity.types=JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // 可以添加自定义categories // NSSet<UNNotificationCategory *> *categories for iOS10 or later // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9 } [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; // Optional // 获取IDFA // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值 NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; // Required // init Push // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。 [JPUSHService setupWithOption:launchOptions appKey:@"appKey" channel:@"AppStore" apsForProduction:0 advertisingIdentifier:advertisingId]; return YES; }

###pragma mark - iOS10 收到通知(本地和远端) UNUserNotificationCenterDelegate //App处于前台接收通知时 只会是app处于前台状态 前台状态 and 前台状态下才会走,后台模式下是不会走这里的 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ //收到推送的请求 UNNotificationRequest *request = notification.request; //收到推送的内容 UNNotificationContent *content = request.content; //收到用户的基本信息 NSDictionary *userInfo = content.userInfo; //收到推送消息的角标 NSNumber *badge = content.badge; //收到推送消息body NSString *body = content.body; //推送消息的声音 UNNotificationSound *sound = content.sound; // 推送消息的副标题 NSString *subtitle = content.subtitle; // 推送消息的标题 NSString *title = content.title; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { //此处省略一万行需求代码。。。。。。 // NSLog(@"iOS10 收到远程通知:%@",userInfo); }else { // 判断为本地通知 //此处省略一万行需求代码。。。。。。 NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); }

// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置 completionHandler(UNNotificationPresentationOptionBadge| UNNotificationPresentationOptionSound| UNNotificationPresentationOptionAlert); }

####App通知的点击事件 下面这个代理方法, - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ //收到推送的请求 UNNotificationRequest *request = response.notification.request; //收到推送的内容 UNNotificationContent *content = request.content; //收到用户的基本信息 NSDictionary *userInfo = content.userInfo; //收到推送消息的角标 NSNumber *badge = content.badge; //收到推送消息body NSString *body = content.body; //推送消息的声音 UNNotificationSound *sound = content.sound; // 推送消息的副标题 NSString *subtitle = content.subtitle; // 推送消息的标题 NSString *title = content.title; if([response.notification.request.trigger isKindOfClass: [UNPushNotificationTrigger class]]) { NSLog(@"iOS10 收到远程通知:%@",userInfo); //此处省略一万行需求代码。。。。。。 }else { // 判断为本地通知 //此处省略一万行需求代码。。。。。。 NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); } completionHandler(); // 系统要求执行这个方法 }

####pragma mark -iOS 10之前收到通知 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"iOS6及以下系统,收到通知:%@", userInfo); [JPUSHService handleRemoteNotification:userInfo]; //此处省略一万行需求代码。。。。。。 }

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"iOS7及以上系统,收到通知:%@", userInfo); [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); //此处省略一万行需求代码。。。。。。
}

######appKey 在极光获取 ######channel 指明应用程序包的下载渠道,为方便分渠道统计,具体值由你自行定义 ######apsForProduction 0 (默认值)表示采用的是开发证书,1 表示采用生产证书发布应用 ######advertisingIdentifier 广告标识符吧好像 记不太清了 用处不大

###注册APNs成功并上报DeviceToken - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Required - 注册 DeviceToken [JPUSHService registerDeviceToken:deviceToken]; //这里应该将获取的registrationID 和服务器存储的用户绑定 来实现定点推送 [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) { NSLog(@"%@", registrationID); }]; }

###实现注册APNs失败接口(可选) - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); } ###添加处理APNs通知回调方法 #pragma mark- JPUSHRegisterDelegate // iOS 10 Support - (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三种类型可以选择设置 }

// iOS 10 Support - (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(); // 系统要求执行这个方法 }

#pragma mark -iOS 10之前收到通知 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"iOS6及以下系统,收到通知:%@", userInfo); [JPUSHService handleRemoteNotification:userInfo]; //此处省略一万行需求代码。。。。。。 }

###注册

###成功运行

#####真机调试该项目,如果控制台输出以下日志则代表您已经集成成功。

2016-08-19 17:12:12.745823 219b28[1443:286814] | JPUSH | I - [JPUSHLogin] ----- login result -----
uid:5460310207
registrationID:171976fa8a8620a14a4

###配置证书

Snip20170923_1.png

创建 App ID,填写 App ID 的 NAME 和 Bundle ID(如果 ID 已经存在可以直接跳过此步骤)。

Snip20170923_2.png

Snip20170923_3.png

为 App 开启 Push Notification 功能。如果是已经创建的 App ID 也可以通过设置开启 Push Notification 功能。

Snip20170923_6.png

Snip20170923_7.png

如果你之前没有创建过 Push 证书或者是要重新创建一个新的,请在证书列表下面新建。

Snip20170923_9.png

新建证书需要注意选择 APNs 证书种类。如图 APNs 证书有开发(Development)和生产(Production)两种。

注:开发证书用于开发调试使用;生产证书既能用于开发调试,也可用于产品发布。此处我们选择生产证书为例。

Snip20170923_10.png

点击 "Continue", 之后选择该证书准备绑定的 AppID。

Snip20170923_11.png

击 “Continue”,会进入 CSR 说明界面。 再点 “Continue” 会让你上传 CSR 文件。( CSR 文件会在下一步创建)

Snip20170923_12.png

打开系统自带的 KeychainAccess 创建 Certificate Signing Request。如下图操作:

Snip20170923_14.png

填写“用户邮箱”和“常用名称” ,并选择“存储到磁盘”,证书文件后缀为 .certSigningRequest 。

Snip20170923_15.png

回到浏览器中 CSR 上传页面,上传刚刚生成的后缀为 .certSigningRequest 的文件。 生成证书成功后,点击 “Download” 按钮把证书下载下来,是后缀为 .cer 的文件。

Snip20170923_16.png

双击证书后,会在“KeychainAccess”中打开,选择左侧“钥匙串”列表中“登录”,以及“种类”列表中“我的证书”,找到刚才下载的证书,并导出为 .p12 文件。如下图:

Snip20170923_17.png

Snip20170923_18.png

在极光控制台上,进入你应用的应用设置中 iOS 的鉴权方式选择 “证书”,上传刚才导出的 .p12 证书。极光会在后台为你的应用进行鉴权。

Snip20170923_19.png

###注意事项 #####开发环境测试 在对 JPush iOS 开发环境进行测试前,请确保 3 个统一: App 是开发环境打包(开发证书 Development) 上传了开发证书并验证通过 ####发布环境测试 在对 JPush iOS 生产环境进行测试前,请确保 3 个统一: App 是 ad-hoc 打包或者App Store 版本(发布证书 Production) 上传了发布证书并验证通过 ####可能存在的其他问题 收到消息不够稳定 JPush iOS 是对原生官方 APNs 推送的一个补充,是对其的封装,以帮助开发人员更轻松地使用 APNs 。 由于APNs 本身不承诺保证消息到达,客户端网络与服务器端的连通性,对 APNs 是否及时接收到消息具有很大的影响。