激光推送
第一步:先在开发者账号创建app id,即创建应用程序ID
- 登陆 苹果开发者网站 进入开发者账户。
2、从开发者账户页面左侧入口进入“Certificates, IDs & Profiles” 页面。
3、创建 App ID,填写 App ID 的 Name 和 Bundle ID( 注: 此处需要指定具体的 Bundle ID 不要使用通配符)。
4、为 App 开启 Push Notification 功能。如果是已经创建的 App ID 也可以通过设置开启 Push Notification 功能。
5、填写好以上属性后,点击 “Continue”,确认 AppId 属性的正确性,点击 “Register”,注册 AppId 成功。
第二步 配置证书,包括开发证书和生产证书两个证书
简述极光推送证书的基本生成流程,两者都为导出的p12文件
开发证书的详细步骤:
1、开发证书的生成(注意选择的Development)。
2、点击continue,来到选择App ID界面。
3、说明让上传自己的CSR文件,continue…
4、上传请求生成的CSR文件,continue…
5、打开系统自带的 “钥匙串访问” 创建 Certificate Signing Request。如下图操作:
6、填写“用户邮箱”和“常用名称” ,并选择“存储到磁盘”,证书文件后缀为.certSigningRequest 。
7、回到浏览器中 CSR 上传页面,上传刚刚生成的后缀为 .certSigningRequest 的文件。
8、生成证书成功后,点击 “Download” 按钮把证书下载下来,是后缀为 .cer 的文件(第一个为“Download”得到的生产开发证书,第二个为“Download”得到的开发证书)。
9、双击证书后,会在“KeychainAccess”中打开,选择左侧“钥匙串”列表中“登录”,以及“种类”列表中“我的证书”,找到刚才下载的证书,并导出为 .p12 文件。如下图:
生产证书的详细步骤:
1、注意这里的选择方式和开发证书有略微的区别,选择Production那一项就好
2、以下的步骤略去,和开发的一样,到最后下载一下文件,得到下面图的两个文件,双击在钥匙串访问中可见。
最后得到导出的证书(第二个)
第三步 把导出的开发证书和生产证书分别上传到极光平台
1、在极光控制台上,进入你应用的应用设置中 iOS 的鉴权方式选择 “证书”,上传刚才导出的 .p12 证书。极光会在后台为你的应用进行鉴权。
第四步 集成极光推送SDK,配置工程
1、导入SDK
选择1:Cocoapods导入
通过Cocoapods下载地址:
pod ‘JPush’
如果需要安装指定版本则使用以下方式(以3.0.2版本为例):
pod ‘JPush’, ‘3.0.2’
选择2:手动导入
- 在极光官网下载最新 SDK
- 将 SDK 包解压,在 Xcode 中选择 “Add files to ‘Your project name’…”,将解压后的 lib 子文件夹(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a )添加到你的工程目录中。
- 添加 Framework
- CFNetwork.framework
- CoreFoundation.framework
- CoreTelephony.framework
- SystemConfiguration.framework
- CoreGraphics.framework
- Foundation.framework
- UIKit.framework
- Security.framework
- libz.tbd(Xcode 7 以下版本是 libz.dylib)
- AdSupport.framework(获取 IDFA 需要;如果不使用 IDFA,请不要添加)
- UserNotifications.framework(Xcode 8 及以上)
- libresolv.tbd(JPush 2.2.0 及以上版本需要,Xcode 7 以下版本是 libresolv.dylib)
注意: 如果集成 JPush 3.0.1 及以上版本, 且同时集成极光其他 SDK(如:JMessage 3.0.0 及以上版本)
1. Cocoapods 导入,建议都更新为线上最新版本,来避免 JCore 版本不一致导致的冲突。
2. 手动导入,在工程中只需保留一个最新版本的 jcore-ios-x.x.x.a 静态库文件。
Build Settings
如果你的工程需要支持小于 7.0 的 iOS 系统,请到 Build Settings 关闭 bitCode 选项,否则将无法正常编译通过。
- 设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如 SDK 文件夹(默认为 lib )与工程文件在同一级目录下,则都设置为 “$(SRCROOT)/{静态库所在文件夹名称}” 即可。
Capabilities
如使用 Xcode 8 及以上环境开发,请开启 Application Target 的 Capabilities->Push Notifications 选项,如图:
允许 Xcode 7 支持 Http 传输方法
如果您使用的是 2.1.9 及以上的版本则不需要配置此步骤
如果用的是 Xcode 7 或更新版本,需要在 App 项目的 plist 手动配置下 key 和值以支持 http 传输:
选择1:根据域名配置
- 在项目的 info.plist 中添加一个 Key:NSAppTransportSecurity,类型为字典类型。
- 然后给它添加一个 NSExceptionDomains,类型为字典类型;
- 把需要的支持的域添加给 NSExceptionDomains。其中 jpush.cn 作为 Key,类型为字典类型。
- 每个域下面需要设置 2 个属性:NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads。 两个属性均为 Boolean 类型,值分别为 YES、YES。
如图:
第五步 工程代码部分
为 AppDelegate 添加 Delegate。
参考代码:
@interface AppDelegate ()<JPUSHRegisterDelegate> @end
在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中添加
// 1.注册系统通知 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; [application registerUserNotificationSettings:settings]; } // 2.注册极光推送 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]; } [JPUSHService setupWithOption:launchOptions appKey:JPushAPPkey channel:@"Publish channel" apsForProduction:NO advertisingIdentifier:nil]; // 接收应用内消息 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil]; // 极光推送登录成功后可以注册别名 [defaultCenter addObserver:self selector:@selector(registerAlias:) name:kJPFNetworkDidLoginNotification object:nil];
// 3.注册 DeviceToken - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [JPUSHService registerDeviceToken:deviceToken]; } #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三种类型可以选择设置 // 通知内容为:notification.request.content.body } // iOS 10 Support,用户点击了通知进入app - (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(); // 系统要求执行这个方法 [UIApplication sharedApplication].applicationIconBadgeNumber = 0; } #pragma mark 绑定tag - (void)registerAlias:(NSNotification *)notification{ // 注册别名 // [JPUSHService setAlias:[NSString stringWithFormat:@"%@",[RYSave account].user_id] callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self]; [JPUSHService setAlias:[NSString stringWithFormat:@"%@",[RYSave account].user_id] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) { DLog(@"极光别名注册的回调方法rescode: %ld, \n, \n alias: %@\n", (long)iResCode , iAlias); if (iResCode == 0) { // 注册成功 } } seq:1]; } // 极光别名注册的回调方法 -(void)tagsAliasCallback:(int)iResCode tags:(NSSet*)tags alias:(NSString*)alias { DLog(@"极光别名注册的回调方法rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, tags , alias); if (iResCode == 0) { // 注册成功 } } #pragma mark 解析极光推送的应用内消息 - (void)networkDidReceiveMessage:(NSNotification *)notification { NSDictionary * userInfo = [notification userInfo]; DLog(@"解析极光推送的应用内消息:%@",userInfo); } // 接收到远程通知之后 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // Required,For systems with less than or equal to iOS6 [JPUSHService handleRemoteNotification:userInfo]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // IOS 7 Support Required [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } //获取 deviceToken 失败后 远程推送(极光推送)打开失败 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //Optional DLog(@"获取 device token 失败 %@", error); }
绑定别名需要注意的是:在删除绑定的时候把用户唯一标示改为空
[JPUSHService setAlias:[NSString stringWithFormat:@"%@",<#用户唯一标示#> completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) { DLog(@"极光别名注册的回调方法rescode: %ld, \n, \n alias: %@\n", (long)iResCode , iAlias); if (iResCode == 0) { // 注册成功 } } seq:1];