1

Based on the documentation I follow here documentation

the payload I had sent does not trigger onMessageReceived method for me to parse it instead it automatically trigger notification by the Huawei notification center.

this is a sample payload I've sent, and I've already include foreground_show and set it to false as mention in the web:

{
    "validate_only":false,
    "message": {
        "notification": {
            "title": "message title",
            "body": "message body"
        },
        "android": {
            "notification": {
                "foreground_show": false,
                "click_action": {
                    "type": 3
                }
            }
        },
        "data":"{'param2':'value1','param3':'value2'}",
        "token": [
            "ABW18Q4Rw5CAB68f9yS_1f859k0s-t3G1aIZheq5l6TedFj_Iold4I6M2EK-pwPTzt6HXxL_"
        ]
    }
}

the result was, it does not trigger onMessageReceive function but it automatically creates the notification on the device.

but if I remove notification and android from the payload and only sending data it successfully trigger onMessageReceive :

{
    "validate_only": false,
    "message": { 
        "data": "{'param1':'value1','param2':'value2'}",
        "token": [
            "ABW18Q4Rw5CAB68f9yS_1f859k0s-t3G1aIZheq5l6TedFj_Iold4I6M2EK-pwPTzt6HXxL_"
        ]
    }
}

this is the class where I already override the onMessageReceived:

class CustomPushService : HmsMessageService() {
    private val TAG = "PushTokenLog"

    override fun onNewToken(token: String?, bundle: Bundle?) {
        super.onNewToken(token, bundle)
        Log.d(TAG, "receive token:$token")
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage?) {
        Log.d(TAG, "onMessageReceived")
        Log.d(TAG, "onMessageReceived:title:${remoteMessage?.notification?.title}")
        super.onMessageReceived(remoteMessage)

    }
}

I already include foreground_show: false and it wont trigger onMessageReceived unless if I only sending data in the payload then it will trigger onMessageReceived.

so is it not possible to send full payload as shown in the first payload and trigger onMessageReceived so that I can process the payload? and please let me know if my method wrong

faryz ryz
  • 11
  • 2

1 Answers1

1

In your request payload, you've set "message.android.notification.foreground_show” to “false”, which means your app is in the foreground and you will get the message data in the function onMessageReceived. Please double-check if your app is in the foreground.

Your device EMUI must be greater than EMUI 9.1.0, and Push SDK version must be greater than 4.0.

The class CustomPushService definition is good.

I used your 1st sample payload to test on my project. It worked very well. Please see below screenshot. The process entered into the function onMessageReceived as below.

enter image description here

Please refer to https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-server-dev-0000001050040110 and https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/https-send-api-0000001050986197 for more information.

If you still have issues, please share logcat logs with me.

Zinna
  • 1,947
  • 2
  • 5
  • 20