从系统托盘中的通知中检索通知值 android firebase FCM
Retrieve notification values from a notification in system tray android firebase FCM
我正在使用 firebase 向我的应用程序发送通知。我想将每台设备收到的通知保存在本地数据库中。问题是,当应用程序在后台并收到通知时,不会调用方法 onMessageReceived()
,而是根据文档将通知添加到系统托盘。该文档还提到了以下内容,用于在应用程序处于后台时接收通知:
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.
因此,当我尝试访问 Intent 中的附加功能时,找不到通知的键。
我的代码在 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();
}
}
我们从服务器发送的通知:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
有人知道如何检索通知中的值吗?
这不是一个简单或干净的解决方案,但您可以使用 NotificationListenerService 接收每个通知的回调。进一步的限制是它仅支持 API 18 及以上。
这是预期的行为(如您在 documentation 中所见)。如果您使用的是 notification
- 仅 消息负载(您是),则只有当您的应用位于 中时才会调用 onMessageReceived()
前景,如果没有,系统托盘将接收它。
data
(重点是我的):
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.
是 data
消息负载(参见 Message Types)。
您必须发送包含 notification
和 data
的负载。像这样:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data": {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
另见 。
您只需更新您的通知推送格式即可。
发件人:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
至
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
解释:
您的系统有一个 firebase 服务 运行,它适用于所有应用程序并处理所有推送通知,如果它包含 "notification" 标签。但如果仅包含 "data" 标签,它只是将任务移交给应用程序创建的服务,您可以在其中执行 anything.In 您的案例显示用户通知并保存到本地数据库。欲了解更多信息,请阅读此处 Documentation : Handle Messages。
将此添加到清单文件
<intent-filter>
<action android:name=".MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
我正在使用 firebase 向我的应用程序发送通知。我想将每台设备收到的通知保存在本地数据库中。问题是,当应用程序在后台并收到通知时,不会调用方法 onMessageReceived()
,而是根据文档将通知添加到系统托盘。该文档还提到了以下内容,用于在应用程序处于后台时接收通知:
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.
因此,当我尝试访问 Intent 中的附加功能时,找不到通知的键。
我的代码在 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();
}
}
我们从服务器发送的通知:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
有人知道如何检索通知中的值吗?
这不是一个简单或干净的解决方案,但您可以使用 NotificationListenerService 接收每个通知的回调。进一步的限制是它仅支持 API 18 及以上。
这是预期的行为(如您在 documentation 中所见)。如果您使用的是 notification
- 仅 消息负载(您是),则只有当您的应用位于 中时才会调用 onMessageReceived()
前景,如果没有,系统托盘将接收它。
data
(重点是我的):
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.
是 data
消息负载(参见 Message Types)。
您必须发送包含 notification
和 data
的负载。像这样:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data": {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
另见
您只需更新您的通知推送格式即可。
发件人:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
至
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
解释:
您的系统有一个 firebase 服务 运行,它适用于所有应用程序并处理所有推送通知,如果它包含 "notification" 标签。但如果仅包含 "data" 标签,它只是将任务移交给应用程序创建的服务,您可以在其中执行 anything.In 您的案例显示用户通知并保存到本地数据库。欲了解更多信息,请阅读此处 Documentation : Handle Messages。
将此添加到清单文件
<intent-filter>
<action android:name=".MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>