Integrating React Native Baidu Plugin

How to integrate Pushwoosh Baidu SDK into your React Native project

Download Sample Module API Docs

1. Install plugin

shell
npm install pushwoosh-react-native-baidu-plugin --save

React Native 0.59 and earlier versions

Starting with 0.60 version, React Native uses Autolinking, so you do not need to link the plugin on your own. However, for earlier versions (0.59 and lower) you still have to do it manually:

shell
react-native link pushwoosh-react-native-baidu-plugin

2. Configure Baidu.

3. Import plugin and register for push notifications

import Pushwoosh from 'pushwoosh-react-native-baidu-plugin';
Pushwoosh.init({
"pw_appid" : "YOUR_PUSHWOOSH_APP_ID" ,
"baidu_api_key" : "YOUR_BAIDU_API_KEY"
});
Pushwoosh.register();
// this event is fired when the push is received in the app
DeviceEventEmitter.addListener('pushReceived', (e: Event) => {
console.warn("pushReceived: " + JSON.stringify(e));
// shows a push is received. Implement passive reaction to a push, such as UI update or data download.
});
// this event is fired when user clicks on notification
DeviceEventEmitter.addListener('pushOpened', (e: Event) => {
console.warn("pushOpened: " + JSON.stringify(e));
// shows a user tapped the notification. Implement user interaction, such as showing push details
});

In some cases, you might need to enable Multidex support for Android app. To do that, add the following parameters to your build.gradle:

build.gradle
defaultConfig {
// Pushwoosh Android SDK doesn't support lower then 17
minSdkVersion 17
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
}

Push Notifications in foreground

When receiving a push in the background, no events are triggered until a push notification is clicked. After it is opened, Pushwoosh plugin fires pushReceived and pushOpenedevents.

When a push is received in the foreground, the plugin fires pushReceived automatically and creates a notification in the Notification Center. When this notification is opened, it fires pushOpened.

You can listen to pushReceived event to immediately react on a push in case it is received in foreground, e.g., update content on a current page in your app. pushOpened, on the other side, is used to react on a notification click event, which requires user interaction, e.g., to navigate within your app, trigger a new process in your app, etc.

Foreground notification creation can be controlled with the following flags added to AndroidManifest.xml and info.plist:

AndroidManifest.xml
AndroidManifest.xml
<!--Add this line to show push notifications in foreground. Use "false" value to disable it-->
<meta-data android:name="PW_BROADCAST_PUSH" android:value="true"/>