There are two types of apps one which will be used by users and the other which will be used by me i.e the owner. So whenever any users add a content (data) to the Cloud Firestore database, I want to get notified. In short, how to send notifications whenever there is a change in Cloud Firestore database?
-
1You can set up [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/android/first-message) to send a notification to your token ID which you might store somewhere in the database and update it everytime you use your(owner's) app. It uses Firebase functions, which can be triggered on data changes in firestore. – shraiysh Jul 06 '18 at 04:50
-
you can refer https://stackoverflow.com/a/50663966/7639541 answer, this is what you want. if you need any other help in cloud function let me know. – Rohit Maurya Jul 06 '18 at 05:46
1 Answers
The simplest way to achieve this is to use Cloud Functions for Firebase. This will help you notify users when something interesting happens, in your case, when users will add content (data) to the database. In order to make it work, you need to implement Firebase Authentication. This will help you send notifications to a particular user or to a group of users when something new happens.
So, please consider follow the steps below.
First, implement Firebase Authentication
. As soon as it is implemented, create a collection of users in which each user will be a document within users collection. Your database structure should look like this:
Firebase-root
|
--- users (collection)
|
--- uid1 (document)
| |
| --- //user properties
|
--- uid2 (document)
|
--- //user properties
Beside user details, you also need to add to each user a tokenId
. You get can it very simply using the following line of code:
String tokenId = FirebaseInstanceId.getInstance().getToken();
A user document should look like this:
uid1 (document)
|
--- userName: "John"
|
--- userEmail: john@email.com
|
--- tokenId: "e_wLukMfq..." //very long token
|
--- //other specific user details
Second, create a new collection within user document named notifications
, in which you should add all the notifications that you need to send to the admin. The properties that are needed are notification message
and the sender
. The user document should look something like this:
uid1 (document)
|
--- userName: "John"
|
--- userEmail: john@email.com
|
--- tokenId: "e_wLukMfq..." //very long token
|
--- notifications (collection)
| |
| --- notificationId1
| |
| --- notificationMessage: "My Notification"
| |
| --- fromUser: "My Notification"
|
--- //other details
Please see the nested notifications
collection which is hosted beneath user's document.
Now you need to use Node.js to write a function in Cloud Functions
that will listen for every new notification that appears within this reference:
"users/{uid}/notifications/{notificationId}"
Here is a straightforward example of how you can write the Node.js function.
Once a new notification appears, you can use sendToDevice
function and the tokenId
to send the notification to a specific user. The notification will be handled by the Android system and will be displayed to the user. Note, this will work only when the app is in background
. You can receive notifications also when the app is in foreground
by implementing FirebaseMessagingService
.
I also have explained in one of my tutorials step by step, how you can send notifications as you need to.
-
Is there everything alright, can I help you with other informations? – Alex Mamo Jul 07 '18 at 07:38
-
Is it not better to create a new root collection "Notifications" with a reference to the user to avoid nesting and eventual trigger loops? It also keeps the User isolated from Notification information. – Mikael Mar 25 '19 at 08:54
-
1@Mikael There is not a better or the best solution. That might be another solution and if it works for you, go ahead with that ;) – Alex Mamo Mar 25 '19 at 09:04
-
1As Firebase is a very specific storage type, it comes with special constraints as well. The way data is stored can also change drastically the queries performances (especially when the system scales) that's why I think that giving -by default - good advices about how data can be structured to support good scalability is a nice way to start. Of course I agree on the fact that each software has its own constraints. – Mikael Mar 28 '19 at 06:57
-
will very helpful for me, can anyone share step by step for iOS? – Sohaib Siddique Jun 06 '20 at 13:18