我正在使用 FCM 进行简单通知
I am using FCM for Simple notification
当应用程序处于前台时,一切正常.我在 onMessageReceived
方法中收到通知和数据消息.
When the app is in foreground, everything working properly. I am getting a notification plus the data message inside the onMessageReceived
method.
但是当应用程序处于后台时,我会在系统托盘中收到通知.当我单击控件时,它会转到主要活动.当我解析 intent.getExtras();
时,我只得到这个关键数据 - google.sent_time
、from
、google.message_id
, collapse_key
.
But when app is in background, I am getting notification in system tray. And when I click on the control, it goes to the main activity. And When I parse intent.getExtras();
, I am getting only this key data - google.sent_time
, from
, google.message_id
, collapse_key
.
如何从intent.getExtras()
获取系统托盘中可见的通知消息标题和消息?
How to get the notification message title and Message which is visible in system tray from intent.getExtras()
?
我正在使用 FCM 控制台发送通知我没有专门的服务器来执行此操作.
I am using FCM console for sending notification I don't have my dedicated server to do this.
接收消息的代码:
final Bundle extras = intent.getExtras();
final Set<String> keySet = extras.keySet();
final Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
final String key = iterator.next();
final Object o = extras.get(key);
System.out.println(key + ":" + o);
}
如 Handling Messages for Android FCM docs,如果你发送的payload既有通知又有数据,会单独处理.通知部分将由通知托盘处理,而数据部分将在 Intent 的附加部分中.
As seen in the Handling Messages for Android FCM docs, if the payload you sent has both notification and data, it will be handled separately. The notification part will be handled by the Notification Tray, while the data part will be in the extras of the intent.
AFAIK,当应用程序处于后台时(始终由通知托盘处理),无法获取通知负载.但是,您可以将自定义键值对添加到数据负载中,如下所示:
AFAIK, there is no way to get the notification payload when the app is in background (always handled by the Notification Tray). However, what you could do is add custom key-value pairs to your data payload instead, like so:
{
"data": {
"notification_title": "title here",
"notification_message": "message here"
}
}
当然,您必须确保 notification_title
和 notification_message
的数据值与您在通知负载中设置的值相同.然后像往常一样从 Intent extras 中检索它.
Of course you'll have to make sure that the data value for notification_title
and notification_message
is the same as to what you set it in the notification payload. Then just retrieve it from the Intent extras like usual.
这篇关于当应用程序是后台 FCM 时如何检索通知消息 intent.getExtras()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!