【问题标题】:Xamarin HMS Push Kit, notification message, parameter "foreground_show" is ignored?Xamarin HMS Push Kit,通知消息,参数“foreground_show”被忽略?
【发布时间】:2021-03-08 00:27:40
【问题描述】:

根据华为官方文档,“你可以设置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 类的消息服务,然后重写了它的 OnMessageReceived(RemoteMessage message) 方法。

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

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

【问题讨论】:

    标签: android huawei-mobile-services


    【解决方案1】:

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

    解决方案:

    1. 为 Activity 实现 IHmsPushEvent 接口。
    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。

    【讨论】:

    • 我找到了真正的原因 :) 在编译 Xamarin Android 库之前,我忘记从 xamarin-push-package-5.0.2.300 源文件中删除示例模块。并且 XamarinHmsPushDemo.Hmssample 类截获了我的消息 :) Mea culpa, mea maxima culpa :) 但无论如何谢谢你,你的回答帮助我更深入地研究 Push Kit 模块:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多