您的位置:首页 > Web前端 > React

极光推送Jpush-react-native在ios中的详细配置 本人亲测

2018-03-14 17:17 519 查看
IOS极光推送配置:

安装

npm install --save jpush-react-native
npm install --save jcore-react-native


1.自动配置部分(以下命令均在你的React Native Project目录下运行)

react-native link


根据提示填写appKey 填写包名

自动配置操作会自动插入Native代码 检查自动配置代码,在 Native 中需要添加的代码:

在 AppDelegate.m 文件中 填写如下代码,这里的的 appkey(极光官网上创建应用时自动生成)、channel(一般为cli)、和 isProduction (false为测试环境true为开发环境)填写自己的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService setupWithOption:launchOptions     appKey:appkey
channel:nil
apsForProduction:isProduction];
}


在 AppDelegate.m didRegisterForRemoteNotificationsWithDeviceToken 方法中添加 [JPUSHService registerDeviceToken:deviceToken]; 如下所示

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}


为了在收到推送点击进入应用能够获取该条推送内容需要在 AppDelegate.m didReceiveRemoteNotification 方法里面添加 [[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo] 方法,注意:这里需要在两个方法里面加一个是 iOS7 以前的一个是 iOS7 即以后的,如果 AppDelegate.m 没有这个两个方法则直接复制这两个方法,在 iOS10 的设备则可以使用 JPush 提供的两个方法;如下所示

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// 取得 APNs 标准信息内容

[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
//iOS 7 Remote Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:  (NSDictionary *)userInfo fetchCompletionHandler:(void (^)   (UIBackgroundFetchResult))completionHandler {

[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];

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

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

NSDictionary * userInfo = response.notification.request.content.userInfo;
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFOpenNotification object:userInfo];

completionHandler();
}


2.手动操作部分(自动配置后,部分操作需要手动修改)

在手动配置之前需在TARGETS-> BUILD Phases -> LinkBinary with Libraries中添加 AdSupport.framework 库 (有时会添加不上去需多添加几遍就ok了)

iOS 手动操作部分 (3 个步骤)

● 在 iOS 工程中设置 TARGETS-> BUILD Phases -> LinkBinary with Libraries 找到 UserNotifications.framework 把 status 设为 optional

● 在 iOS 工程中如果找不到头文件可能要在 TARGETS-> BUILD SETTINGS -> Search Paths -> Header Search Paths 添加如下路径

$(SRCROOT)/../node_modules/jpush-react-native/ios/RCTJPushModule


● 在 xcode8 之后需要点开推送选项: TARGETS -> Capabilities -> Push Notification 设为 on 状态

注意

必须是真机测试 虚拟就不支持APns

配置证书http://blog.csdn.net/liu__520/article/details/53133497

建议配置证书观看极光官网的教学视频

https://community.jiguang.cn/t/jpush-ios-sdk/4247

在配置好证书后 需要将极光上ios中的Bundle ID 复制到 Xcode中General下 Bundle identifier 中

在General->Signing 中Team选择的apple ID 必须与你注册证书的apple ID 一致,且保证不报错,接下来就可以在极光官网上发送推送任务了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: