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
Related
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.
I've managed to retrieve the data when the app is in foreground and when the app is in the background and the user tap the notification. The problem happens when the user dismiss the notification, the FirebaseMessaging.MessageReceived callback never got called.
Is there a way to retrieve/retain the notification data when the user dismissed the Firebase notification?
It appears that this behaviour is specific to messages that contains both notification and data payload. If you want to receive the data packet no matter what, send it as pure data package.
In this case, I simply split the message into two, one containing the notification and the other containing the data, and then send them via the Fcm HTTP endpoint separately.
I'm working with Firebase Cloud Messaging. I have a couple of question I was not able to understand from the documentation:
Android: Lets suppose the app is closed (not backgrounded: closed). If I send a notification with also the data payload, this data payload is passed to the activity through the Intent Extra
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.
What if the user does not tap on the notification? Is the data payload lost? Is there a way to retrieve it?
iOS/Android. Lets suppose the user disabled the notification and I sent a notification to the client: is there a way to retrieve (pull) the notification at the application start?
Thanks very much
If the app is closed (not in background) the onMessageReceived method is called when the notification is received and there you can retrieve the data payload with remoteMessage.getData().get("key_for_parameter"); where key_for_parameter is the name of the parameter that you send in the notification. This method is called even if the application is closed. However, take into account that the "onMessageReceived" is called only if you omit the "notification" param in the notification (look this post)
So once you have the params in the onMessageReceived you can look for an strategy to use them in your application like storing in the DB and you are not going to lose the data if the user does not click on the notification.
if your app in background, Firebase will not trigger onMessageReceived(). Why.....? I have no idea. In this situation, I do not see any point in implementing FirebaseMessagingService.
According to docs, if you want to process background message arrival, you have to send 'click_action' with your message. But it is not possible if you send message from Firebase console, only via Firebase API. It means you will have to build your own "console" in order to enable marketing people to use it. So, this makes Firebase console also quite useless!
There is really good, promising, idea behind this new tool, but executed badly.
I suppose we will have to wait for new versions and improvements/fixes
I'm using Firebase Cloud Messaging to send notifications with data payload to my app.
When i send a notification, if my app is running (foreground) i get the data overriding the onMessageReceived() from FirebaseMessagingService class.
If my app is not running the notification is sent to the "system tray" and when the user click's in notification my app start and i can get the data with the getExtras of the intent.
But, how could i get the data if the user dismiss the notification in "system tray"?
I need to write some background service that "listen" to the notifications to get this?
Seems like you are sending notifications from the Firebase console. These messages are always notification messages. If a notification message has an accompanying data payload then that data is only available to your application if the user taps the notification. If the user never taps the notification then that data will not be available to your app. So you should not use notification messages to send app critical data to your application.
If you send data messages they are always handled by onMessageReceived callback. At that point you can either silently handle the message or display your own notification. So use data messages if you want to be sure that your application has an opportunity to handle the data in the message.
So basically you want to store that data ?
if you want to store that data then write your code in OnReceive() method and write Database insertion code in it.
and put one flag in it like "Dismissed=true"
and if user open your notification then make it false and you can get your result.
There is no specific method to detect whether your app's notification is dismissed or not. So you have to manually maintain that data.
Is it possible/is there a reliable way to get a call back when a notification is received on the android platform? i.e. I want to record when the notification is received (not when it is later opened).
No, It's your app which receives the message so if you need things logged, then log it. Simply handle it as you like once you receive it. Note that if you use FCM with Notification message message type, then you will not be directly notified about message arrival when app is in background (notification will be automatically posted by Firebase - your app will get message payload once use taps the notification). In such case consider switching to Data messages so you will get it no matter your app is in foreground or not.
See docs: https://firebase.google.com/docs/cloud-messaging/downstream