Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve notifications whenever there is change in Firebase Firestore Android?

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?

like image 260
Pokhraj Sah Avatar asked Jul 06 '18 04:07

Pokhraj Sah


People also ask

How do I listen to changes on firestore?

You can listen to a document with the onSnapshot() method. An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.

How do you handle notifications when an app is in foreground in Firebase?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.

How do I get push notifications on Android?

Turn on notifications for Android devicesTap More on the bottom navigation bar and select Settings. Tap Turn on notifications. Tap Notifications. Tap Show notifications.


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: [email protected]
 |
 --- 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: [email protected]
     |
     --- 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.

like image 133
Alex Mamo Avatar answered Oct 11 '22 12:10

Alex Mamo