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.
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'm developing an application for my College:
Users (students) login on it with their college credentials, once they are authenticated, the app creates a Firebase Database data for that user containing al his info, including the disciplines(subjects) that he is on course.
Then comes the Firebase Cloud Messaging part, the users can send notifications for other users that are on the same disciplines.
Example: I'm coursing Math, then can I send a notification to the others users that are also doing Math. There are hundreds of disciplines in my College.
My idea is to send the notification to all of my app's users then, before notifying, handle with some code to check if the user is registered to the notification's target discipline or not, if he is, send notification, else, do not send anything, in others words, filter the message before showing the notification!
I studied several Firebase Cloud Messaging docs and examples but I couldn't find a way how to do it... Can somebody give a light?
Yes, actually, it'd be best to decide server-side on the group of users that will receive the notification, meaning, you need to have them in some sort of group. Firebase has a concept of group and subscription for messaging. And you can setup a cloud function to actually send the message to the clients
https://firebase.google.com/docs/cloud-messaging/js/send-multiple
Okay, I know the question may sound stupid. I’m building an app where there are pages that publish posts and users can subscribe/unsubscribe to those pages.
The goal here is to find a way to send notifications whenever a new post is published but only to the subscribed users. I though that I can do this by sending a push notification to all the devices on my app whenever the “Posts” reference is updated on Firebase database, and then choose whether to show this notification or not if the user is subscribed (on client side)
Is this a good idea? And if yes, how can I accomplish that?
Is this a good idea?
Yes, the idea is not bad. However, filtering notification to be shown in client side is done in many cases.
And if yes, how can I accomplish that?
I hope you already have a login or authentication system using Firebase authentication or any other server side authentication. When you have this, you might have already considered sending a push registration id to your server or firebase when a user signs up in your application and save it in your firebase database.
Now when its time to send a push notification, you are planning to send the push notification to all of your registered devices and you want to filter the notification will be shown or nor in the client side.
This can be achieved by keeping a flag in the client side, for example a SharedPreference having the id or tag of the last post. If you have an incremental id for each post, then it will be a lot easier to implement. When a user launches your application, it pulls the posts from your firebase database as I can think of. Just save the latest id of the post in your SharedPreference and when a push is received, match the id of the post that came along with the push notification with the latest id stored locally.
If the id received via push notification is greater than the id stored in your SharedPreference, you will show the notification in system tray and the notification will not be shown otherwise.
Hope that helps.
You can push notification to only subscribed users if you are maintaining a list of subscribed users FCM token in your database
Your approach is correct, what you would need to do is that maintain different database tables for all pages where users are inserted when they subscribe to that particular group/table.
So, let's suppose when a person subscribes to the page containing information about Sports, you add him to that group. Later, when you are sending update/notifications related to 'Sports' you would only send those push notifications to the user-tokens in 'Sports' table.
In this way, only relevant subscribers would receive those push notifications.
We have a design approach which is creating a topic for each registered user.
Creating a new user
Save it to our database with a generated token
Subscribe to /topics/{user-token} when user login on android or
ios device.
So if user have more than one device and if we want to send a user specific notification, we just send it to /topics/{user-token} so it received by all devices.
We've not encountered any problem with a few users yet, but is that ok for Firebase limitations and is it a good approach?
(I am moving my comments into an answer)
Most of the times creating an FCM TOPIC per user is NOT a good idea.
Messages sent to an FCM TOPICS are public. Any user (even from a
different app) can subscribe to /topics/{user-name} and receive those
messages.
Example:
Another developer can copy the google-services.json file from your apk.
Then he can subscribe to any topic.
To intercept your user messages the attacker still need to guess the {user-name} or any other identifier you are using. But if you assume this can happen then the issue is big because you would never know if someone is receiving a copy of your messages, and you usually never change {user-name}.
This is not a security issue of FCM. This is part of the topic API design.
If you need secure messages you can send them directly to the device token.
If you still want to do one topic per user, please pay attention to not send sensitive data, or data that should not be intercepted by third parties.
I am learning how to implement the GCM both from client side (Android) and from server side (ASP.NET). I spent a bit of time on reading the whole google documentation related to GCM and I also tried the sample that they provided.
Because I need to integrate the GCM in an already existing app, I would like to know some specific stuff.
As I got it, the registration id is a token which ties the app on a specific device to the GCM service and the app server; so, the backend can send downstream messages directly to that device.
In my scenario, I could have multiple users who can use the app on the same device, that means they need to login in the app and they have an account on a database in the server side. Do I need to store a different registration id for each user on that device? Or still the registration id refers to only the app?
And what about the same scenario but distributed on multiple device, because a user can have multiple devices?
Thanks in advance.
This relates a lot to the context of your app and what you want to do with notifications
Having one registration ID per device will be okay.
I manage my multiple users by using subscription tags handled by my server.
So take the scenario if you have a sports app:
User A & User B share the same device. Each user subscribes to a tag.
User A is subscribed to two tags (Basketball & Football)
User B is subscribed to two tags (Tennis & Basketball)
When User A logs out you clear the tags associated with that user and when User B logs in you fetch his/her tags.
Your server knows notifications to send to the device based on the tags the user has subscribed for.
Multiple device scenario:
The same goes, when your user logs In you get their tags. The device also has its own token from GCM.
The registration id is the app's id, it may be changed if app version was updated. Your push notification will be sent on device with your app, regardless user account. So every user on every device will receive your notification, you must store only one refistration id per device.
For multiple users: If your messages are user specific, you would want to retrieve the user's token and subscribe to it only while they are logged in. When you switch users remove/unregister the previous token, then save/register the new user's token.
You should not only do this if you have multiple users per device, but when a user signs out. This will prevent user-specific messages being shown at wrong times to wrong users.
For multiple devices: it sounds like you are looking for Device Group Messaging.
With device group messaging, app servers can send a single message to multiple instance of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user.
This is also nice because of the collapse_key. When one of the devices on the same account opens a notification, it will dismiss the notification on the other devices..