Firebase notifications in the foreground

13,307

Solution 1

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.

Solution 2

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.

Share:
13,307
Goran_1992
Author by

Goran_1992

Updated on June 11, 2022

Comments

  • Goran_1992
    Goran_1992 almost 2 years

    I am having problem with FireBase push notifications. When my app is in the background, notifications are coming, but when my app is in the foreground i don't receive notifications, but in console that notification is displayed, that means that notification is here but it doesn't show in notification bar . Could you help me?

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("OnMessage", "Received");
        super.onMessageReceived(remoteMessage);
        Log.d(TAG, "From " + remoteMessage.getFrom());
        Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
        String aa=remoteMessage.toString();
        Intent intent=new Intent(MyFirebaseMessagingService.this, MainActivity.class);
        intent.putExtra("msg", aa);
        sendNotification(remoteMessage);
        NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setContentText(remoteMessage.getNotification().getBody());
    
        // remoteMessage.getNotification().getBody();
    
    }
    
    
    private void sendNotification(RemoteMessage remoteMessage) {
    
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
    
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    
  • Goran_1992
    Goran_1992 almost 8 years
    Yeah, that was a problem, thanks mate. Just another question, who to set setContentTitle() when my backend send me a notification and in json sets title?
  • Arthur Thompson
    Arthur Thompson almost 8 years
    Notifications displayed via a notification message (json), is totally separate from you creating and displaying your notification in code like you are doing here. If you use a notification message and your app is in the background then it will be displayed given the specified json. Otherwise you will get the onMessageReceived callback and it is up to your code to display a notification if that is what you want to happen.
  • Yazon2006
    Yazon2006 almost 8 years
    Have you noticed that sendNotification(RemoteMessage remoteMessage) work with RemoteMessage? But remoteMessage.getNotification().getBody() actually returns String. You gave bad advice.
  • VISALIG
    VISALIG almost 5 years
    Working for fine for me