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

时间:2019-03-04 10:46:33

标签: android firebase android-intent push-notification firebase-cloud-messaging

我从服务器收到这种格式的推送通知。

{
                title: messageTitle,
                body: messageBody,
               subtitle: messageSubtitle
}

在我的android项目中,我使用以下代码读取了通知。

remoteMessage.getNotification().getTitle();
remoteMessage.getNotification().getBody(); 

以上两行给了我TitleBody,但我无法理解如何读取subtitle属性。

3 个答案:

答案 0 :(得分:1)

// 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()

        }`

答案 1 :(得分:1)

标准通知格式不包含字幕,因此没有获取方法,要获取字幕消息,请使用 data 键。

  

标准通知格式

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}

如果需要字幕,请在数据字段中添加字幕,然后使用getData()方法获取字幕

  

自定义字幕通知

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "subtitle" : "Mario",
      "subHeading" : "PortugalVSDenmark"
    }
  }
}

答案 2 :(得分:0)

要接收带有自定义数据的FCM通知,我们需要按以下方式检查数据有效负载:-

  • 在后台,应用程序会在 通知托盘,仅在用户使用时处理数据有效载荷 点击通知。

  • 在前台时,您的应用会收到一个 具有两个有效负载的消息对象。

    在情况2中,您只需检查以下内容即可:-

    String customData = remoteMessage.getData().get("customData");

对于第一种情况,请参考答案;-

handle notification with custom data

希望这会有所帮助。

相关问题