React Native 安卓 OPPO 厂商推送

React Native 2020-05-11 阅读 32 评论 0

react-native-oppo-push 使用 OPPO PUSH 客户端SDK接口文档(2.1.0版本),适配最新 Android 10(Android Q)的 OPPO 手机厂商推送,系统级触达,无拦截,亿级推送能力。

安装

在终端运行以下命令:

yarn add react-native-oppo-push

React Native 0.59 以及以下版本,需要手动 Link。

react-native link react-native-oppo-push

在 AndroidManifest.xml 文件中添加以下配置:

<application
    ...
    android:allowBackup="true"
    ...>
    <meta-data android:name="oppo_app_id" android:value="在 OPPO 推送平台的 AppId" />
    <meta-data android:name="oppo_app_key" android:value="在 OPPO 推送平台的 AppKey" />
    <meta-data android:name="oppo_app_secret" android:value="在 OPPO 推送平台的 AppSecret" />
    <meta-data android:name="oppo_channel_id" android:value="在 OPPO 推送平台配置的通道ID" />
    <meta-data android:name="oppo_channel_name" android:value="在 OPPO 推送平台配置的通道名称" />
    <meta-data android:name="oppo_channel_description" android:value="在 OPPO 推送平台配置的通道描述" />
    ...
</application>

Android 8.0(API ≥ 26),需要配置通道,参考 OPPO 文档

Api

引入:

import {
    OppoPush,
    OPPOPushEmitter,
    OT_REGISTER,
    OT_UN_REGISTER,
    OT_GET_PUSH_STATUS,
    OT_GET_NOTIFICATION_STATUS,
    OT_SET_PUSH_TIME,
    OT_ERROR
} from "react-native-oppo-push";

OppoPush 模块的方法如下:

  • init  初始化 OPPO PUSH 服务,创建默认通道。
  • getRegister  获取注册OPPO PUSH推送服务的注册ID
  • unRegister 注销注册OPPO PUSH推送服务
  • requestNotificationPermission  弹出通知栏权限弹窗(仅一次)
  • isSupportPush  判断手机平台是否支持 OPPO PUSH 服务
  • openNotificationSettings  打开通知栏设置界面
  • getPushStatus  获取 OPPO PUSH 推送服务状态
  • getNotificationStatus  获取通知栏状态
  • pausePush  暂停接收 OPPO PUSH 服务推送的消息
  • resumePush  恢复接收OPPO PUSH服务推送的消息,这时服务器会把暂停时期的推送消息重新推送过来
  • getPushVersionCode  获取OPPO PUSH推送服务MCS版本(例如"1701")
  • getPushVersionName  获取 OPPO PUSH 推送服务MCS名称(例如"1.7.1")
  • getSDKVersion  获取 OPPO PUSH 推送服务SDK版本(例如"2.1.0")
  • setPushTime  设置允许推送时间

常量如下:

  • OT_REGISTER  注册
  • OT_UN_REGISTER  注销
  • OT_GET_PUSH_STATUS  推送状态
  • OT_GET_NOTIFICATION_STATUS  通知状态
  • OT_SET_PUSH_TIME  设置推送时间
  • OT_ERROR  错误

事件订阅 OPPOPushEmitter

export default class App extends Component {
    constructor(props) {
        super(props);
        this.onOPPOPushListener = this._onOPPOPushListener.bind(this);
    }
    
    componentDidMount() {
        OPPOPushEmitter.on("OPPO_Push_Response", this.onOPPOPushListener);
    }

    componentWillUnmount() {
        OPPOPushEmitter.removeListener('OPPO_Push_Response', this.onOPPOPushListener);
    }
    
    _onOPPOPushListener(data) {
        let text = "";
        if (data != null) {
            let {code, data, status, message, type} = data;
            switch(type) {
                case OT_REGISTER:
                    if (code == 0) {
                        text = "【注册成功】registerId:" + data;
                    } else {
                        text = "【注册失败】code=" + code;
                    }
                    break;
                case OT_UN_REGISTER:
                    if (code == 0) {
                        text = "【注销成功】code=" + code;
                    } else {
                        text = "【注销失败】code=" + code;
                    }
                    break;
                case OT_GET_PUSH_STATUS:
                    if (code == 0 && status == 0) {
                        text = `【Push状态正常】code=${code},status=${status}`;
                    } else {
                        text = `【Push状态错误】code=${code},status=${status}`;
                    }
                    break;
                case OT_GET_NOTIFICATION_STATUS:
                    if (code == 0 && status == 0) {
                        text = `【通知状态正常】code=${code},status=${status}`;
                    } else {
                        text = `【通知状态错误】code=${code},status=${status}`;
                    }
                    break;
                case OT_SET_PUSH_TIME:
                    text = `【SetPushTime】code=${code},result:${data}`;
                    break;
                case OT_ERROR:
                    text = message;
                    break;
            }
        }
        console.log(text);
    }
}

Demo

  • Demo 代码参考 example

  • 安卓手机也可以直接下载并安装已编译的 apk

最后更新 2020-05-11
MIP.watch('startSearch', function (newVal, oldVal) { if(newVal) { var keyword = MIP.getData('keyword'); console.log(keyword); // 替换当前历史记录,新增 MIP.viewer.open('/s/' + keyword, {replace: true}); setTimeout(function () { MIP.setData({startSearch: false}) }, 1000); } }); MIP.watch('goHome', function (newVal, oldVal) { MIP.viewer.open('/', {replace: false}); });