极光推送RN集成

十一月 08, 2018

Android项目配置

  1. settings.gradle 配置

    include ':jcore-react-native'
    project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')
    
    include ':jpush-react-native'
    project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android')
  2. android-build.gradle 配置

    defaultConfig {
        // 这里与极光推送 后台配置的应用包名一致:com.example
        applicationId "com.example"
    
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    
        // 这里新增配置
        manifestPlaceholders = [
            JPUSH_APPKEY: "9b65e821e1f045880ca7f024",   // 极光推送生成的应用key
            APP_CHANNEL : "default"                     // 默认
        ]
    
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
  3. AndroidManifest.xml 配置

    <application>
    ...
        <meta-data
            android:name="JPUSH_APPKEY"
            android:value="${JPUSH_APPKEY}" />
        <meta-data
        android:name="JPUSH_CHANNEL"
        android:value="${APP_CHANNEL}" />
    ...
    </application>
  4. package包集成

    protected List<ReactPackage> getPackages() {
    // Add additional packages you require here
    // No need to add RnnPackage and MainReactPackage
        return Arrays.<ReactPackage>asList(
            // eg. new VectorIconsPackage()
            new LottiePackage(),
            new SvgPackage(),
            new RNSentryPackage(MainApplication.this),
            new AMapGeolocationPackage(),
            new SplashScreenReactPackage(),
            new ImagePickerPackage(),
            new JPushPackage(false, false)  // 新增
        );
    }

ios配置

  1. 在 iOS 工程中设置 TARGETS-> BUILD Phases -> LinkBinary with Libraries 找到 UserNotifications.framework 把 status 设为 optional
  2. 在 iOS 工程中如果找不到头文件可能要在 TARGETS-> BUILD SETTINGS -> Search Paths -> Header Search Paths 添加如下路径

    $(SRCROOT)/../node_modules/jpush-react-native/ios/RCTJPushModule
  3. 在 xcode8 之后需要点开推送选项: TARGETS -> Capabilities -> Push Notification 设为 on 状态

ios集成证书配置

  1. 具体参考官方介绍

调用推送

  1. 使用REST API v3

    curl --insecure -X POST -v https://api.jpush.cn/v3/push -H "Content-Type: application/json" -u "9b65e821e1f045880ca7f024:90cd1ef396c86675417be793"  -d  '{"platform":"all","audience":"all","notification":{"alert":"Hi,JPush!"}}'
  2. 使用极光后台管理系统推送