Get the payload field
This document is applicable for developing applications on the following platforms: iOS and Android.
Introduction
The offline push sender sends additional information to the receiver by configuring the payload parameter of ZIMPushConfig (the version before 2.5.0 is the extendedData parameter). When the receiver needs to use the ZPNs SDK, the payload in the extras of the push channel field to get this information.
This document introduces how to use the ZPNs SDK interface to obtain the payload transparent transmission field.
- The ZPNs SDK of the ReactNative platform needs to be used with ZIM ReactNative SDK 2.2.0 or above.
- Before using the ZPNs SDK, please configure the ZIM offline push certificate in the ZEGO Admin Console (for details, please refer to Project Management- IM- Offline Push Configuration), If it cannot be configured, please contact ZEGOCLOUD technical support.
Prerequisites
Before getting the payload transparent field, please make sure:
- Integrate the ZPNs SDK. For more, refer to the Implement offline push notification.
Implementation steps
According to the push service provider of the offline push receiving end, developers should choose different ways to obtain the payload transparent transmission field, which are respectively listening to the notification of ZPNsEventHandler and obtaining it from the Activity in a native way.
Obtained through the ZPNsMessage object in the ZPNsEventHandler callback method (supports all push service providers)
Through the on method of ZPNs, listen to the callbacks of ZPNsEventHandler:
/**
* Interface definition
*/
interface ZPNsEventHandler {
registered: (message: ZPNsRegisterMessage) => void;
notificationArrived: (message: ZPNsMessage) => void;
notificationClicked: (message: ZPNsMessage) => void;
throughMessageReceived: (message: ZPNsMessage) => void;
}
/* register listener - Example */
ZPNs.getInstance().on('registered', function(message) {
console.log('[ZPNs] registered. pushID: ' + message.pushID + ", error: " + message.errorCode)
});
/* Use the following method as needed to get the payload field */
ZPNs.getInstance().on('throughMessageReceived', function(message) {
console.log('[ZPNs] throughMessageReceived', message.extras.payload)
});
ZPNs.getInstance().on('notificationClicked', function(message) {
console.log('[ZPNs] notificationClicked', message.extras.payload)
});
ZPNs.getInstance().on('notificationArrived', function(message) {
console.log('[ZPNs] notificationArrived', message.extras.payload)
});
Obtain from Activity (only supports Google FCM)
- In the AndroidManifest.xml file, set the exported attribute of the main Activity to true, and configure the intent-filter.
<activity
android:name="The Main Activity path of your project"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- The developer obtains the data in the
onCreate
oronNewIntent
of the main Activity through the following methods.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String payload = getIntent().getExtras().getString("payload");
}