Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM onMessageReceived Returns blank message and title when the app is running

As you wrote in the title , when the application is closed it's working well and the onMessageReceived gets the message body and the title but if the app is in the foreground mode (running mode) the notifications can be sent but without message and title !

What to do please ?

The Code :

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String title = remoteMessage.getData().get("title");
        String message = remoteMessage.getData().get("body");
        sendNotification(title,message);
    }

I know there are alot of issues published here in Stackoverflow but I've tried their solutions but the problem still exist !

like image 205
صارحني - Sarihny Avatar asked Jul 10 '17 19:07

صارحني - Sarihny


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 you handle notifications when an app is in the background in Firebase Web?

There are two types of messages in FCM (Firebase Cloud Messaging): Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground. Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed.

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.


1 Answers

Replace

String title = remoteMessage.getData().get("title");
String message = remoteMessage.getData().get("body);

By

String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();

What you used is for data payload that you set as key-value pair while sending a notification. You may find input fields for them under Advanced Options section as Custom Data in firebase notification console.

like image 100
Birendra Singh Avatar answered Nov 09 '22 02:11

Birendra Singh