如何在Firebase Firestore Android发生变化时实现通知? [英] How to achieve notifications whenever there is change in Firebase Firestore Android?

查看:137
本文介绍了如何在Firebase Firestore Android发生变化时实现通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种类型的应用程序,一种将由用户使用,另一种将由我使用,即所有者。因此,只要有任何用户将内容(数据)添加到Cloud Firestore数据库,我就会收到通知。简而言之,如何在Cloud Firestore数据库发生更改时发送通知?

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?

推荐答案

实现此目的的最简单方法是使用 Firebase的云功能。这将帮助您在发生有趣事情时通知用户,在您的情况下,当用户将内容(数据)添加到数据库时。为了使其有效,您需要实施 Firebase身份验证。这将有助于您在发生新情况时向特定用户或一组用户发送通知。

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.

首先,实施 Firebase身份验证。一旦实现,就创建一个用户集合,其中每个用户将成为用户集合中的文档。您的数据库结构应如下所示:

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

除了用户详细信息,还需要添加到每个用户 tokenId 。你可以非常简单地使用以下代码行:

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();

用户文档应如下所示:

uid1 (document)
 |
 --- userName: "John"
 |
 --- userEmail: john@email.com
 |
 --- tokenId: "e_wLukMfq..." //very long token
 |
 --- //other specific user details

其次,在用户中创建新的库选文档名为 notifications ,您应在其中添加需要发送给管理员的所有通知。所需的属性是通知消息发件人。用户文档应如下所示:

Second, create a new coolection 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.

现在您需要使用 Node.js 云函数中编写一个函数,它将监听此引用中出现的每个新通知:

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}"

此处 是一个关于如何编写Node.js函数的简单示例。

Here is a straightforward example on how you can write the Node.js function.

一旦出现新通知,您可以使用 sendToDevice function和 tokenId 将通知发送给特定用户。该通知将由Android系统处理,并将显示给用户。请注意,仅当应用程序位于后台时,此功能才有效。当应用程序位于前景时,您可以通过实施 FirebaseMessagingService 来接收通知。

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 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.

我也在我的一个 教程中取得了成功。 / strong>一步一步,如何 发送通知

I also have exaplained in one of my tutorials step by step, how you can send notifications as you need to.

这篇关于如何在Firebase Firestore Android发生变化时实现通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆