1

We know that a Firebase notification sent using Cloud Messaging will be delivered to the notification tray and the data sent along with it would be in the extras of the intent if the app is in the background. See here.

However, I lose the data sent through the notification if the user does not click on the notification. I would like to know if there is a way I can obtain the data irrespective of whether the user clears the notification or interacts with it. I do not want to send two messages, namely one notification and one message with just the data payload which would be processed in the onMessageReceived method as firebase does not guarantee the order of delivery.

speedster01
  • 433
  • 3
  • 19

1 Answers1

0

What i mean is. if you get notification and you want to get the data you sent in notification then "remoateMessage.getData()" is the way to get the data no matter the app is in background or foreground.

Check this link for more info and read the official docs.

Official documentation for Notification in FCM.

In my case I'm getting data in json format from server and i'm converting it to json object like this...

JSONObject jsonObject = new JSONObject(remoteMessage.getData());

And if the notification data contains any title or description or body then i'm using this...

String title = jsonObject.getString("title");
String description = jsonObject.getString("description");
JSONObject bodyJsonObject = new JSONObject(jsonObject.get("body").toString());

And I no data is lost in this case. No matter the app is in background or foreground. provided the Internet is working to get the data from FCM.

We do it through api using C# language.. NotificationData is also a json object which we create at server..

JObject Bodydata = new JObject();
Bodydata["NotificationData"] = JToken.FromObject(NotificationData);

var data = new
{
    to = deviceId,
    data = new
    {
        body = Bodydata.ToString(), // this is the json which we create based on the data we need to send.
        title = title,
        description = description
    }
};
Amit Jangid
  • 2,741
  • 1
  • 22
  • 28
  • Nope, this does not work. According to the documentation, if my app is in the background and the notification has a data payload, the data is in the extras of the intent. This intent data is accessed only if the user taps the notification and opens an activity (which is also what is done in the link you shared). If the user clears the notification, I lose the intent data which is not what I want. And the onMessageReceived method is definitely not called. PS: I already checked if that method is called when the notification has a data payload, and as mentioned in the docs, it isn't. – speedster01 Oct 30 '18 at 04:47
  • check the answer i have edited it. It's definitely working for me. – Amit Jangid Oct 30 '18 at 04:58
  • In which method are you implementing the above code? – speedster01 Oct 30 '18 at 04:59
  • In onMessageReceived method of MyFirebaseMessagingService class. – Amit Jangid Oct 30 '18 at 05:00
  • Can you tell me the message format you are sending from the server? – speedster01 Oct 30 '18 at 05:02
  • what language are you using for server? – Amit Jangid Oct 30 '18 at 05:13
  • I'm using the Firebase Admin SDK. – speedster01 Oct 30 '18 at 05:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182763/discussion-between-amit-jangid-and-speedster01). – Amit Jangid Oct 30 '18 at 05:23
  • @speedster01 Did you able to get a solution? – Anand Aug 02 '20 at 06:55
  • @Anand unfortunately not. – speedster01 Aug 02 '20 at 08:47