Firebase handles notifications differently when the app is in background (killed process) and when in foreground (active).
When your app is in the background, notification messages are displayed in the system tray, and onMessageReceived is not called. For notification messages with a data payload, the notification message is displayed in the system tray, and the data that was included with the notification message can be retrieved from the intent launched when the user taps on the notification. [1]
If the app is in background the service will trigger the notification by default with the title
and body
in the notification and as mentioned onMessageReceived
method will not be trigger. Instead, the the click will open the activity
from the Manifest.xml
marked with:
<intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter>
From this point forward you can get your data
within this activity's intent
:
if (getIntent() != null && getIntent().getExtras() != null) { String customString = (String) getIntent().getExtras().get("myStringData"); Integer customInteger = (Integer) getIntent().getExtras().get("myIntData"); }
Setting icon and icon background color in this case can be done from within Manifest.xml
[2]:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/your_drawable_icon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/your_color" />
source 1: FCM background handling
source 2: FCM github repository