首先cd到项目的跟路径下面添加极光推送的库
npm install jcore-react-native --save 如图
4920F336-0AB9-4AA2-88E0-6A7A5074F4E8.png
在执行 npm install jpush-react-native --save 如图
5C742718-3507-4EF9-9EFD-B55F58583807.png
再去把这些库添加到项目里,执行 react-native link 导入成功
0FAC3292-F320-4439-AAC6-3EC3945ACD90.png
ios 的配置:
在 iOS 工程 target 的 Build Phases->Link Binary with Libraries 中加入如下库:如下图
libz.tbd
CoreTelephony.framework
Security.framework
CFNetwork.framework
CoreFoundation.framework
SystemConfiguration.framework
Foundation.framework
UIKit.framework
UserNotifications.framework
libresolv.tbd
12DEC172-78B8-407D-A1C8-05191CBB0076.png
打开项目中的AppDelegate 文件,引入两个文件名
import
import ,
并且定义三个参数
2D171439-BB81-46D6-8138-F496417671C2.png
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//iOS 8以前 categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel apsForProduction:isProduction];
NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"ts"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
//添加这个方法,将设备token传给极光的服务器
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
//取得APNS推送过来的消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
添加如下几个方法
- (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];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
- (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];
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
completionHandler();// 系统要求执行这个方法
}
//iOS 7 Remote Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[NSNotificationCenter defaultCenter] postNotificationName:kJPFDidReceiveRemoteNotification object:userInfo];
}
代码写完后,配置Info.plist文件
设置ATS权限 App Transport Security Settings的 Allow Arbitrary Loads为YES Exception Domains下的jpush.cn下的NSExceptionAllowsInsecureHTTPLoads为YES 和NSIncludesSubdomains为YES
688C1F21-D227-44B7-959F-1ECB27B0585F.png
打开推送
18C18ACF-4459-43AA-824B-0060E0601D3A.png
打开选择配置证书
092CC34C-EA73-4892-BCD6-5C1E53FE3F82.png
证书的配置
70556BC8-D831-427E-9906-3A8576C90D6A.png
F7DFAAA7-1BE7-449B-A728-3AE4D57DA36A.png
D307753D-1C77-444D-B0EE-CA3A8F37A94C.png
这里就配置完ios的配置了
然后看下代码的实现
0B8024FA-4A14-4241-B882-FE518F9D3290.png
这样就可以推送了。
安卓的配置
94CF55BE-E0A9-4824-A112-D38D87E80911.png
include ':jpush-react-native',':jcore-react-native'
project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android')
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')
Pasted Graphic.png
打开app文件下的build.gradle
manifestPlaceholders=[ JPUSH_APPKEY: "946df2fa1542693f71161217”,//自己的申请的key APP_CHANNEL: "应用宝" //应用渠道号,默认就好]
5FCA3DB5-B30A-4FBF-A6E1-14A8BC92C206.png
21F0F84C-FF6B-4A14-BC4F-5604E6386635.png
41608E83-6678-4488-AC72-7232FBA8F33A.png
6F99A996-70AC-46B3-A7A1-70C667C19D2A.png
配置完成后,在同步,然后就可以正常推送了