Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM for android: popup system notification when app is in background

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.

like image 750
prcu Avatar asked Feb 18 '17 12:02

prcu


People also ask

How do I get notification data when an app is in the background?

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).

How do I get notifications when an app is closed Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you handle payload notifications?

This notification payload are used when you want to automactically showing notification on the system tray when your app is in the background. To get notification data when your app in the background, you should add click_action inside notification payload.

What is difference between GCM and FCM in Android?

FCM is a cloud platform that provides messages and push notifications for operating systems- ios and Android, and websites as well. Google Cloud Messaging is a messaging service that enables the message transfer from server to clients apps.


2 Answers

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.

like image 152
AL. Avatar answered Oct 03 '22 20:10

AL.


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

like image 38
Sangeet Suresh Avatar answered Oct 01 '22 20:10

Sangeet Suresh