Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get custom data from android firebase notification?

I'm trying to implement firebase notifications. But I have trouble finding any documentation on how to retrieve custom data from firebase notification.

Firebase console

But in in code how to get the custom key.

enter image description here

I'm using FirebaseMessagingService.onMessageReceived to get the message data.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
like image 295
Isuru Avatar asked Jul 22 '16 12:07

Isuru


People also ask

What is onMessageReceived?

onMessageReceived. Notification: system tray. Data: in extras of the intent. For more information about message types, see Notifications and data messages. For all messages where onMessageReceived is provided, your service should handle any message within 20 seconds of receipt (10 seconds on Android Marshmallow).

What is Firebasemessagingservice?

Firebase Cloud Messaging Platform (formerly named as GCM) is a free mobile notification service by Google that enables (third-party) app developers to send notifications from GCM (Google Cloud Messaging) servers to their users.


1 Answers

You can check your custom data using:

for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
    String key = entry.getKey();
    String value = entry.getValue();
    Log.d(TAG, "key, " + key + " value " + value);
}

To get specific key:

String value = remoteMessage.getData().get("<YOUR_KEY>");
like image 56
Chintan Soni Avatar answered Sep 27 '22 19:09

Chintan Soni