3

我正在尝试读取通知的有效负载,但该事件不会触发。它适用于数据消息,但似乎没有注意到通知。

安卓清单:

        <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);
        }
    }

构建.gradle

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

我认为 message.getNotification() 始终为空,因此不会触发。

4

3 回答 3

7

日期:

根据@Senthil Ssk的回答,我将答案分为两部分:

  1. 如果应用程序在前台运行,通知消息会透明地传输到应用程序。当您的应用服务器发送通知消息时,该消息将被应用程序处理以显示。这个功能可以通过设置message.android.notification.foreground_show字段来实现。
  2. 如果应用离线并且通过推送通知单击打开,则可以通过调用HmsPush.getInitialNotification检索远程消息。请参阅文档

这是 HMS Core Push Kit 和 FCM 的区别。使用 HMS Core Push Kit 时,通知消息将发送到系统托盘,数据消息将默认发送到 onMessageReceived 方法。

此外,HMS Core Push Kit 提供了当您的应用在前台时,向 onMessageReceived 方法发送通知消息的功能。解决方法是在使用HMS Core Push Kit服务端API发送消息时,可以在message > android > notification中设置foreground_show字段。如果此字段设置为 true 或留空,即使您的应用程序在前台运行,通知消息也会显示在系统托盘中。如果此字段设置为 false,则可以将消息传递到 onMessageReceived 方法。

以下是有效载荷的示例:

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

更多详情,您可以参考UI 上的通知消息显示

于 2020-09-01T07:11:09.910 回答
1

onMessageReceived()仅对数据消息调用。查看Push Kit FAQ的这一部分:

[Q1] 数据消息和通知消息有什么区别?

HUAWEI Push Kit向手机发送数据消息后不显示。相反,消息被传输到开发者的应用程序,应用程序负责解析和显示消息。

设备收到通知消息后,系统直接在NC中显示。用户可以点击通知消息触发相应的动作,例如打开应用程序、网页或应用程序中的特定页面。

于 2020-08-31T14:34:37.950 回答
1

对于通知消息,请使用以下代码片段。

如果应用是通过推送通知点击打开的,则可以通过调用 HmsPush.getInitialNotification 来获取远程消息。如果应用不是通过推送通知点击打开的,则返回 null。

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

这是一个链接

这包含在最新的插件版本 [Cordova Push Plugin 5.0.2 (2020-09-30)] 中。这肯定会奏效。

于 2020-10-05T06:35:55.257 回答