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.
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
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.
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 have a android client app, a server side in django and now I am adding push notifications with GCM.
In my app I have users that login/logout, so I will have a table in my database with devices ids coupled with users, so that every time I want to notification a user, I will lookup its device id and send the notification.
my question is:
When is best to register the Device to the GCM?
When to add the Device to my Server side database?
In the Google Docs it says you should do it once, when the app is installed.
Because I have users in my app, and I want to couple a user with a device in the database, when the app is installed there still isn't a user to associate the Device with.
After the user installs the app, he can register, login and so on.
So when you think is best to register the Device to the GCM and to my server side?
Should it be with a user or only associate it later?
Thanks a lot!
My suggestion would be:
Register the device immediately after the first execution
Save device id somewhere accessible anytime by the app
Couple the id with user details after registration
With this approach you will be able to handle issues if something goes wrong with the registration process and send a notification to the device even if the user is not registered.
EDIT:
You should also implement a strategy to check on a regular basis if the association between the user and the device is still valid or needs to be updated
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..