Xamarin HMS Push Kit, notification message, parameter "foreground_show" is ignored?

Viewed 186

According to official Huawei docs, "You can set the message.android.notification.foreground_show field to make notification messages not displayed by NC. In this case, when your app is running in the foreground, the notification messages are transparently transmitted to your app, and processed and displayed by your app."

Sample code:

{
"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"
    ]
}

}

I have a problem with this parameter, it seems it's just ignored. I've created a message service that implements HmsMessageService class, then have overridden its OnMessageReceived(RemoteMessage message) method.

My app succesfully obtains access tokens from server, receives data messages when in foreground, my device receives notification messages when app killed or in background and then starts an app on user tap, but I still can't intercept notification messages when app is in foreground.

Has anyone else had this problem?

1 Answers

You have to implement IHmsPushEvent interface to your Activity or you cannot listen events. This interfaces's functions provides the listening HmsPush events.

Solution:

  1. Implement IHmsPushEvent interface to Activity.
  2. Configure OnMessageReceived method.
  3. Remote Message will be received, after you send the notification.
  4. Use GetNotification method to get notification data.

Sample Code:

        public void OnMessageReceived(RemoteMessage message)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            RemoteMessage.Notification notification = message.GetNotification();
            Log.Info(MethodName,$"Title: {notification.Title}\nBody:{notification.Body}");
        }

For more details, see docs.

Related