从系统托盘android firebase FCM中的通知中检索通知值 [英] Retrieve notification values from a notification in system tray android firebase FCM

查看:154
本文介绍了从系统托盘android firebase FCM中的通知中检索通知值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase向我的应用发送通知。我想将每个设备上收到的通知保存在本地数据库中。问题是,当应用程序在后台并收到通知时,方法 onMessageReceived()不会被调用,而是通知将被添加到系统托盘按文档。该文档还提到了当应用程序处于后台时接收通知的以下情况:


在这些情况下,通知会传递到设备系统托盘,并且数据有效载荷以您的启动程序Activity的意图附加内容交付。


所以,当我尝试要访问意图中的附加内容,找不到通知的键。
我的代码位于 MainActivity.java

  if getIntent()。getExtras()!= null){
for(String key:getIntent()。getExtras()。keySet()){
Object value = getIntent()。getExtras()。get (键);
Toast.makeText(this,Key:+ key +Value:+ value,Toast.LENGTH_LONG).show();


我们从服务器发送的通知: p>

  {
to:bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...,
notification:{
身体:伟大的比赛!,
冠军:葡萄牙对丹麦,
图标:myicon
}
}

任何人都知道如何检索通知内的值?

解决方案

这是预期行为(正如您在 documentation )。如果您使用通知 - only 消息负载(您是), onMessageReceived()前台中,则只会调用$ c>,否则系统托盘将收到它。


$ b
lockquote>

在这些情况下,通知将传递到设备的系统托盘,并且 data 有效内容将以您启动程序Activity的目的附加内容的形式传递。


data 消息有效负载(请参阅消息类型)。



您必须使用通知数据。像这样:

  {
to:bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1 ...,
通知:{
body:很棒的比赛!,
title:Portugal vs. Denmark,
icon:myicon
},
data:{
body:很棒的比赛!,
title:Portugal vs. Denmark,
icon:myicon


另见我的答案在这里


I am using firebase to send notifications to my app. I want to save the notifications received on each device in a local database. The problem is, when the app is in the background and a notification is received, the method onMessageReceived() won't be called, but instead the notification would be added to the system tray as per documentation. The documentation also mentions the following for receiving a notification when the app is in background:

In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

So, when I'm trying to access the extras in the intent, the keys of the notification are not found. My code in MainActivity.java:

if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Toast.makeText(this, "Key: " + key + " Value: " + value, Toast.LENGTH_LONG).show();
        }
    }

The notification we are sending from the server:

{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
  "body" : "great match!",
  "title" : "Portugal vs. Denmark",
  "icon" : "myicon"
  }
}

Anyone has an idea of how I can retrieve the values inside the notification?

解决方案

This is the intended behavior (as you've seen in the documentation). If you're using a notification-only message payload (which you are), onMessageReceived() will only be called if your app is in foreground, if not, the System Tray will be receiving it.

The data being referred to in (emphasis mine):

In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

is the data message payload (See Message Types).

You'll have to send a payload with both notification and data. Like so:

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
    },
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
  }
}

Also see my answer here.

这篇关于从系统托盘android firebase FCM中的通知中检索通知值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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