当应用程序是后台FCM时如何检索通知消息intent.getExtras() [英] How to retrieve notification message intent.getExtras() when app is background FCM

查看:378
本文介绍了当应用程序是后台FCM时如何检索通知消息intent.getExtras()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FCM进行简单通知

当应用处于前台时,一切工作正常。我收到一个通知加上 onMessageReceived 方法中的数据信息。



但是当应用程序在后台时,我正在系统托盘中收到通知。当我点击控件时,它会转到主要活动。当我解析 intent.getExtras(); 时,我只得到这个关键数据 - google.sent_time 来自 google.message_id collapse_key



如何从 intent.getExtras()

中获取系统托盘中显示的通知消息标题和消息

我正在使用FCM控制台发送通知,我没有专门的服务器来执行此操作。



接收消息:

  final Bunds extras = intent.getExtras(); 
final Set< String> keySet = extras.keySet();
final迭代器< String> iterator = keySet.iterator();
while(iterator.hasNext()){
final String key = iterator.next();
final Object o = extras.get(key);
System.out.println(key +:+ o);


解决方案

href =https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages =noreferrer>处理Android FCM文档信息,如果您发送的有效载荷同时有通知和数据,它将被分开处理。通知部分将通过通知托盘处理,而数据部分将在额外的意图。



AFAIK,没有办法获得通知有效载荷当应用程序在后台(通常由通知托盘处理)。但是,您可以做的是将自定义键值对添加到您的数据负载中,如下所示:

  {
data:{
notification_title:title here,
notification_message:message here
}
}
notification_title
的数据值>

notification_message 与您在通知有效内容中设置的内容相同。然后就像往常一样从Intent extras中检索它。


I am using FCM for Simple notification

When the app is in foreground, everything working properly. I am getting a notification plus the data message inside the onMessageReceived method.

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.

How to get the notification message title and Message which is visible in system tray from intent.getExtras()?

I am using FCM console for sending notification I don't have my dedicated server to do this.

Code for receiving the message:

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); 
} 

解决方案

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, 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"
     }
}

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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