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.
Related
i am developping an android application based on a wordpress blog where users can post their own articles, i work with WP REST API to get posts , comments and users ...
I would like to send a notification automatically to the user when his article got a new comment , i have installed FCM correctly and tested the simple forme of notifications but i need to :
Trigger FCM Notification on new comment sent ( Or just on a button click )
Specify receiver so the notification is sent to logged in user with the right email/username
I thought of
working with setting User Property for every user logged like this :
mFirebaseAnalytics.setUserProperty("user_name_for_notification", "the_username");
Sending notification for all devices then handle it in onMessageReceived
but i don't think those are good ideas
So , any ideas where to start
thank you
FCM can send notifications with three different targeting methods:
to a specific FCM token/instance ID token, which identifies an installation of a specific application on a specific device.
to a group of such device/instance IDs.
to a specific topic, which FCM clients can subscribe to.
Firebase Cloud Messaging does not have the concept of a user, therefor it cannot target users directly. If you want to target users, you will have to map them to one of the targeting methods outline above.
The most common ways that I know of:
Store the token(s) for a user in a database under their UID, and then send to the user's token(s) when you need to target the user.
Give each user their own topic, based on their UID. Have the app subscribe to that topic, and send message to the user's topic when you need to target them.
Also see:
How to send one to one message using Firebase Messaging
Sending message to custom user using firebase
Send Firebase Cloud Messaging notification to users by user property
how to send data message to user segment From Rest API
I am implementing push notification in my app after having it for IOS for a while.
Use case: I have user login, and accounts stored on our servers. I only want to send push notifications when a user is logged in, and only for the user currently logged in. Notifications are targeted to the individual user.
To do this, i fetch the token using
FirebaseInstanceId.getInstance().getToken()
, send push token to our server when user logs in, and remove it on logout.
Everything works as far as registering token, sending the token etc., HOWEVER, there could be scenarios where this doesn't work, for example if a user logs out with flight mode, so our server still has the token and thinks it should still send them.
On IOS, there are two local functions, register/unregisterforremotenotifications, that basically turns notifications on/off, regardless of whether my server could be contacted. I can call these on login/logout, and IOS won't show any remote notifications for my app, and i'm safe.
However, with Firebase, i can send the token to the server on login -
for logout, however, its more complex since there's no "local" system-function to call that i can find.
The best thing i've figured out, is to always send a 'Data' notification,
as described in this question,
so that my notification service always gets called, even in the background, and there check if i am logged in, and not show the notification if i'm not.
However, the notification for the wrong user will still be sent to the phone, and it's a risk, for example if i, god forbid would have a bug... or the notification gets logged somewhere in the system.
Sooo, my question is if there's any way to disable notifications on logout via Firebase?
I hope this makes sense, thoughts much appreciated!
Yes, what you do is you create your own Login and Logout APIs.
In your Login you should be storing your Token for PUSH notifications.
then whatever data triggers need to PUSH do a loop and build a push for known registered tokens.
On logout, simply delete that token from your Database and the loop will no longer include it for PUSH. You are correct using DATA tag will only work in foreground, but could be for wrong user that is correct.
If you do not have a backend, then please provide more clarity as to where/how you are storing and using your tokens to PUSH so I can help you further.
I have done this exact scenario to avoid wrong person getting push, but I support foreground and background on my scenario, so only logout or expired token will disable the PUSH on my app.
There are two ways:-
Your app supports multi user login at the same time scenarios :-
You can probably store the device token in share preferences with a boolean flag , when user relaunch the app , check if the flag for deletion is set then you can try deregistering from the service on server.
If it fails then again you can do the same , so it would be something like checking boolean flag for account deletion on every app launch to make sure it deregister.
Your app supports single usr:-
You can simply delete firebase instance Id before signing into the account. This would take care of the scenario where a different user had signed out offline and you were not able to de register from your service.
You can also handle scenarios when to show notification based on accountId or user Id of the signed in user.
I'm working on a doorbell project in which I want to send Push Notification to user when a guest come and ring the doorbell, it should send a Push Notification to the user (i.e owner of house). I want to use Firebase Cloud Messaging for this in my android app.
We are using RaspberryPi as the hardware controller. It will trigger notification to user using FCM on their android device when someone presses their doorbell.(which is not a point of concern.)
My confusion is:
According to this post on SO, FCM will send Push Notification to the predefined 'registration_ids' which we have mentioned in the app.
But in my case, every user will have their different registration ids. Also the number of users are not fixed.
It could be any number of users. Whoever uses the doorbell will have their own id.
So it will be a possible scenario like:
suppose I have not mentioned any registration id in app. 10 users have installed this doorbell at home and someone on the house no 1 presses doorbell button, the FCM will send push notification to all the 10 users. Reason is, the registration id will be same for all the devices who have installed app from PlayStore.
suppose I have mentioned 3 registration id in app. So 3 users will be getting the same Push Notification at the same time.
And if i'm not wrong, if i put only 1 registration id, only 1 user will get notification even if 5 different house's doorbell are pressed, the same user will be notified 5 times.
If i declare a specific registration id to be notified in server side (i.e RaspberryPi), I have to regularly add new registration ids as the number of users increases in my app and update it frequently.
So how do I resolve this problem ?
I haven't started developing this app till now because I'm confused with the first step itself.
If I've gone wrong somewhere, please guide me as I'm new to Push Notifications or FCM. I may not have understood concept of FCM, which is possible but I'm not sure. If this solution is not possible using FCM, suggest me some other options to achieve this please.
A Registration token corresponds to a single app instance (see my answer here).
In your post, it seems like you were confusing the registration id as something that is permanent in the app, which is not.
Whenever a user installs your app, on the first initialization, it should generate it's own Registration token, where you'll have to save that token to your App Server, making sure that you associate it with the corresponding user details.
For your use-case, you could make use of Topic Messaging, where you simply subscribe the user to their corresponding doorbell topic, and every time that specific doorbell is triggered, you send a message to the corresponding topic.
I want to build an Android app that allows a user to send notification and data messages to other users. I've started using Firebase only recently, and Firebase Cloud Messaging is still pretty confusing to me.
I have already implemented sending a message to a specific device using the Firebase Instance Id. However, in my app, a user can log out and log into their accounts using different devices, so this isn't really what I want. I read the documentation and it's pretty confusing, they mention sending messages to user groups, and topics (which only apps can subscribe to, not users).
Is there a way to send a push notification directly to another user or a group of users using only their UIDs? If not, is there any other way I can implement this?
I did it storing the FCM Token in a device structure by user. When the user login, add the deviceData to the userToken structure. You need clear deviceData when the user logout. This way, you will only send notifications to logged devices.
Ex (This is not like mine structure, but it can help you to wondering a good way to do this):
-userToken
-idUser1
-device
-idDevice1
-fcmToken: "xxxxx"
-idDevice2
-fcmToken: "YYYYY"
Hope that I helped you
Alright, so to make sure this works across multiple devices when a user logs in, I just make the device subscribe to a topic with the name equal to the user's UID. Now if I want to send a notification to a user, I just send an FCM message on the topic with the name equal to the user's UID!
Example: If my UID is equal to "asdf", whenever I login using multiple devices, each of the devices automatically subscribes to the topic with name "asdf". So now, if I send an FCM from the server on this topic, all the devices from which I am logged on to obtain this notification.
I know this has been discussed before, but the only solution I found (canonical IDs) don't work in my scenario.
Scenario:
User installs App and registers with his user account A
User gets push notifications meant to be delivered to this specific user account A
User uninstalls the app
User reinstalls the app
User registers with a different user account B
Now notifications are delivered for both user accounts. From my understanding, using cannonical GCM Reg IDs would only consolidate those IDs and prevent sending duplicate notifications. In this case the App gets notifications for a different user that shouldn't be delivered at all.
Is there any fix for that? Only thing I can think of would be actively deregistering when uninstalling the app, but in another thread I read, it's not possible to execute code on deinstallation.
From my understanding, using cannonical GCM Reg IDs would only consolidate those IDs and prevent sending duplicate notifications.
That's one way of looking at it, but on a simpler note, Canonical IDs are like saying "old ID you used is expired, delete it (if you saved it) and use me instead".
The thing that makes this a bit odd is that, when the user uninstalls the app, the InstanceID should be invalidated. (see the docs here).
What I think you can do to make sure that tokens are deleted, you can call deleteInstanceId() to revoke all tokens, then re-register.
But to make sure that the message is for the intended user, you can refer to what is stated in the docs (first one similar to what #Ak9637 said):
To make sure that messages go to the intended user:
The app server can maintain a mapping between the current user and the registration token.
The client app can then check to ensure that messages it receives match the logged in user.
You can simply check ,when registering in your tokens database,when u receive token of user B , just check if it is already associated with any user or not, if it is simply nullify that field and save the new token to new user.
As token identifies the device not the user.
If you wish to take total autonomy and control of notifications related to your app, You should use data notifications instead of message notifications , this will avoid the OS handling the notifications instead of your app