如何让“onMessageReceived"触发 React Native 中的通知(HMS 推送工具包)? [英] How do I get 'onMessageReceived' to trigger on Notification (HMS Push Kit) in React Native?

查看:112
本文介绍了如何让“onMessageReceived"触发 React Native 中的通知(HMS 推送工具包)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to read the payload of my notifications, but the event won't trigger. It works fine for Data messages, but doesn't seem to notice notifications.

AndroidManifest:

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

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

build.gradle

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

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

I think that message.getNotification() is always null, thus not triggering.

推荐答案

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

For Notification Message use the below code snippet.

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

If the app is opened by a push notification click, the remote message can be retrieved by calling HmsPush.getInitialNotification If the app is not opened by a push notification click, it returns null.

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

这是链接

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

This is included in latest plugin release [Cordova Push Plugin 5.0.2 (2020-09-30)]. This will work for sure.

这篇关于如何让“onMessageReceived"触发 React Native 中的通知(HMS 推送工具包)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆