Retrieve notification values from a notification in system tray android firebase FCM

how to handle notification when app in background in firebase android
how to handle notification when app in foreground in firebase android
firebase cloud messaging notification not received by device android
how to handle notifications from the system tray android
fcm high priority data message
firebase notification when app is killed android
firebase notification android
firebase cloud messaging custom data

I am using firebase to send notifications to my app. I want to save the notifications received on each device in a local database. The problem is, when the app is in the background and a notification is received, the method onMessageReceived() won't be called, but instead the notification would be added to the system tray as per documentation. The documentation also mentions the following for receiving a notification when the app is in background:

In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

So, when I'm trying to access the extras in the intent, the keys of the notification are not found. My code in MainActivity.java:

if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Toast.makeText(this, "Key: " + key + " Value: " + value, Toast.LENGTH_LONG).show();
        }
    }

The notification we are sending from the server:

{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
  "body" : "great match!",
  "title" : "Portugal vs. Denmark",
  "icon" : "myicon"
  }
}

Anyone has an idea of how I can retrieve the values inside the notification?

This is the intended behavior (as you've seen in the documentation). If you're using a notification-only message payload (which you are), onMessageReceived() will only be called if your app is in foreground, if not, the System Tray will be receiving it.

The data being referred to in (emphasis mine):

In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

is the data message payload (See Message Types).

You'll have to send a payload with both notification and data. Like so:

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
    },
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
  }
}

Also see my answer here.

Receive messages in an Android app, In these cases, the notification is delivered to the device's system tray, and the data There is a firebase service running by your system which works for all app same issue when backend team introduced notification tag in the fcm payload. Handle notification messages in a backgrounded app. When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

All you have to do is update your notification push format.

From:

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
   }
}

To

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "data" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
   }
}

Explanation:

There is a firebase service running by your system which works for all app and handle all push notification if it contains the "notification" tag. but if contain the "data" tag only it just handover the task to the service created by the app where you can do anything.In your case show user notification and save it to the local db. For more you read here Documentation : Handle Messages.

Topic messaging on Android, Android. Sign in with a pre-built UI · Get Started with the Firebase SDK · Manage If you want foregrounded apps to receive notification messages or data messages, In this case, the notification is delivered to the device's system tray. For insight into message delivery to your app, see the FCM reporting dashboard,  When you send a message with a notification payload, FCM works exactly as you described. When then app is in the background, it will display a notification for you. When in the foreground, it will not. Please note that data and notification payloads behave differently, as described in the documentation.

It's not a simple or clean solution, but you could use a NotificationListenerService to receive a callback for each notification. A further limitation is that it supports only API 18 and above.

Handle FCM messages on Android, To subscribe to a topic, the client app calls Firebase Cloud Messaging case, the notification is delivered to the device's system tray, and the data payload is  If your app is in the background, then the Firebase SDK will automatically process the message and display it as a notification in the device’s system tray. Since the Android system builds the

Add this to manifest file

        <intent-filter>
            <action android:name=".MainActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

How to handle FCM notification in backgrounded Android applications, When using an FCM notification message, the system handles showing the notification on the app content with the FCM payload data (Max 4KB) or fetch content from app server. Notification delivered to System Tray. 🚀See output below and notice that Internet/Wi-Fi is OFF still at exactly 02:12 pm I’m getting a notification on the system tray 😃. At exact 02:12, Notification is displayed though Data is OFF.

Push Notifications with Firebase Cloud Messaging, After setting up FCM in Android application, you can use Firebase console to send notifications. When notification delivers to the device's system tray and you can not handle notification with Data messages let developers send up to 4KB custom key-value pairs. A button that says 'Get it on, Google Play', and if clicked. The FCM works fine when the app is in foreground and a push comes, but when the app is in background and the system tray notification is clicked , it just opens up the app without passing any intent extras. I have added the click_action in Notification Payload from Advanced option of Firebase Console

firebase_messaging, Your server informs Firebase that a notification has to be sent. Then Firebase does the work behind the scenes to get the notification published. 1 2 3 4 5 6 7 8 <meta-data android:name="com.google.firebase. For an unified notifications system, we use messages that have a data-only payload. The notification and data is sent through the Firebase web console. When the notification is tapped while the app is in background, but not killed, the app is re-opened normally and the intent extras doesn't have the payload. The FirebaseMessagingService onMessageReceived() was never called.

[Android][Notification] Notification goes to tray silently when app in , Flutter plugin for Firebase Cloud Messaging, a cross-platform messaging Note: If this section is not completed you will get an error like this: Notification on Android, onMessage, Notification is delivered to system tray. It is possible to include additional data in notification messages by adding them to  When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

Comments
  • is there a way to handle the notification when the "notification"payload is there, this is because the system tray doesn't do a lot of things that are recommended, like grouping notifications, or in case of a messaging app, showing the avatar of the person that sends the notification
  • @Andrea I haven't tried overriding it. When I need custom handling for my notifications, I just send a data-only message payload, then handle it in onMessageReceived()
  • But can you retrieve the notification's title using getBody() and get Title() message in case of a data payload? If it's a purely data payload, we need to create the notification in our onMessageReceived method correct?
  • Yes, you can bind the json response to a Model class and can create getter and setter for each property. :)