5

Is it possible to show popup for firebase system notification (when app is in background) on the top of screen? I use setPriority(Notification.PRIORITY_MAX) for this to show notifications from onMessageReceived in foreground.

I use both notification and data fields, so I don't have access to received notification in foreground. I've tried setting priority to high and using notification-only pushes - nothing helps.

prcu
  • 903
  • 1
  • 10
  • 23

2 Answers2

8

It is only possible to handle the message in onMessageReceived when your app is in background when using a data only payload.

When using both notification and data in your payload, the expected behavior is that the Android System will handle the notification when the app is in background, regardless of what is the value of your priority.

See the official docs on Handling Android Messages for more details.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • 1
    Thanks! I've left only `data` field in payload and showed custom notification with `Notification.Builder` from `onMessageReceived`. First I thought it would be unnecessary code to show notification myself while FCM can do it on its own, but later I found that this way is much more flexible. – prcu Nov 02 '17 at 10:26
1

If you sent data field only in notification, you can handle notification inside the below function

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " +remoteMessage.getNotification().getBody());
    }

}

Please note that don't include notification field in that request

Sangeet Suresh
  • 2,527
  • 1
  • 18
  • 19
  • It's not called in while app is in background. From docs: "When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification." – prcu Feb 18 '17 at 14:09
  • Send only data field. Not notification field. If both are send, FCM will automatically show notification. If you send only data field, it will be received in onMessageReceived() – Sangeet Suresh Feb 18 '17 at 14:44