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

前端 未结 1 1969
有刺的猬
有刺的猬 2020-12-25 10:02

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 Fir

相关标签:
1条回答
  • 2020-12-25 10:44

    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.

    0 讨论(0)
提交回复
热议问题