Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase notifications in the foreground

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());
}
like image 526
Goran_1992 Avatar asked Jun 02 '16 11:06

Goran_1992


2 Answers

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 image 200
KRUPEN GHETIYA Avatar answered Oct 24 '22 06:10

KRUPEN GHETIYA


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.

like image 39
Arthur Thompson Avatar answered Oct 24 '22 06:10

Arthur Thompson