[英]How to get subtitle from firebase push notification
I am getting push notification in this format from server. 我正在从服务器获取这种格式的推送通知。
{
title: messageTitle,
body: messageBody,
subtitle: messageSubtitle
}
In my android project i read notification by using following code. 在我的android项目中,我使用以下代码阅读通知。
remoteMessage.getNotification().getTitle();
remoteMessage.getNotification().getBody();
The above 2 lines gives me Title
and Body
, but i am unable to understand how to read subtitle
property. 上面的2行给了我
Title
和Body
,但是我不明白如何阅读subtitle
属性。
//remoteMessage is reference of RemoteMessage // remoteMessage是RemoteMessage的引用
`val data = remoteMessage.data
if(data.isNotEmpty())
{
if (data.containsKey("title"))
title = data["title"].toString()
if (data.containsKey("body"))
body = data["path"].toString()
if(data.containsKey("subtitle"))
subtitle = data["subtitle"].toString()
if(data.containsKey("notifyId"))
notifyId = data["notifyId"]?.toInt()
}`
Standard notification format does not include subtitle hence there is no method to fetch it, to get a subtitle message please use data key. 标准通知格式不包含字幕,因此没有获取字幕的方法,要获取字幕消息,请使用数据键。
Standard Notification format
标准通知格式
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
if you want a subtitle please add subtitle to data field and then then fetch using getData() method 如果需要字幕,请在数据字段中添加字幕,然后使用getData()方法获取
Customized Notification for subtitle
字幕定制通知
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"subtitle" : "Mario",
"subHeading" : "PortugalVSDenmark"
}
}
}
To recieve FCM notifications with custom data, we need to check the data payloads as follows:- 要使用自定义数据接收FCM通知,我们需要按以下方式检查数据有效负载:
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification. 在后台时,应用程序会在通知托盘中接收通知有效载荷,并且仅在用户点击通知时处理数据有效载荷。
When in the foreground, your app receives a message object with both payloads available. 在前台时,您的应用程序会收到一个同时具有两个有效负载的消息对象。
In case 2 , you can simply check as below:- 在情况2中,您只需检查以下内容:
String customData = remoteMessage.getData().get("customData");
And for the first case please refer to the answer;- 对于第一种情况,请参考答案;-
handle notification with custom data 使用自定义数据处理通知
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.