安装
- RN版本 0.49
npm i jpush-react-native --save
npm i jcore-react-native --save ## jpush-react-native 1.4.2 版本以后需要同时安装
1.配置
- 执行脚本
npm run configureJPush <yourAppKey> <yourModuleName>
//module name 指的是你 Android 项目中的模块名字(对 iOS 没有影响,不填写的话默认值为 app,会影响到查找 AndroidManifest 问题,
//如果没找到 AndroidManifest,则需要手动修改
//举个例子:
npm run configureJPush d4ee2375846bc30fa51334f5 app
//执行完毕再link
react-native link
- 检查配置项
/android/app/build.gradle 没有就加上
android {
defaultConfig {
applicationId "yourApplicationId" //包名
...
manifestPlaceholders = [
JPUSH_APPKEY: "yourAppKey", //在此替换你的APPKey
APP_CHANNEL: "developer-default" //应用渠道号
]
}
}
...
dependencies {
compile fileTree(dir: "libs", include: ["*.jar"])
compile project(':jpush-react-native') // 添加 jpush 依赖
compile project(':jcore-react-native') // 添加 jcore 依赖
compile "com.facebook.react:react-native:+"
}
android/settings.gradle 没有就加上
include ':app', ':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')
android/app/src/main/AndroidManifest.xml 下面没有的就加上
<permission android:name="${applicationId}.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> //可以把权限也加上
<application
...
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data android:name="JPUSH_CHANNEL" android:value="${APP_CHANNEL}"/>
<meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}"/>
</application>
更新 MainActivity.java 文件
import cn.jpush.android.api.JPushInterface; //导入
public class MainActivity extends ReactActivity {
//方法没有就全部加上
@Override
protected void onCreate(Bundle savedInstanceState) {
JPushInterface.init(this); //添加到这里
super.onCreate(savedInstanceState);
}
@Override
protected String getMainComponentName() {
return "RNTEST";
}
@Override
protected void onPause() {
super.onPause();
JPushInterface.onPause(this);
}
@Override
protected void onResume() {
super.onResume();
JPushInterface.onResume(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
更新 MainApplication.java 文件
import cn.jpush.reactnativejpush.JPushPackage; //导入
public class MainApplication extends Application implements ReactApplication {
// 设置为 true 将不弹出 toast
private boolean SHUTDOWN_TOAST = false;
// 设置为 true 将不打印 log
private boolean SHUTDOWN_LOG = false;
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new JPushPackage(SHUTDOWN_TOAST, SHUTDOWN_LOG) //这行没有就加上
);
}
};
···
}
3.使用
import JPushModule from 'jpush-react-native';
export default class WelcomePage extends Component {
componentWillMount() {
//必须先调用这个方法
JPushModule.notifyJSDidLoad((resultCode) => {
if (resultCode === 0) {
}
});
//这是点击通知触发的事件
JPushModule.addReceiveNotificationListener((map) => {
});
}
}