Xamarin HMS Push Kit,通知消息,参数"foreground_show"被忽略?

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

根据华为官方文档,“您可以设置 message.android.notification.foreground_show 字段使通知消息不被 NC 显示。在这种情况下,当您的应用程序在前台 运行 时,通知消息透明传输到您的应用程序,并由您的应用程序处理和显示。"

示例代码:

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

}

我对这个参数有疑问,它似乎被忽略了。我创建了一个实现 HmsMessageService class 的消息服务,然后覆盖了它的 OnMessageReceived(RemoteMessage message) 方法。

我的应用程序成功从服务器获取访问令牌,在前台时接收数据消息,当应用程序被杀死或在后台时我的设备收到通知消息,然后在用户点击时启动应用程序,但我仍然无法拦截通知消息当应用程序在前台时。

有其他人遇到过这个问题吗?

您必须为您的 Activity 实现 IHmsPushEvent 接口,否则您无法监听事件。该接口函数提供监听HmsPush事件

解法:

  1. 实现 IHmsPushEvent 接口到 Activity。
  2. 配置 OnMessageReceived 方法。
  3. 发送通知后,将收到远程消息。
  4. 使用 GetNotification 方法获取通知数据。

示例代码:

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

有关详细信息,请参阅 docs