平台:android、ios
依赖包:jpush-react-native、jcore-react-native
准备:
? ? ? ? 1.首先要在极光推送有个账号【注册/登录 以下链接:极光推送】
? ? ? ? 2.ios设备要把证书申请了【ios推送证书配置】
? ? ? ? 3.下载安装依赖包【jpush-react-native、jcore-react-native】
npm install jpush-react-native jcore-react-native
一、极光推送设置:
? ? ? ? 1.登陆后到应用管理画面 
? ? ?点击右上角【应用管理】 
?2.创建应用填写项目名(必填)和用途(不填或随便选选都行)

?3.首先先创建android【这里如果只用推送就之选消息推送就行,然后点下一步】

?填写包名,包名可以在【android/app/src/main/AndroidManifest.xml 下 manifest标签的package属性上查看】 
?最后这里给了一个demo下面还写了android项目的配置方法都可以拿下来试一试,也可以直接点上面的ios去配置ios
?4.创建ios:
????????第一步和Android的一样 ,我这里只用推送所以只选了【消息推送】

第二步这里要用到之前配置好的生产模式和开发模式的推送证书?,就是上面【准备】时的?【ios推送证书配置】
?上传完之后这里也给了ios的配置方法
?5.查看AppKey【点应用设置可以看到】
二、android配置
? ? ? ?1. 【android/app/src/main/AndroidManifest.xml】
? ? ? ? ? ? ? ? 在application标签下添加
????????<meta-data android:name="JPUSH_CHANNEL" android:value="${JPUSH_CHANNEL}" />
? ? ? ? <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" />
? ? ? ? ?2. 【android\app\build.gradle】
? ? ? ? ? ? ? ? 在dependencies下添加
implementation project(':jpush-react-native')? // 添加 jpush 依赖 | implementation project(':jcore-react-native')? // 添加 jcore 依赖 |

????????????????【这个可以不加在代码init中设置也可以】在defaultConfig下添加
manifestPlaceholders = [ | ????????????????? JPUSH_APPKEY:"xxxxxx",???????? //在此替换你的APPKey | ????????????????? JPUSH_CHANNEL: "xxxxxx"??????? //在此替换你的channel | ????????? ] | |
? ? ? ? 3.【android\settings.gradle】
include ':jpush-react-native' | project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android') | include ':jcore-react-native' | project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android') |
?
?三、IOS配置
? ? ? ? 1.pod【到ios文件夹下,执行pod install】? ? ? ? ? ? ? ?
项目根目录>cd ios
项目根目录/ios>pod install
????????2.?AppDelegate.m文件添加代码:【可参照:AppDelegate.m文件】
? ? ? ? ? ? ? ? 1)导包部分:
#import <RCTJPushModule.h>
// 以下两个包是上面参照文件中没有的但是要加上
#import "JPUSHService.h"
#import <UserNotifications/UserNotifications.h>
?? ? ? ? ? ? ? ? 2)didFinishLaunchingWithOptions下添加如下代码【位置可以参照以下第二张图或AppDelegate.m文件】
// APNS
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
if (@available(iOS 12.0, *)) {
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
// 自定义消息
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
// 地理围栏
[JPUSHService registerLbsGeofenceDelegate:self withLaunchOptions:launchOptions];

? ? ? ? ? ? ? ? ?3)在文件最下面【@end上一行】
?
//************************************************JPush start************************************************
//注册 APNS 成功并上报 DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
//iOS 7 APNS
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// iOS 10 以下 Required
NSLog(@"iOS 7 APNS");
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
//iOS 10 前台收到消息
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// Apns
NSLog(@"iOS 10 APNS 前台收到消息");
[JPUSHService handleRemoteNotification:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_ARRIVED_EVENT object:userInfo];
}
else {
// 本地通知 todo
NSLog(@"iOS 10 本地通知 前台收到消息");
[[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_ARRIVED_EVENT object:userInfo];
}
//需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
completionHandler(UNNotificationPresentationOptionAlert);
}
//iOS 10 消息事件回调
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)(void))completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// Apns
NSLog(@"iOS 10 APNS 消息事件回调");
[JPUSHService handleRemoteNotification:userInfo];
// 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
[[RCTJPushEventQueue sharedInstance]._notificationQueue insertObject:userInfo atIndex:0];
[[NSNotificationCenter defaultCenter] postNotificationName:J_APNS_NOTIFICATION_OPENED_EVENT object:userInfo];
}
else {
// 本地通知
NSLog(@"iOS 10 本地通知 消息事件回调");
// 保障应用被杀死状态下,用户点击推送消息,打开app后可以收到点击通知事件
[[RCTJPushEventQueue sharedInstance]._localNotificationQueue insertObject:userInfo atIndex:0];
[[NSNotificationCenter defaultCenter] postNotificationName:J_LOCAL_NOTIFICATION_OPENED_EVENT object:userInfo];
}
// 系统要求执行这个方法
completionHandler();
}
//自定义消息
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:J_CUSTOM_NOTIFICATION_EVENT object:userInfo];
}
//************************************************JPush end************************************************
?? ? ? ? ?2.Xcode设置
? ? ? ? ? ? ? ? 1)Push Notification
? ? ? ? ? ? ? ? 查看【Signing & Capabilities】有没有?【Push Notification】,没有的话向下图一样添加
? ? ? ? ? ? ? ? 单机【+】,在弹出的搜索框中搜索【Push Notification】并添加

? ? ? ? ? ? ? ? 2)查看【Build Phasses】下【Link Binart With Libraries】是否有以下项目,没有的话点下面的【+】添加
libz.tbd
CoreTelephony.framework
Security.framework
CFNetwork.framework
CoreFoundation.framework
SystemConfiguration.framework
Foundation.framework
UIKit.framework
UserNotifications.framework
libresolv.tbd

?四、代码
// 初始化
JPush.init({"appKey":"你的极光推送key","channel":"dev","production":1});
//连接状态
this.connectListener = result => {
console.log("connectListener:" + JSON.stringify(result))
};
JPush.addConnectEventListener(this.connectListener);
//通知回调
this.notificationListener = result => {
console.log("notificationListener:" + JSON.stringify(result))
};
JPush.addNotificationListener(this.notificationListener);
//本地通知回调
this.localNotificationListener = result => {
console.log("localNotificationListener:" + JSON.stringify(result))
};
JPush.addLocalNotificationListener(this.localNotificationListener);
//tag alias事件回调
this.tagAliasListener = result => {
console.log("tagAliasListener:" + JSON.stringify(result))
};
JPush.addTagAliasListener(this.tagAliasListener);
//手机号码事件回调
this.mobileNumberListener = result => {
console.log("mobileNumberListener:" + JSON.stringify(result))
};
JPush.addMobileNumberListener(this.mobileNumberListener);
JPush.getRegistrationID((registerID) => {
console.log('registerID : ', registerID);
})
? ? ??
|