iOS 篇 未完待续
导入框架
CocoaPods 导入
pod 'JPush', '3.1.0'
手动导入(比较麻烦, 不是很推荐)
在极光官网下载最新 SDK http://docs.jiguang.cn/jpush/resources/
将 SDK 包解压, 在 Xcode 中选择 "Add files to'Your project name'...", 将解压后的 lib 子文件夹 (包含 JPUSHService.h,jpush-iOS-x.x.x.a,jcore-iOS-x.x.x.a ) 添加到你的工程目录中.
3. 添加 Framework
- CFNetwork.framework
- CoreFoundation.framework
- CoreTelephony.framework
- SystemConfiguration.framework
- CoreGraphics.framework
- Foundation.framework
- UIKit.framework
- Security.framework
- libz.tbd
- UserNotifications.framework
- libresolv.tbd
注意: 如果集成 JPush 3.0.1 及以上版本, 且同时集成极光其他 SDK(如: JMessage 3.0.0 及以上版本)
1. CocoaPods 导入, 建议都更新为线上最新版本, 来避免 JCore 版本不一致导致的冲突.
2. 手动导入, 在工程中只需保留一个最新版本的 jcore-iOS-x.x.x.a 静态库文件.
开启 Target 的 Capabilities->Push Notifications 选项, 如图:
1.jpg
在 AppDelegate.m 里引入
- // 引入 JPush 功能所需头文件
- #import "JPUSHService.h"
- // iOS10 注册 APNs 所需头文件
- #ifdef NSFoundationVersionNumber_iOS_9_x_Max
- #import <UserNotifications/UserNotifications.h>
- #endif
初始化
请将以下代码添加到 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 里
JpushAppKey 是你申请的 key
这边是大家经常会犯错的地方, apsForProduction:NO 表示采用的是开发证书, YES 表示采用生产证书发布应用
- // 初始化 APNs
- JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
- entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
- [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
- // 初始化 JPush
- #ifdef DEBUG
- [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:NO];
- #else
- [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:YES];
- #endif
为 AppDelegate 添加 Delegate(JPUSHRegisterDelegate) 并添加回调方法
- @interface AppDelegate ()<JPUSHRegisterDelegate>
- - (void)application:(UIApplication *)application
- didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- // Required - 注册 DeviceToken
- [JPUSHService registerDeviceToken:deviceToken];
- }
- @end
添加处理 APNs 通知回调方法
- #pragma mark- JPUSHRegisterDelegate
- // iOS 12 Support
- - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
- if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
- // 从通知界面直接进入应用
- }else{
- // 从通知设置界面进入应用
- }
- }
- // 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(); // 系统要求执行这个方法
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- // Required, iOS 7 Support
- [JPUSHService handleRemoteNotification:userInfo];
- completionHandler(UIBackgroundFetchResultNewData);
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
- // Required, For systems with Less than or equal to iOS 6
- [JPUSHService handleRemoteNotification:userInfo];
- }
上传 registrationID 给服务器端(也可以设置别名, 设置别名也在这里写)
- [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
- ZPLog(@"------>registrationID: %@",[JPUSHService registrationID]);
- // 上传极光 registrationID 给服务器
- }];
公司推送后台
标签类型
项目
性别
年龄
用户类型
1.PNG
查看通知后的动作
默认打开 App
打开 App 里的某个模块
打开网页
2.PNG
思路: 本来是想 App 用户登录成功后, 后台返回改用户的标签数组给我, 然后我调用此 API 来设置标签. 但是发现这些标签中有个岁数, 假设用户登录后, 一直不退出, 那他的标签里的岁数一直就没变. 商量后, 由我 App 这边自己组装好和后台约定的用户的标签数组, 然后每次打开 App, 都设置一下标签
- /**
- 覆盖 tags
- 调用该接口会覆盖用户所有的 tags
- @param tags 需要设置的 tags 集合
- @param completion 响应回调
- @param seq 请求序列号
- */
- + (void)setTags:(NSSet<NSString *> *)tags
- completion:(JPUSHTagsOperationCompletion)completion
- seq:(NSInteger)seq;
常见收不到通知的原因
环境不对, 先检查客户端的方法
初始化代码中 apsForProduction:isProduction 的值需要与客户端的证书环境保持一致
后台 API 推送的话再去看后台的, 后台也有一个配置生产还是测试环境的属性.
前后端的 appkey 不一致
Android 篇
如果你找不到原因, 建议你可以加 qq 群 445928015, 极光有专业的客服工作日在线答疑. 我使用过好多三方服务, 第一次见不付费的非 VIP 用户也会被客服耐心解答的, 经常遇到相同的问题客服也是非常有耐心, 非常难得.
本文为极光征文参赛文章
来源: http://www.jianshu.com/p/4dc175e2a101