简体   繁体   English

如何从Firebase推送通知中获取字幕

[英]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行给了我TitleBody ,但是我不明白如何阅读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.

相关问题 收到Android(Firebase)推送时如何从通知获取数据? - How to get data from notification when receiving push for android (firebase)? 如何从Firebase获取推送通知上的图标? - How to fetch icon on Push Notification from Firebase? 如何从Firebase推送通知中获取数据并将其显示在Android活动中? - How to get the data from Firebase Push Notification and display it in your Android Activity? 如何发送推送通知Firebase - How to send Push Notification Firebase 如何从客户端向每个人发送简单的Firebase推送通知? - How to send a simple Firebase push notification to everyone from the client? 如何从节点服务器发送 firebase 推送通知 - How to send firebase push notification from node server 如何使用自定义点击操作从Firebase控制台发送推送通知 - How to send Push Notification With Custom Click Action From Firebase Console 如何使用谷歌应用脚​​本从 Firebase 发送推送通知? - How to send push notification from Firebase using google apps script? 如何以编程方式从状态栏 android 清除 firebase 推送通知 - How to clear firebase Push notification from status bar android programmatically Flutter - Firebase 使用 firebase 消息传递成功但未收到通知的推送通知 - Flutter - Firebase push notification using firebase messaging success but not get notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM