I want to know how FCM Notifications works in below scenarios:
Same user logging into multiple devices. Will all the devices gets the notification?
What if user uninstalls the app? Does FCM sends the notification even if user uninstalls the device, and later checks for sending failed event?
If login changes on the device - Suppose if I login to a device as user A, and then logged out of the device and logged in as user B. I must not get the notifications for user A. How this situation is handled in FCM?
Same user logging into multiple devices - Will all the devices gets the notification?
Firebase Cloud Messaging sends messages to devices, it doesn't know anything about users. If you're sending messages to FCM Instance IDs, the message will only be delivered to the device with that ID. If you're sending messages to topics, the message will only be delivered to devices that subscribed to that topic.
Does FCM sends the notification even if user uninstalls the [app from the device]?
When the app is uninstalled, the Instance ID for that app installation is deleted to. So no message will be delivered to the app on that device anymore.
If the user later reinstalls the app on the device, it generates a new Instance ID. So it won't receive any messages from when the app was uninstalled.
If login changes on the device - Suppose if I login to a device as user A, and then logged out of the device and logged in as user B. I must not get the notifications for user A. How this situation is handled in fCm?
Since Firebase Cloud Messaging doesn't know anything about the users of your app, it is up to your application code to make this connection. Many apps clear the Instance ID from the device when a user explicitly signs out.
Also see:
How to refresh FCM Token on user Logout?
Firebase Cloud Messaging - Handling logout
How to unsubscribe from FCM notifications after logout from app?
How to release Firebase Cloud Messaging token when Android user logs out?
Related
Is it possible that a downloaded and installed app can still do push notifications even though the user has never once launched the app?
In order for Firebase Cloud Messaging to send messages to an app on a specific device, the app must have collected a device ID token. This requires that the app have been launched at least once, so the app code can get that token and send it to your backend. If the app was never launched, that means it never had a chance to collect that token, and messaging to it is not possible.
Is there any way to send push notifications to the users who have installed the app but not opened yet. I am using firebase to send push notifications.
In order for Firebase Cloud Messaging to send messages to an app on a specific device, the app must have collected a device ID token. This requires that the app have been launched at least once, so the app code can get that token and send it to your backend. If the app was never launched, that means it never had a chance to collect that token, and messaging to it is impossible.
In my Android App, there are multiple users using it and they might be logged into it in different times.
I'm using Cloud Functions to send notifications to specific tokens, which means to specific devices, but I couldn't find a way to make sure that the notification will be shown only to the user it was sent to.
Let's say that users A and B are logged in to my app on the same device. The received notification is meant to User A, but the currently logged user is User B, and I don't want User B to see User A'snotifications.
I have tried to check it this way in my FirebaseMessagingService:
String receiverEmail= remoteMessage.getData().get("sendTo");
String loggedEmail=firebaseUser.getEmail();
if (loggedEmail!=null){
if (receiverEmail.equals(loggedEmail)){
sendNotification(
context.getString(R.string.your_friend_request_was_accepted),
context.getString(R.string.by)+" "+senderName);
}
}
and it works, only when the app is running. It doesn't work when it's closed.
I have also tried to get the logged user from the Shared Preferences and check it, but then loggedEmail was null, and it didn't work at all.
Since both users are using the same app installation, they're identified by the same FCM token. This means that there's no way to make the split on the server, and you'll have to direct the message to the correct user on the client.
But if neither user is current actively using the app, notification messages will be handled by the system. This means there is no concept of a currently active user, and the system will show a generic notification (that will typically activate your app when clicked).
I'd consider if FCM is really the best tool for targeting such notifications. FCM targets at its most detailed level by FCM token, while you more likely want to target by UID (or email address). I'd consider using the Firebase Realtime Database for storing messages by UID (or email address), and then use FCM for sending a notification to the device. When a user clicks the notification message, the app starts and you can retrieve the messages from the database for whoever is signed in.
If your FCM message has the notification node in its json, the system sends a notification automatically if the app is in the background, and comes to FirebaseMessagingService only if the app is in the foreground.
Since your push message already has a data node (which you're using to identify the user it's sent to), you are not required to have the notification node. Once you remove the notification node from the push message json, the flow will always come to your FirebaseMessagingService.
in `Firebase Cloud Messaging (FCM) I m getting device token.and notification is coming .but in my app there is two user so i m sending notification for 2 user notification is coming.But i m logged in as 1 user. because both device id stored in my database and based on device id is fire base sent notification.so i want to restrict or unregistered fire base device token .if i m not logged in as 2user.i can delete that device id from database that is one solution. but some reason i cant delete for counting user it is required .so help me for this situation..thanks for helping
FCM is running on GCM as its core. The behavior related to GCM when an app is being uninstalled should still be the same for FCM.
When your app is uninstalled, it is the developer's responsibility to unregister/remove the corresponding registration token from your own App Server, along with the other actions (in your scenario, count the number of uninstalls) you need.
In turn, the FCM server will determine if the app is uninstalled and then invalidate the corresponding registration token. From the docs:
Finally, when FCM attempts to deliver a message to the device and the app was uninstalled, FCM discards that message right away and invalidates the registration token. Future attempts to send a message to that device results in a NotRegistered error.
For the application that I am working I need to integrate Google Cloud Messaging. After playing a little with different examples, I was able to send and receive a notification on my deivce.
However, I have come across an interesting situation. As I know (please correct me if I'm wrong), the registration_id is issued per device and per application.
The application that I'm working supports login functionality. When the application is installed and the user logs in for the first time (let it be "UserA"), I request the registration_id from GCM which then I send it to my server.
Now imagine that UserA logs out and gives his device to some UserB to log in. With other words, UserB logs in using UserA's device.
The problem is that if meanwhile UserA receives a notification, UserB will be able to intercept it. And if UserB receives a notification, he won't be able to receive it.
This seems normal because the registration_id is per device and per application, but it does not seem reasonable for my case.
So I'm asking if there is a way I could make the registration_id to be dependent of some user id (besides the device and app)? Or how could I make such that the logged in user to receive only his own notifications?
Yes its true that you have one google registration ID for the app per device.
But you can always register and unregister users at your own Server that will actually send messages to GCM and GCM will send this to the registered devices.
Define some interfaces for your Server like registerOnServer and unRegisteronServer , send some unique value for each user on this interface.
So, in your case, when A use Log ins , regitration is done on GCM first and register the user on your Server with registerOnServer inteface and while the user is logged in send notifications pertaining to him to GCM to be send to the device.
When A logs out, unregister him using unRegisterServer and do not send any messages from your Server to GCM as A is unregistered.
So,now if B Logs in even with the same device , register him at your Server and send his messages.
This shall solve your problem!