MahmoudTarek MahmoudTarek - 27 days ago 5x
Android Question

Firebase notification doesn't work properly if application is in background

I have an FirebaseMessagingService class for Firebase:

public class FcmMessagingService extends FirebaseMessagingService {

public void onMessageReceived(RemoteMessage remoteMessage) {
String message = remoteMessage.getNotification().getBody();

Map<String, String> params = remoteMessage.getData();

Bitmap remote_picture = null;
remote_picture = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.logo);

intent = new Intent(this, HomeDrawer.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle("Notification Title");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification();
noti =;
//notificationBuilder.setVibrate(new long[] { 1000, 1000});
noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;


If the application is in background, (not actively open, but in the background, not closed) the notification smallIcon and LargeIcon doesn't appear, the default ones for android appears instead, also if I changed

intent = new Intent(this, HomeDrawer.class);

to any other activity class, it doesn't open the class I specified and opens the last class I left open in the background.

What am I doing wrong? SmallIcon and LargeIcon are not working properly if the background. However, notification message appears fine and also remoteMessage.getData(); gets the data correctly, it's all about the behavior of icons and intent class.

Thanks in advance.


That happens because notifications are handled differently if your app is in background.

If app is running in foreground then yours FcmMessagingService will be executed and you are handling notifications on your own.

If app is not running, notifications are handled by the android system itseld.FcmMessagingService is never executed. For this case you have to properly setup icon on the server!

I believe this behaviour happens since introduction of doze mechanism.

The notification sent from server should look something like this

"notification" : {
  "body" : "great match!",
  "title" : "Portugal vs. Denmark",
  "icon" : "myicon", // the name of the resource
  "sound" : "mySound"