简体   繁体   English

当app是后台FCM时,如何检索通知消息intent.getExtras()

[英]How to retrieve notification message intent.getExtras() when app is background FCM

I am using FCM for Simple notification 我正在使用FCM进行简单通知

When the app is in foreground, everything working properly. 当应用程序处于前台时,一切正常。 I am getting a notification plus the data message inside the onMessageReceived method. 我在onMessageReceived方法中收到通知和数据消息。

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(); 当我解析intent.getExtras(); , I am getting only this key data - google.sent_time , from , google.message_id , collapse_key . ,我只获得这些关键数据 - google.sent_timefromgoogle.message_idcollapse_key

How to get the notification message title and Message which is visible in system tray from intent.getExtras() ? 如何从intent.getExtras()系统托盘中可见的通知消息标题和消息?

I am using FCM console for sending notification I don't have my dedicated server to do this. 我正在使用FCM控制台发送通知我没有我的专用服务器来执行此操作。

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. 处理Android FCM文档的消息中所示 ,如果您发送的有效负载同时包含通知和数据,则将单独处理。 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). AFAIK,当应用程序处于后台时,无法获取通知有效负载(始终由通知托盘处理)。 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. 当然,您必须确保notification_titlenotification_message的数据值与您在通知有效负载中设置的数据值相同。 Then just retrieve it from the Intent extras like usual. 然后就像往常一样从Intent extras中检索它。

Firebase Notfication will behave as Data Message when your app is in background or is killed. 当您的应用处于后台或被杀时,Firebase Notfication将充当数据消息。 In these Scenarios, if you want to retrieve your notification Message then you must define it in Key Value pair under 在这些方案中,如果要检索通知消息,则必须在“键值对”下对其进行定义

Advanced Option of FCM Console FCM控制台的高级选项

在此输入图像描述

and then retrieve this message by using this key in your activity which will be open by tabbing the Notification. 然后在您的活动中使用此来检索此消息,该将通过标记通知打开。

 if (getIntent().getExtras() != null) {
        Object value ;
        for (String key : getIntent().getExtras().keySet()) {
            if(key.equals("Message Key")) {
                 value = getIntent().getExtras().get(key); // value will represend your message body... Enjoy It
                 Log.d("NotificationTag" , key+"____" + value);
                }
          }
   } 

Just override the handleIntent() method of FirebaseMessagingService .class it will called in both foreground and background mode and here your can get and parse notification key and payload data 只需覆盖FirebaseMessagingService .class的handleIntent()方法,它将在前台和后台模式下调用,在这里您可以获取和解析通知密钥和有效负载数据

public void handleIntent(Intent intent)
    {
    String title = bundle.getString("gcm.notification.title");
     String body = bundle.getString("gcm.notification.body");
    }

Note: it will worked for play service 11 注意:它适用于游戏服务11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM