【问题标题】:How do I get 'onMessageReceived' to trigger on Notification (HMS Push Kit) in React Native?如何让“onMessageReceived”触发 React Native 中的通知(HMS 推送工具包)?
【发布时间】:2020-12-19 11:21:24
【问题描述】:

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

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

【问题讨论】:

    标签: android react-native huawei-mobile-services huawei-push-notification


    【解决方案1】:

    取消日期:

    根据@Senthil Ssk 的回答,我把答案分成了两部分:

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

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

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

    【讨论】:

    • 即使使用"foreground_show": false 也无法正常工作,通知总是出现在状态栏中
    • hi@deviant,EMUI 9.1.0及以上版本和HMS Core SDK 4.0及以上版本的设备支持该功能,详情请参考Docs
    • 我使用安装了 hms core 5.1.1 的谷歌设备。顺便说一句,它在华为设备上运行良好。
    • Sorry@deviant,经团队确认,此功能仅适用于华为设备。
    【解决方案2】:

    对于通知消息,请使用以下代码 sn-p。

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

    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)] 中。这肯定会奏效。

    【讨论】:

    • 尽管官方文档不是最新的,但GitHub 上的源代码和自述文件包含了新方法。谢谢你的信息。 :)
    【解决方案3】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      相关资源
      最近更新 更多