I have an app that should show headsup notification while the app is in both foreground and background(not in history).
In foreground case , i acheived this by the folowing method.
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setFullScreenIntent(pendingIntent,true);
But in background, it always showing in the notification area.Can anybody tell how to acheive this in background(not in history)
i tried the below parameters for notification but not working.
{
"to" : "_Token_",
"priority":"high",
"data":
{
"title": "Test",
"text": "Fcm"
},
"notification":
{
"title": "Test",
"text": "Fcm"
}
}
Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.
The FCM backend receives the message request, generates a message ID and other metadata, and sends it to the platform specific transport layer. When the device is online, the message is sent via the platform-specific transport layer to the device. On the device, the client app receives the message or notification.
You should completely delete notification part from your Json and just use data ! That's the trick. I mean this:
{
"to" : "_Token_",
"priority":"high",
"data":
{
"title": "Test",
"text": "Fcm"
}
}
When you have notification tag in your Json, sometimes the library decide to handle the notifications itself. so when your app is in foreground it wont call your on message receiver and it show the notification itself.
Just remove the notification and always use data tag.
It works both when your app is in foreground/background or killed and stopped
Firebase allows notification customization when data messages are used. Data messages invokes onMessageReceived() method even when the app in backgroud so you create your custom notification.
You can take reference of this link to know more about data notifications Firebase notifications
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