首先安装 jpush-react-native
npm install jpush-react-native --save
注意:如果项目里没有jcore-react-native,需要安装
npm install jcore-react-native --save
配置
build.gradle
android {
defaultConfig {
applicationId "yourApplicationId" //在此替换你的应用包名
...
manifestPlaceholders = [
JPUSH_APPKEY: "yourAppKey", //在此替换你的APPKey
JPUSH_CHANNEL: "yourChannel" //在此替换你的channel
]
}
}
dependencies {
...
implementation project(':jpush-react-native') // 添加 jpush 依赖
implementation project(':jcore-react-native') // 添加 jcore 依赖
}
setting.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')
AndroidManifest.xml
<meta-data
android:name="JPUSH_CHANNEL"
android:value="${JPUSH_CHANNEL}" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="${JPUSH_APPKEY}" />
在MainApplication.java 文件中
添加
import cn.jiguang.plugins.push.JPushModule;
@Override
publicvoidonCreate() {
super.onCreate();
SoLoader.init(this,/*native exopackage*/false);
//调用此方法:点击通知让应用从后台切到前台
JPushModule.registerActivityLifecycle(this); <----------此处添加
}
}
最后页面引入:import JPush from 'jpush-react-native';
使用:
componentWillMount(){
console.log("连接JPUSH》》")
JPush.init();
//连接状态
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);
//自定义消息回调
this.customMessageListener = result => {
console.log("customMessageListener:" + JSON.stringify(result))
};
JPush.addCustomMessagegListener(this.customMessageListener);
//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);
}
最后极光推送的方法,可以在极光官网上看