前期准备
1.注册登录极光网站并创建应用
2.创建完应用会得到一个appkey(后续配置会用到)
3.进入自己的项目根目录
安装
npm install jpush-react-native --save
npm install jcore-react-native --save
并连接到原生库
react-native link jpush-react-native
react-native link jcore-react-native
配置
一、Android配置
1.项目名\android\app\build.gradle
android {
defaultConfig {
applicationId "yourApplicationId" //在此替换你的应用包名 例com.iotapp
...
manifestPlaceholders = [
JPUSH_APPKEY: "yourAppKey", //在此替换你的APPKey
JPUSH_CHANNEL: "yourChannel" //在此替换你的channel
]
}
}
dependencies {
...
implementation project(':jpush-react-native') // 添加 jpush 依赖
implementation project(':jcore-react-native') // 添加 jcore 依赖
}
2.项目名\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')
3.项目名\android\app\src\main\AndroidManifest.xml
<meta-data
android:name="JPUSH_CHANNEL"
android:value="${JPUSH_CHANNEL}" />
<meta-data
android:name="JPUSH_APPKEY"
android:value="${JPUSH_APPKEY}" />
4.项目名\android\app\src\main\java\com\iotapp\MainApplication.java
import cn.jiguang.plugins.push.JPushPackage;
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new JPushPackage(),//添加这句
);
}
5.项目名\App.js
import JPush from 'jpush-react-native';
componentDidMount() {
const { navigation } = this.props;
JPush.init();
}
点开推送消息跳转到相应页面!!!!
import { AsyncStorage,DeviceEventEmitter,Platform } from ‘react-native’;
测试
进入创建的应用