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.
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
I am attempting to create an app that sends notifications to user while it is not the current app or open in the recent apps. I can create an application that sends notifications when the app is open, but cannot keep it going when the app is not the current app. I have attempted to use services but to no success. Any help is great.
I am testing my program on Samsung Galaxy A 50, running Android 9 Pie. I wish for my application to send notifications at a repeating time using AlarmManager. I am trying to use the service class to allow for notifications to be sent to the user while the app is closed
it seems you are receiving notifications while your app is in foreground but not in background or killed state.
Here is a quick suggestion, Instead of sending notification with "notification" keyword try to change this to "data"
Example:
{
"to": "device token"
"notification":{
"title":"Title",
"body":"Body"
},
"priority":123
}
Change to this:
{
"to": "device token"
"data":{
"title":"Title",
"body":"Body"
},
"priority":123
}
onMessageReceived is provided for most message types, with the following exceptions:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
Source: https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive
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'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 ?