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.
Related
am implementing push notifications to a Xamarin.Forms app. I included this plugin - https://github.com/CrossGeeks/FirebasePushNotificationPlugin. It works great, the only thing that I am missing is getting push notifications when the app is closed (killed).
Started playing with the sample the plugin authors provide (https://github.com/CrossGeeks/FirebasePushNotificationPlugin/tree/master/samples), just needed to add Xamarin.GooglePlayServices.Base nuget package, the rest is almost untouched. I set up the project in Firebase Console and now I can send notifications from Postman, which looks good:
when app is in foreground then the
CrossFirebasePushNotification.Current.OnNotificationReceived
event is properly triggered
when app is backgrounded then the notification is displayed, jingle is played and when I click it, the
CrossFirebasePushNotification.Current.OnNotificationOpened
event is triggered.
So far so good.
But how to show notification when the app is killed? I supposed it should be done by operating system when notification arrives? But obviously I am wrong or I am missing something. If i send a notification from Postman and the app is closed then nothing happens, no notification is displayed and no event is triggered.
There is also following event handler in MainApplication.cs class that looks promissing but it is also triggered only when app is backgrounded.
//Handle notification when app is closed here
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);
};
Any ideas how to make this work?
thanks
Jiri
Due to numerous app restrictions by android including Doze Mode and different OEM having their own implementation of battery optimization.
So I found out that GCM services is always running, is there a way for FCM to handle notification in background on its own?? Without depending on my app or app state?
I only send notification message which I want the FCM to handle without my app, because my app might be forced stopped or killed by app killers.
Can FCM just handle notification without my app's involvement?
FCM documentations states that
With FCM, you can send two types of messages to clients:
Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
Data messages, which are handled by the client app.
In your case, it seems that you are looking for Notification messages. In short, FCM will automatically display a standard android notification with a title and a message (body), when your app is in background.
To get this to work, the notification needs to be sent through Firebase (for example using the firebase console or cloud functions) and it needs to contain the notification key like in the following sample:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
For what regards your app's side, it mainly just needs to be setup as a Firebase client. If in any case you want to receive/display the notification also when your app is in foreground, then you'll have to create a service that extends FirebaseMessagingService as explained here
Bonus: since it looks like your target is android 8+, you may also want to look at Notification channels as every notification must be assigned to a channel in order to be displayed. By default, you'll get one with basic settings.
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
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 ?
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.