在React Native中,如何使' onMessageReceived '在通知(HMS Push Kit)触发?

4

我正在尝试读取我的通知的有效内容,但事件不会触发。对于数据消息来说,它可以正常工作,但好像没有注意到通知。

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()始终为空,因此不会触发。
3个回答

7

更新:

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

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

这是HMS核心推送套件和FCM之间的区别。使用HMS Core Push Kit时,默认情况下通知消息将传递到系统托盘,数据消息将传递到onMessageReceived方法。

此外,HMS Core Push Kit提供了将通知消息发送到onMessageReceived方法的特性,当您的应用程序在前台运行时。解决方案是,在使用HMS Core Push Kit服务器API发送消息时,在message > android > notification中设置foreground_show field。如果将此字段设置为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上的显示


即使设置了 "foreground_show": false,仍无法使其正常工作,通知总是出现在状态栏中。 - deviant
hi@deviant,此函数支持运行EMUI 9.1.0或更高版本和HMS Core SDK 4.0或更高版本的设备。详情请参阅文档 - zhangxaochen
我正在使用已安装了HMS Core 5.1.1的谷歌设备。顺便说一下,在华为设备上运行良好。 - deviant
1
抱歉@ deviant,团队确认,此功能仅适用于华为设备。 - zhangxaochen

1

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

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

华为 Push Kit 发送数据消息后,不会在手机上显示消息。相反,消息将传输到开发者的应用程序中,并且应用程序负责解析和显示消息。

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


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)]中。这肯定会起作用。


尽管官方文档不是最新的,但在 GitHub 上的源代码和自述文件中包含了新的方法。感谢提供信息。 :) - Radu Iamandi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接