React Native如何让'onMessageReceived'触发Notification(HMS Push Kit)?

How do I get 'onMessageReceived' to trigger on Notification (HMS Push Kit) in React Native?

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

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'

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

onMessageReceived() 仅针对数据消息调用。 查看 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.

未日期:

根据的回答,我把答案分为两部分:

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

这是HMS Core推送服务与FCM的区别。使用HMS Core推送服务时,通知消息默认下发到系统托盘,数据消息默认下发到onMessageReceived方法

另外,HMS Core推送服务提供了应用在前台时向onMessageReceived方法发送通知消息的功能。解决方法是可以在message>android[=36中设置foreground_show字段 =] > notification 使用HMS Core Push Kit服务器API发送消息时。如果此字段设置为 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"
        ]
    }
}

更多详情可以参考Notification Message Display on UI

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

如果应用程序是通过点击推送通知打开的,则可以通过调用 HmsPush.getInitialNotification 来检索远程消息。如果应用程序不是通过点击推送通知打开的,则它 returns 为空。

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

这里是a link

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