7

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());
}
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
Goran_1992
  • 103
  • 1
  • 2
  • 9

3 Answers3

4

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.

KRUPEN GHETIYA
  • 211
  • 2
  • 6
  • Have you noticed that sendNotification(RemoteMessage remoteMessage) work with RemoteMessage? But remoteMessage.getNotification().getBody() actually returns String. You gave bad advice. – Yazon2006 Jun 15 '16 at 15:02
2

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.

Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
  • 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? – Goran_1992 Jun 06 '16 at 07:10
  • 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. – Arthur Thompson Jun 07 '16 at 19:39
-1

Intent intent = new Intent(this, "your class"); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
           PendingIntent.FLAG_ONE_SHOT);

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
        notificationManager.createNotificationChannel(notificationChannel);
        builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
    } else {
        builder = new NotificationCompat.Builder(getApplicationContext());
    }

    builder = builder
            .setSmallIcon(R.mipmap.ic_app_icon)
            .setColor(ContextCompat.getColor(getApplicationContext(), R.color.picker_button_background_innactive))
            .setContentTitle((messageBody))
            .setTicker(title)
            .setContentText(message_body)
            .setDefaults(Notification.DEFAULT_ALL)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    notificationManager.notify(1, builder.build());
VISALIG
  • 27
  • 4