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 !
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With