dev90
dev90

Reputation: 7549

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.

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.

Upvotes: 0

Views: 4648

Answers (3)

Kaveri
Kaveri

Reputation: 1088

To recieve FCM notifications with custom data, we need to check the data payloads as follows:-

  • 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:-

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

And for the first case please refer to the answer;-

handle notification with custom data

Hope this helps.

Upvotes: 0

Dewashish
Dewashish

Reputation: 151

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

Customized Notification for subtitle

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

Upvotes: 1

Rohit Singh
Rohit Singh

Reputation: 413

//remoteMessage is reference of 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()

        }`

Upvotes: 1

Related Questions