美文网首页
reactNative 极光推送踩过的坑

reactNative 极光推送踩过的坑

作者: HT_Jonson | 来源:发表于2017-01-19 17:32 被阅读0次

    这个安卓的极光推送搞了一个月,当然,中间有很多事情要忙,所以拖了一个月的时间.

    此处我用的是jpush-react-native,这个是极光官网维护的,还有一个是react-antive-jpush,这是中文网的,官网的已经停止维护,大家要使用极光维护的插件.

    我这里只介绍一下安卓吧,iOS的可以自行百度,没任何问题,我iOS至少配置了10几次,重来没出过错.

    一、手动配置

    1.1、进入你的项目目录,然后打开命令终端输入:

    npm install jpush-react-native --save
    
    rnpm link jpush-react-native(这个貌似只对iOS有作用)
    

    Android

    使用 Android Studio import 你的 React Native 应用(选择你的 React Native 应用所在目录下的 android 文件夹即可)

    二、修改 android 项目下的 settings.gradle 配置:

    settings.gradle

    include ':app', ':jpush-react-native
    project(':jpush-react-native').projectDir = new File(rootProject.projectDir, ../node_modules/jpush-react-native/android')
    

    修改 app 下的 AndroidManifest 配置,将 jpush 相关的配置复制到这个文件中,参考 demo 的 AndroidManifest:(增加了 <meta-data> 部分)

    your react native project/android/app/AndroidManifest.xml

    <application
            android:name=".MainApplication"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    
            <!-- 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>
    
    

    修改 app 下的 build.gradle 配置:
    your react native project/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')
        compile "com.facebook.react:react-native:+"  // From node_modules
    }
    

    将此处的 yourApplicationId 替换为你的项目的包名;yourAppKey 替换成你在官网上申请的应用的 AppKey。到此为止,配置完成。

    现在重新 sync 一下项目,应该能看到 jpush-react-native 作为一个 android Library 项目导进来了

    Paste_Image.png

    如果你还是不能集成的话加如下代码MainApplication.js下加入如下代码
    android/src/main/com/你的项目名/MainApplication

    Paste_Image.png

    这里配置就结束了,对了 就这么简单.

    三、集成成功所需要的

    RegistrationID  这个如果你没获取到的话,那么证明你没有集成成功
    

    怎么获取看下面代码

    首先导入

    import JPushModule from 'jpush-react-native';//极光推送
    
    componentDidMount() {
            //---------------------------------android start---------------------------------
    
    
            JPushModule.addReceiveCustomMsgListener((message) => {
                this.setState({ pushMsg: message });
                alert(message)
            });
            JPushModule.addReceiveNotificationListener((message) => {
                console.log("receive notification: " + message);
                alert(JSON.stringify(message));
            });
            //获取RegistrationID  
            JPushModule.getRegistrationID((id)=>{
                console.debug(JSON.stringify(id));
            })
    }
    

    不要以为这样就完成了
    你会发现开始还是无法获取到
    那么你就要加上如下代码,
    这段代码要在RN启动的时候加进去,别忘记导入包

    JPushModule.initPush()
    

    好了完成,如果你还是收不到消息再看下面
    android/build.gradle

    Paste_Image.png

    版本如果是1.2.3的话 那么是不支持的,请更新到1.3.1以上.

    关于这个版本的问题以后有时间再贴出来

    相关文章

      网友评论

          本文标题:reactNative 极光推送踩过的坑

          本文链接:https://www.haomeiwen.com/subject/grbibttx.html