2

I have an FirebaseMessagingService class for Firebase:

public class FcmMessagingService extends FirebaseMessagingService {

@Override
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);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("Notification Title");
    notificationBuilder.setContentText(message);
    notificationBuilder.setSmallIcon(R.drawable.ic_notif);
    notificationBuilder.setLargeIcon(remote_picture);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification noti = new Notification();
    noti = notificationBuilder.build();
    //notificationBuilder.setVibrate(new long[] { 1000, 1000});
    //notificationBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
    noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    notificationManager.notify(0,noti);

    super.onMessageReceived(remoteMessage);
}

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.

MahmutTariq
  • 217
  • 1
  • 4
  • 17

1 Answers1

1

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"
}
Adam Fręśko
  • 1,064
  • 9
  • 14