问题描述
我正在尝试读取通知的有效内容,但是该事件不会触发。它适用于数据消息,但似乎不会注意到通知。
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,因此不会触发。
解决方法
未日期:
根据@Senthil Ssk的答案,我将答案分为两部分:
- 如果应用程序在前台运行,则通知消息将透明地传输到应用程序。当您的应用服务器发送通知消息时,该消息将被处理以供应用显示。可以通过设置 message.android.notification.foreground_show 字段来实现此功能。
- 如果该应用程序处于脱机状态,并且通过单击推送通知打开,则可以通过调用HmsPush.getInitialNotification来检索远程消息。请参阅docs。
这是HMS Core Push Kit和FCM之间的区别。使用HMS Core Push Kit时,默认情况下,通知消息将传递到系统托盘,数据消息将传递到onMessageReceived方法。
此外,HMS Core Push Kit提供了在应用程序处于前台时将通知消息发送到onMessageReceived方法的功能。解决方案是,使用HMS Core Push Kit时,可以在消息> android > 通知中设置 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。
, onMessageReceived()
仅用于数据消息。
查看Push Kit FAQ的这一部分:
,[Q1]数据消息和通知消息之间有什么区别?
HUAWEI Push Kit将数据消息发送到手机后,不会显示数据消息。而是将消息转移到开发人员的应用程序,该应用程序负责解析和显示消息。
设备收到通知消息后,系统直接在NC中显示该消息。用户可以点击通知消息以触发相应的操作,例如打开应用程序,网页或应用程序中的特定页面。
对于通知消息,请使用以下代码段。
如果通过推送通知单击打开应用程序,则可以通过调用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)]中。这肯定可以工作。