Firebase notifications in the foreground
EDIT:
I just noticed that onMessageReceived() of FirebaseMessagingService is not invoked(as of now) when you send notifications from console and your app is in foreground. One solution is you can use Firebase APIs to send push notifications and it will invoke onMessageReceived() regardless of app being in foreground or background.
If you use "notification" body to send push notification i.e.
notification: {
title: "notification title",
icon: "ic_launcher",
color: "#4E2AA5",
body: "notification body"
}
To retrieve this in onMessageReceived(), use something like this:
String color = remoteMessage.getNotification().getColor();
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
String icon = remoteMessage.getNotification().getIcon();
If your app is in background, Firebase will display a notification corresponding to the data you set in push and will also invoke onMessageReceived(). Thus you will see 2 notifications if you have also written code to show custom notification in onMessageReceived().
To work around this, you can use some "data" key like this:
data: {
title: "notification title",
body: "notification body"
}
and to retrieve it in onMessageReceived(), use something like this:
Map<String, String> map = remoteMessage.getData();
for (Map.Entry<String, String> entry : map.entrySet()) {
Log.d(TAG, entry.getKey() + "/" + entry.getValue());
}
You can then build your own custom notification and display it from onMessageReceived() only that notification will show up.
P.S. Please vote up if it helps. I am new to stackoverflow.
Like you said you are receiving the message since you are seeing the log messages in the console so you have handled the FCM part properly and the issue is likely with your creation of the notification.
If you look at your notification creation code you are missing a couple of the required elements to create a notification on Android. From the docs:
A Notification object must contain the following:
- A small icon, set by setSmallIcon()
- A title, set by setContentTitle()
- Detail text, set by setContentText()
So I think if you add those items to your notification creation you should see the notification in the notification area.