Please note that there are NO basic issues with the client code like service registration or token issuance issue.
I am using the FCM REST API to send messaged from my app server. The requirement is to show a dialog when the push messages comes and app is in foreground or if the app is in background, just display a notification.
When I use the simple notification payload, everything works fine as expected. But as soon as my message contains a data payload too (in addition to the notification payload), none of the callbacks on my app gets called irrespective of whether the app is in foreground or background.
From documentation, it sounds like message with both kind of payloads should still work - FireBase Android doc.
To summarise this works -
{
"to":"verylongtoken",
"token":"verylongtoken",
"notification":{
"title":"title",
"body":"body"
},
"priority":"high"
}
But this does not -
{
"to":"verylongtoken",
"token":"verylongtoken",
"notification":{
"title":"title",
"body":"body"
},
"data":{
"message_type":"1"
},
"priority":"high"
}
Sounds really silly, but what am i missing ?
Related
I am working in a application that uses FCM notifications, when app is running (foreground or background) the notification receives but when i clear the app from my recent apps i do not receive any notification i am using FirebaseMessagingService. So i want to create a service that will keep alive myService which is extending FirebaseMessaging service even if app is not running or killed.
I found a reason for this problem
when you Release-APK is not from GPlay some mobile companies block services which want to auto start the app. you can do two think first download app from GPlay or in your phone settings disable battery optimization for your app or permit your app auto start.
I've tried everything - in my opinion theres no reliable solution to prevent any service from being killed.
The only way is to make sure to deliver the notification to the system tray not to the application.
There are 2 types of FCM notifications: Notification message and Data message.
Data messages are delivered to system tray and are always display - even if service is not running.
Notification message looks like:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
and triggers method OnMessageReceaved() of FirebaseMessagingService. Many devices (especially Huawei and Xiaomi) try to do everything to kill background services to prevent battery drain. So the FirebaseMessagingService isn't the best way to handle notifications.
Second type is
Data message:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
This type is handled by the system tray, so you don't need any of service running to get the notification. Its much more convenient method, but as far i know, it can't be achieved with the console.
You would probably need server API to send Data message.
Read this for more details.
When the application is killed. You need to push silent notification (payload without notitication property) to handle in the FirebaseMessagingService.
You can refer this link: https://firebase.google.com/docs/cloud-messaging/concept-options
You will need to use Data-Messages from FCM to have control of your notification payload, by using Data-Messages your app's onMessageReceived will be called even if your app is in background or killed, you will have to create a custom notification yourself to show your payload.
On the other hand I used Notification-Messages in FCM, they were delivered even if the app was killed.
I am triggering the notifications from the server using FCM using notification parameter looks like this:
"notification": { "title": "Test Title", "body": "Test Body" }
The notification's title & body is displayed as expected when the app is in background while the notification is being triggered. But it doesn't show the body if the app is in foreground while the notification is being sent.
The one triggered when the app was in foreground. The body is missing here.
The one triggered when the app was in background - expected behavior
They both were triggered using the same code/endpoint/data etc. but at different times. Only difference of these two is that one was triggered when the app was in background and the other wasn't.
Do you think this is related to Firebase API request or something to be configured on mobile app side?
Tested on Android 8.1.0
Seems like the notifications are not generated automatically when the app is in foreground and the mobile side is missing the body in the notification builder in onMessageReceived handler.
Nothing to be fixed for the backend side.
I'm with a problem here.
I have a logic with deep links in my push notification.
This is the FCM's json now:
{
"to": token_here",
"data": {
"DEEP_LINK_PATH": "STRING_HERE",
},
"notification": {
"title": "Teste",
"body": "Push de teste"
}
}
As you can see, all the logic for deep links is inside DATA, as usual.
When my app is open, everything looks fine.
But when its close, or in background, not working because its not passing inside my FirebaseMessagingService. Its seens like the notification is creating automatic by Android with base in NOTIFICATION body and title.
Is there a way to continue using this model of json and receive FirebaseMessagingService be call in background too?
Firebase Cloud Messaging has two basic types of messages:
Notifications messages
These messages are delivered to your application code when the app is active, and are handled by the system (Android in your case) when your app is not active. The system displays them as a notification in the system tray in that case.
Data messages
These messages are always delivered to your application code, no matter if the app is active or not. Your application code can either display a message when it receives a data message, or process it in any other way you see fit.
So it sounds like you're looking for a data message.
Also see:
the documentation on message types
the documentation on sending messages
I am using fcm to send notification to ios and android. The notification payload is like
{
"to":"APA91bFOrqYNhaZxlmAk-........",
"priority":"high",
"notification":{
"body":"this is game notification",
"title":"hi",
"icon" : "myIcon"
},
"data" : {
"name" : "XYYZ",
"company" : "ABC Pvt Ltd",
"Country":"India"
}
}
The Problem is most of the time data is not being received by the android device. So how can I know if the data was properly sent to the device? Is it FCM problem or android side problem?
There was no problem with FCM or Android, but it has certain limitations. Only in 2 scenarios app can receive push notifications.
The app should be in Foreground or Background to receive push notifications.
Still, the app can receive push notifications when it was killed or force closed, but the app should be whitelisted to start automatically in the background whenever it needs. Kindly check apps like Security360, Autostart Manager, Background App Manager, etc.
This answer is not a solution, but it is the workaround. Hope it helps.
I implemented push notification.
It works find when the app is in foreground (the notification triggers OnMessageReceived method).
But, when the app is in background, there's no heads up.
And, when the phone is in sleep mode, the notification don't turns on the screen.
How can I fix this
Check that you send data.
Your request body should be constant data field. E.q.:
{
"to": "/topics/fcm_globals",
"data": {
"field1": "value1",
"field2": "value2"
}
}
If you have Xiaomi device. Check that system not blocked you app in background.
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages."
Data messages, which are handled by the client app.
As per FCM official document,
Use only data messages if you want to receive(In OnMessageReceived method) and process it when your app is in background.