How to get custom data

Viewed 197

The custom tagged data defined in message payload like this.

{
    "message": {
        "notification": {
            "title": "message title",
            "body": "message body"
        },
        "android": {
            ...
        },
        "token": [
            "pushtoken1"
        ]
    },
    "custom_data": ...
}

And I want to get this custom_data from RemoteMessage object.

public class HmsService extends HmsMessageService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        // remoteMessage.getCustomData
    }
}

How to code this function?

2 Answers

Try with the following code.

if (remoteMessage.getData().containsKey("custom_data")) {
           remoteMessage.getData().get("custom_data");
}

Update

Refer the document ,you can try the following sample code:

enter image description here

enter image description here


Huawei Push Kit supports two types of messages: notification messages and data messages.

According to the data you send, you are using Notification message. The customized array of Notification message cannot be directly sent to the app. Data needs to be transferred through ClickAction method.

The user-defined data can be directly transferred to the data field in a Data Messages,and you can use onMessageReceived method to receive it.

For more details, you can refer to the following Docs:

Sending Downlink Messages

Passing Parameters Through ClickAction

Receiving Messages

Related