3

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?

ccd
  • 5,788
  • 10
  • 46
  • 96

2 Answers2

0

Try with the following code.

if (remoteMessage.getData().containsKey("custom_data")) {
           remoteMessage.getData().get("custom_data");
}
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7
0

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

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Is it mean the only way to pass custom_data tag is to give it under ClickAction tag, could provide some example code to explain more. – ccd Jun 22 '21 at 04:01
  • hi@ccd,i just updated my answer,pls kindly refer. – zhangxaochen Jun 22 '21 at 06:09
  • From document, can't see anything about data message topic, is there any specific reference pages? – ccd Jun 22 '21 at 11:51
  • The document said that when the hms background service is killed, it can't send any data message to client app. If the client app is closed, and I want to sync my server data with the client app data, I must give the server data in notification message pass through user tapping instead of directly pass to the app without user's action? – ccd Jun 22 '21 at 12:32
  • can't see anything about data message topic, is there any specific reference pages? ---you can refere to this [Docs](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-topic-based-sending-0000001050040114?ha_source=hms1). – zhangxaochen Jun 23 '21 at 01:42
  • With regard to the second question that you asked,In this case, for mobile phones outside China, emui 10.0 or later, the closed client app can be wake up by data message. – zhangxaochen Jun 23 '21 at 01:43
  • Why in China different? – ccd Jun 23 '21 at 05:06
  • Applications in China also can wake up the closed client app by using data message,but need to apply for permission, which is different from apps outside China. – zhangxaochen Jun 23 '21 at 06:07