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
Related
I am seeing how Huawei Push Kit works in Android. For this I am using postman to send the notifications, I am sending a notification message and a data message. Notification messages are seen in the notification area, but I can't get the data that I sent in the data message. I would like to know how to get them both when the app is in foreground and background.
You have to include "foreground_show": false to get the message "data" payload to get delivered to "onMessageReceived".. otherwise it will only be displayed in the notification area .. and you wont be able to read the payload..
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/android-basic-receivemsg-0000001087370610-V5
Read "Receiving a Notification Message" Title in the above doc
There's a difference between HMS Core Push Kit and FCM. Notification messages will be delivered to the system tray, and data messages will be delivered to the onMessageReceived method by default when using HMS Core Push Kit. So notification message title and body cannot be obtained.
If you want to pass data to your app when sending notification message, you can tap the notification message to trigger the corresponding action such as opening the app, a web page, or a specific page in the app. To obtain data with customized actions, see docs.
For the data message parameters, see this:
{
"validate_only": false,
"message": {
"data": "{'param1':'value1','param2':'value2'}",
"token": [
"pushtoken1",
"pushtoken2"
]
}
}
and get the data with the onMessageReceived() method.
When you send a push notification without explicitly setting the value of the *message.android.notification.foreground_show = **false***, the data content won't be read because the NC (Notification Center of Huawei phones) takes care of the notification (when your app run in the foreground).
The NC doesn't implement the method of reading the data content as stated here by Huawei.
Therefore, you have to instruct your app, so that is your app handling the push-notification rather than the NC.
To do so you have to:
message.android.notification.foreground_show = false in the payload (See here)
You need to declare the implementation class of HmsMessageService in the AndroidManifest.xml file of the app (See here).
Here you can find all the details about the payload of a Downlink Messages
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 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.
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.