I am working with FCM to receive push notifications. I am so confused with this I can easily push notification and receive them with my Firebase account but I am not getting its proper flow.
I've used this link and to understand how it works I removed onMessageReceived(RemoteMessage remoteMessage) {....}
and I am still getting notification.
I'd like to understand the flow of notification received from Firebase.
If your app is in the foreground, the method onMessageReceived will be called and notification will not be shown.
If your app is in the background (not running or minimized), the method onMessageReceived will not be called and you will get notification directly.
So if your app is in the background, there will be no impact of removing onMessageReceived method. You will still get the notifications.
What type of notification you are sending it from your back-end?
If its type of
"notification": { }
then android system will handle this and responsible to display notification.
If you are using "data" : { } then you will get those details in onMessageReceived() callback.
Related
I'm using FCM to send my users app notification, I also store user status(which is logged in or not) using shared preferences. when I send notification to my users all of them will receive message, how can I prevent receiving message for users who are not logged into app?
You should send data message which can be processed from your FCMService class. Hence you can show the notification from onMessageReceived conditionally.
Refer this documentation to send data notifications.
Atleast add some code for better suggestion, but here is what you can do to make sure that only those users who are logged in receive the notifications. Since notifications are created by you only so you can control them as well.
Add below logic in your Service class which extends FirebaseMessagingService.
for example.
override fun onMessageReceived(message: RemoteMessage) {
// this is where you receive your notifications.
//just before showing messages just check if user us logged in or not
if(!sharedPreference.isLoggedIn()) return
//if he is logged in, your notification showing code goes below.
}
edit:
in case of app is not in foreground then onMessageReceived won't get called if you are sending notification payload in that case from server make sure to send only data payload not notification object and then it will get called onMessageReceived and based on received payload check if the user is loggedin or not based on that show notification.
please read this thread for more clarification.
https://stackoverflow.com/a/40083727/4517450
I have a problem that to handle FCM notification message.
I want to handle notification message without data fields.
So I've implement firebasemessagingservice in my project, but onMessageReceived function did not triggered.
Is it possible? Can I handle none data fields notification message in background state?
No, onMessageReceived only gets triggered in the background when you have a data payload, see the documentation for details.
If you only have a notification field, it will land in the system tray and be handled by system, if you still want to handle the notification yourself, change the message payload to data and just include the related notification information and push out a notification yourself, for more details, see documentation.
The documentation says you can do this with setBackgroundMessageHandler() which goes in the firebase-messaging-sw.js file.
I have never done it, but that is what the documentations says.
My app has push notification (FCM). When app runs in foreground,I can get data payload in onMessageReceived() function. When app runs in background or app was fully closed, onMessageReceived() function was not invoked.
My question is, is possible to get data payload when app in background or closed?
Thanks in advance!
If you want to receive data payload in your onMessageReceived() then you need to do change where you generating the notification(like at your server side),
You need to remove notification payload just need to send data payload If you send only data payload then its work like bellow one.
From
{"to":"[add your token]","notification":{"title":"[add title]","body":"[add your message]"},"priority":"high"}
TO
{"to":"[add your token]","data":{"title":"[add title]","body":"[add your message]"},"priority":"high"}
After doing this you will get notification perfectly
I have an android app connected to FCM(firebase cloud messaging) for sending notifications . However , when the app is in background , it doesn't apply
custom notifications. I think this is because it does not enter OnMessageReceived method . Whats the solution for this ? As i read somewhere it can be done using HTTP Api but i am not sure of how to go about it ? Please help.Thank you.
Whether the method onMessageReceived is called is dependent on the notification payload.
See 'Handling Messages' section in FCM docs here.
Handling messages
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, both background and foreground. 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.
In summary, if the app is in foreground, then onMessageReceived will always be called. If the app is in background, then onMessageReceived will only be called if the notification payload is data only.
So to answer your question, if you must have onMessageReceived every time regardless if the app is in foreground or background state, then set your notification payload to contain data only.
I want to start a background service when I received a special type of message. For this I am doing the following
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().get("message").equals("something"){
//start service
}
}
}
My problem is, it only works when app is foreground , but when the app is in background onMessageReceived doesn't trigger. Rather than it just show a notification .
What I want is to disable notification when app is in background , rather start a service.
I have looked other answers is SO, some of them suggest to accomplish this I need to send data-message . And I am sending data message from firebase console, but it is not working.
What is suggested is to send a data-only message payload. This is not possible using the Firebase Notifications Console.
When using the console, the message sent is treated as a notification message payload, and only adding Custom Data will the data payload be included. However, in that case, it still has both notification and data messages.
To send a data-only message payload, you'd need to build the request yourself through your own App Server or by using Postman or cURL