4

I'm trying to read the payload of my notifications, but the event won't trigger. It works fine for Data messages, but doesn't seem to notice notifications.

AndroidManifest:

        <service
            android:name="com.huawei.hms.push.react.RNHmsMessageService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <receiver android:name="com.huawei.hms.push.react.RNReceiver"/>
        <meta-data
            android:name="push_kit_auto_init_enabled"
            android:value="true" />

RNHmsMessageService

public void onMessageReceived(RemoteMessage message) {
        Log.i(TAG, "onMessageReceived is called");
        if (message == null) {
            Log.e(TAG, "Received message entity is null!");
            return;
        }

        Log.i(TAG, "getCollapseKey: " + message.getCollapseKey()...

        RemoteMessage.Notification notification = message.getNotification();
        if (notification != null) {
            Log.i(TAG, "\n getImageUrl: " + notification.getImageUrl()...
        }

        Boolean judgeWhetherIn10s = false;
        // If the messages are not processed in 10 seconds, the app needs to use WorkManager for processing.
        if (judgeWhetherIn10s) {
            startWorkManagerJob(message);
        } else {
            // Process message within 10s
            processWithin10s(message);
        }
    }

build.gradle

implementation 'com.huawei.hms:push:4.0.4.301'

I think that message.getNotification() is always null, thus not triggering.

3 Answers 3

7

Undate:

According to @Senthil Ssk 's answer, I've split the answer into two parts:

  1. If the app is running in the foreground, notification messages are transparently transmitted to the app. When your app server sends a notification message, the message is processed for display by the app. This function can be implemented by setting the message.android.notification.foreground_show field.
  2. If the app is offline and it is opened by a push notification click, the remote message can be retrieved by calling HmsPush.getInitialNotification. Please kindly refer to docs.

This is a difference between HMS Core Push Kit and FCM. Notification messages will be delivered to the system tray and data messages will be delivered to the onMessageReceived method by default when using HMS Core Push Kit.

In addition, HMS Core Push Kit provides the feature of sending notification messages to the onMessageReceived method when your app is in the foreground. The solution is that you can set the foreground_show field in message > android > notification when using HMS Core Push Kit server API to send messages. If this field is set to true or left empty, notification messages are displayed in system tray even when your app is running in the foreground. If this field is set to false, messages can be delivered to the onMessageReceived method.

Here is an example of payload:

{
    "message": {
        "notification": {
            "title": "message title",
            "body": "message body"
        },
        "android": {
            "notification": {
                "foreground_show": false,
                "click_action": {
                    "type": 1,
                    "action": "com.huawei.codelabpush.intent.action.test"
                }
            }
        },
        "token": [
            "pushtoken1"
        ]
    }
}

For more details, you can refer to Notification Message Display on UI.

4
  • can't get it to work even with "foreground_show": false the notification always appears in the status bar
    – deviant
    Mar 23, 2021 at 10:36
  • hi@deviant,This function is supported on devices running EMUI 9.1.0 or later and the HMS Core SDK 4.0 or later.For Details,pls refer Docs. Mar 24, 2021 at 2:14
  • i use a google device with hms core 5.1.1 installed. btw it works fine on the huawei device.
    – deviant
    Mar 25, 2021 at 7:59
  • 1
    Sorry@ deviant,As team confirmed, this function is only available on Huawei device. Mar 25, 2021 at 8:41
1

For Notification Message use the below code snippet.

If the app is opened by a push notification click, the remote message can be retrieved by calling HmsPush.getInitialNotification If the app is not opened by a push notification click, it returns null.

async function getInitialNotification(){
  try{
     console.log(await HmsPush.getInitialNotification())
  }catch(ex){
     console.log(ex)
  }
}

Here's a link!

This is included in latest plugin release [Cordova Push Plugin 5.0.2 (2020-09-30)]. This will work for sure.

1
  • Even though the official documentation is not up to date, the source code & readme on GitHub contains the new methods. Thank you for the info. :) Oct 6, 2020 at 12:32
1

onMessageReceived() is called only for data messages. Check out this part of Push Kit FAQ:

[Q1] What are differences between data messages and notification messages?

Data messages are not displayed after being sent by HUAWEI Push Kit to phones. Instead, the messages are transferred to the developer's app, and the app is responsible for parsing and displaying messages.

After a device receives a notification message, the system directly displays it in NC. The user can tap the notification message to trigger the corresponding action such as opening the app, a web page, or a specific page in the app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.