npm install jpush-react-native jcore-react-native --save
- 在 iOS 工程中设置 TARGETS-> BUILD Phases -> LinkBinary with Libraries 找到 UserNotifications.framework 把 status 设为 optional
- 在 iOS 工程中如果找不到头文件可能要在 TARGETS-> BUILD SETTINGS -> Search Paths -> Header Search Paths 添加如下路径
$(SRCROOT)/../node_modules/jpush-react-native/ios/RCTJPushModule
- 在 xcode8 之后需要点开推送选项: TARGETS -> Capabilities -> Push Notification 设为 on 状态
-
修改 app 下的 build.gradle 配置:
your react native project/android/app/build.gradle
android { defaultConfig { applicationId "yourApplicationId" ... manifestPlaceholders = [ JPUSH_APPKEY: "yourAppKey", //在此替换你的APPKey JPUSH_CHANNEL: "developer-default" //在此替换你的channel ] } } ... dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation project(':jpush-react-native') // 添加 jpush 依赖 implementation project(':jcore-react-native') // 添加 jcore 依赖 implementation "com.facebook.react:react-native:+" // From node_modules }
同时在AndridManifest.xml中添加如下代码
<meta-data android:name="JPUSH_CHANNEL" android:value="${JPUSH_CHANNEL}" /> <meta-data android:name="JPUSH_APPKEY" android:value="${JPUSH_APPKEY}" />
-
修改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')
操作完成后点击AndroidStudio的构建
-
在Application中添加JPush
public class MainApplication extends Application implements ReactApplication { private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { @Override public boolean getUseDeveloperSupport() { return BuildConfig.DEBUG; } @Override protected List<ReactPackage> getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); //建议在调试期间将参数都设置为true,便于调试 packages.add(new JPushPackage(true,true)); return packages; } @Override protected String getJSMainModuleName() { return "index"; } }; @Override public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); //强烈建议在Application初始化时调用原生接口的init方法 JPushInterface.init(this); } }
Android v1.6.6 版本后新增 notifyJSDidLoad,请务必在接收事件之前调用此方法。
- Android v1.6.7 新增 API
jumpToPushActivity
,使用参考 demo