HMS Push Kit Notification sometimes arrives as data and sometimes as notification for the same JSON body

Viewed 305

I have an issue when using HMS push kit for sending DATA notifications to make sure onMessageReceived is called but the problem is that for the same JSON body the notification received sometimes arrive as data and on message received is called and other times it's received as notification and is handled by the system tray this is the laravel backend code I'm using:

     $body = [
            "validate_only" => false,
            "message" => [
                "data" => json_encode($this->message),
                "token" => $this->tokens
            ]
        ];
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://push-api.cloud.huawei.com/v1/{some_id}/messages:send',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json; charset=UTF-8',
                'Authorization: Bearer ' . $this->access
            )
        ));
        $response = curl_exec($curl);
        curl_close($curl);

how can I make sure it always arrives as Data?

1 Answers

With downlink message sending by the server, please refer to below webpage and information. Please make sure your message configuration is exactly same as the requirement. Meanwhile please make sure your server or team don’t send push message to your test device. Because data message structure is different from notification message. Data message is impossible to be received as a notification message. https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-server-dev-0000001050040110

enter image description here

{ "validate_only": false, "message": { "data": "{'param1':'value1','param2':'value2'}", "token": [ "pushtoken1", "pushtoken2" ] } }

You can try using Huawei AG console to send a data message as below. If below sending works well, it proves your device to receive data message very well. So you will double check your server confirmation for the push data message. https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/msg-sending-android-0000001136294192

enter image description here

The below webpage is for data message development. https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-basic-receivemsg-0000001087370610

If you still have issue after above test and check, please try to catch some logcat logs so we can analyze the logs for more information. For the guide of logcats: https://developer.android.com/studio/command-line/logcat

Related