使用JPush(极光推送)实现远程通知
使用JPush(极光推送)实现远程通知
远程推送是APP 必备的功能, 现在第三方的 SDK 已经做的非常完备了, 在 iOS10.0出来之后, 极光推送也及时更新了他的 SDK, 今天小试了一下效果, 发现坑还是很多的
大致的思路总结一下是这样的

下面就来一步步梳理一下:
- 一 , 创建应用程序 ID
1.首先登录苹果开发者网站https://developer.apple.com, 点击 Account(如果你连苹果开发者账号都没有注册,那么需要赶紧注册一个)

2.点击证书选项, 红色框框都可以点

3.添加一个 APP ID

4.创建 APP ID
APP ID 实际上由两部分组成, 即前缀和后缀, 前缀是默认的, 后缀就是你工程的 Bundle ID, APP ID Description就填你工程的名称即可

勾上推送通知选项

5.点击注册完成创建


- 二 , 配置和下载证书
1.生成证书
生成证书
然后一路 continue, 这时候你会看到一个上传请求
2.此时需要生成一个请求


3.只需要填写用户电子邮件地址, 我填的是我 apple id 的注册的邮箱地址, 以及常用名称两项即可

将证书请求文件存放到磁盘选择桌面,就会生成这么一个玩意儿

4.然后回到要上传的地方, 将刚刚生成的证书文件上传

5.下载证书, 双击打开

打开后会看到这么一个页面
- 三 , 导出 p.12文件
导出p12文件
导出 p.12文件,放到桌面上, 又可以看到这么一个玩意儿

- 四 , 接下来就要生成 profile 文件了




接下来有一个设备需要注册,这时候需要填写你即将真机测试的 UDID
连接好你的 iPhone 到电脑, 打开 iTunes, 然后就可以查看了



生成 Profile 文件时, 要填写 ProfileName, 我填写的是 Bundle ID - dev


- 五 , 创建极光推送开发者账号
1.然后在开发列表中点击创建应用

2.需要填写的是应用名称, 选取p.12文件, 填写开发证书密码


- 六 , 完成之后就可以看到这个界面, 此时, 我们就可以开始配置 XCode环境了

1.下载极光推送的 SDK

2.解压后, 将 Lib 文件拖到工程里去,包含一个静态库和.h 文件

3.然后就是添加各种库
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
Xcode7需要的是libz.tbd;Xcode7以下版本是libz.dylib
Adsupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)
UserNotifications.framework(Xcode8及以上)
libresolv.tbd (JPush 2.2.0及以上版本需要)
添加库完成后, 打印了这样一句话:

解决方案是这样的:

4.infoPlist文件的配置
ATS要配置
另外就是 Bundle Identifier 要修改

5.添加 profile 文件到 Xcode 中,需要将自动改为手动

6.修改 Code Signing

- 七 , 都配置好了以后, 就是上代码的时刻了
AppDelegate.m
#import "AppDelegate.h"
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的头文件(可选)
#import <AdSupport/AdSupport.h>
static NSString *const jPushTestDemoAppID = @"1b7cfe165ad7cb7c0e546405";
static NSString *const publishChannel = @"APP Store";
@interface AppDelegate ()<JPUSHRegisterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注册apns通知
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10
{
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge | UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
}
else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) // iOS8, iOS9
{
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
}
else // iOS7
{
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil];
}
/*
* launchingOption 启动参数.
* appKey 一个JPush 应用必须的,唯一的标识.
* channel 发布渠道. 可选.
* isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES.
* advertisingIdentifier 广告标识符(IDFA) 如果不需要使用IDFA,传nil.
* 此接口必须在 App 启动时调用, 否则 JPush SDK 将无法正常工作.
*/
// 广告标识符
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
// 如不需要使用IDFA,advertisingIdentifier 可为nil
// 注册极光推送
[JPUSHService setupWithOption:launchOptions appKey:jPushTestDemoAppID channel:publishChannel apsForProduction:0 advertisingIdentifier:advertisingId];
//2.1.9版本新增获取registration id block接口。
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0)
{
// iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送
NSLog(@"registrationID获取成功:%@",registrationID);
[[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
NSLog(@"registrationID获取失败,code:%d",resCode);
}
}];
return YES;
}
#pragma mark *** 注册APNs成功并上报DeviceToken ***
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
#pragma mark *** 实现注册APNs失败接口(可选) ***
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#pragma mark *** 添加处理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(); // 系统要求执行这个方法
}
- (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 iOS6
[JPUSHService handleRemoteNotification:userInfo];
}
以上代码都有注释, 很多也是从官方网站上 copy 过来的, 这里就不再赘述了
1.运行的时候会出现

2.点击允许后, 这个时候会在控制台打印 registrationID

是通过这个方法中的 block 回调来获取registrationID的
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0)
{
// iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送
NSLog(@"registrationID获取成功:%@",registrationID);
[[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
NSLog(@"registrationID获取失败,code:%d",resCode);
}
}];
3.在极光推送的后台输入你想要推送的内容

4.把第二步获取得registrationID填到这里

5.最后点击推送即可,出现的效果就是这样滴
