1

I have a scenario when i don't want android system to display push notification if app is in foreground.

Instead i want to display some view or launch activity in the app.

This is my code.

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
  
       //...
       //...
      sendNotification(remoteMessage.getNotification(), subtitle);

}

If i comment out sendNotification()method, it does not display notification on both background & foreground cases.

dev90
  • 7,187
  • 15
  • 80
  • 153
  • 2
    Add Lifecycle support and check whether your activity is on Resumed or on Paused state and display notification if you're app is running in background (activity is onPause state) – M D Jul 02 '20 at 16:56
  • @MD : Where to add Lifecycle support, in class that is extending from FirebaseMessagingService? – dev90 Jul 02 '20 at 17:02
  • No at your app level class or base activity – M D Jul 02 '20 at 17:08
  • Please checkout this https://stackoverflow.com/questions/48965528/sending-a-push-notification-only-when-app-is-in-background – aslamhossin Jul 02 '20 at 17:14

1 Answers1

0

There are two types of messages data messages and notification messages. Data messages are handled here in onMessageReceived whether the app is in the foreground or background. Data messages are the type traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app is in the foreground. When the app is in the background an automatically generated notification is displayed. When the user taps on the notification they are returned to the app. Messages containing both notification and data payloads are treated as notification messages. The Firebase console always sends notification messages.

Thought you can only handle notification when app is in foreground.

Solution:- Tell your server that app is in foreground and background and they will send notification when app is in foreground only

Quick learner
  • 10,632
  • 4
  • 45
  • 55