Push notification show on background and foreground

I already receive notifications, but I noticed the message also notify even the app is open. How can I handle this properly? Thanks!

1 Like

Hi @trck,

You need to use activity lifecycle to handle the push notification in foreground.
You need to add the below methods on your application class

@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() { 
       isBackground=false;        
    }
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onMoveToBackground() {
    isBackground = true;
}

Then you need to use “isBackground” variable in OnMessageRecieved() of your MyfirebaseMessagingService.class to check if the app is in background or not.

Please check the below link