Table of contents

OPPO PUSH for React Native

React Native May 12, 2020 Viewed 13 Comments 0

react-native-oppo-push is developed with the official document OPPO PUSH client SDK interface documentation (version 2.1.0). It is compatible with the latest Android 10 (Android Q) OPPO mobile phone manufacturer push, system-level touch, no interception, and billion-level push capability.

Install

yarn add react-native-oppo-push

React Native <= 0.59

react-native link react-native-oppo-push

In AndroidManifest.xml, add the following code.

<application
    ...
    android:allowBackup="true"
    ...>
    <meta-data android:name="oppo_app_id" android:value="Your AppId" />
    <meta-data android:name="oppo_app_key" android:value="Your AppKey" />
    <meta-data android:name="oppo_app_secret" android:value="Your AppSecret" />
    <meta-data android:name="oppo_channel_id" android:value="Your Channel ID" />
    <meta-data android:name="oppo_channel_name" android:value="Your Channel Name" />
    <meta-data android:name="oppo_channel_description" android:value="Your Channel Desc" />
    ...
</application>

For Android 8.0 (API ≥ 26), you need to configure the channel, refer to the OPPO document.

Api

Import

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";

Methods of the OppoPush

  • init

    Initialize the OPPO PUSH service and create a default channel.

  • getRegister 

    Get the registration ID of OPPO PUSH service.

  • unRegister

    Unregister OPPO PUSH service.

  • requestNotificationPermission 

    Pop-up window requesting notification permission (only once).

  • isSupportPush 

    Check if the mobile supports OPPO PUSH service.

  • openNotificationSettings

    Goto the notification setting page.

  • getPushStatus  

    Get OPPO PUSH service status.

  • getNotificationStatus 

    Get notification status.

  • pausePush 

    Pause receiving messages pushed by OPPO PUSH service.

  • resumePush 

    Resume receiving messages pushed by OPPO PUSH service. Then the OPPO PUSH service will re-push the messages during the pause period.

  • getPushVersionCode 

    Get the MCS version of OPPO PUSH service (eg "1701").

  • getPushVersionName 

    Get the MCS name of the OPPO PUSH service (eg "1.7.1").

  • getSDKVersion

    Get the OPPO PUSH service SDK version (eg "2.1.0").

  • setPushTime 

    Set allow push time.

Constans

  • OT_REGISTER  
  • OT_UN_REGISTER  
  • OT_GET_PUSH_STATUS  
  • OT_GET_NOTIFICATION_STATUS  
  • OT_SET_PUSH_TIME  
  • OT_ERROR  

OPPOPushEmitter

OPPOPushEmitter is for event subscription.

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 = "[Register success] registerId:" + data;
                    } else {
                        text = "[Register fail] code=" + code;
                    }
                    break;
                case OT_UN_REGISTER:
                    if (code == 0) {
                        text = "[Unregister success] code=" + code;
                    } else {
                        text = "[Unregister fail] code=" + code;
                    }
                    break;
                case OT_GET_PUSH_STATUS:
                    if (code == 0 && status == 0) {
                        text = `[Push status is ok] code=${code},status=${status}`;
                    } else {
                        text = `[Push status is error]code=${code},status=${status}`;
                    }
                    break;
                case OT_GET_NOTIFICATION_STATUS:
                    if (code == 0 && status == 0) {
                        text = `[Notification status is ok] code=${code},status=${status}`;
                    } else {
                        text = `[Notification status is error] 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

Updated May 12, 2020