无法理解 Firestore 在 Android 中如何使用数据更改通知。有人能解释一下吗?

huangapple go评论98阅读模式
英文:

Can't understand how notifications on data change work with Firestore in android. Can anyone explain?

问题

如我在标题中所说,我不理解在Firestore中数据更改时通知的工作原理。我有一个应用程序,当数据库中添加数据或现有文档更改时,需要向某个用户组发送通知。

我理解通知通道如何与UI一起工作,但不知道如何设置我的应用程序,以在应用程序不运行时在后台响应数据库中的更改。我可以直接从FireStore控制台发送通知。问题在于当数据库中的数据发生更改时,如前所述。我在Android Studio中使用Java进行编码。

英文:

As I have said in the title, I can't understand how notifications works when data is changed in Firestore. I have an app that requires notifications to a certain group of users when data is added or existing documents are changed in the database.

I understand how notification channels work with the UI but not how to setup my app to respond to changes in the database in the background when the app isn't running. I can send a notification directly from the console in FireStore. The issue is as stated when data changes in the database. I'm coding in Android Studio with Java.

答案1

得分: 2

这里 有一个类似的问题,以下是要点。

我认为你可以考虑使用Firebase的Cloud Functions。这将帮助你在数据库发生有趣的事情时通知用户。为了使它工作,你需要实现Firebase身份验证。这将帮助你在发生新事件时向特定用户或一组用户发送通知。

这里是一个如何编写Node.js函数的简单示例。

一旦出现新通知,你可以使用sendToDevice函数和tokenId将通知发送给特定用户。通知将由Android系统处理并显示给用户。请注意,这仅在应用程序处于后台时起作用。通过实现FirebaseMessagingService,你还可以在应用程序在前台运行时接收通知。

还有一个教程,逐步解释了如何按照你的需求发送通知

英文:

Here is a similar question, following: the highlights.

I think you might consider use Cloud Functions for Firebase. This will help you notify users when something interesting happens in 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.

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 the foreground by implementing FirebaseMessagingService.

There is also explained in one tutorial step by step, how you can send notifications as you need to.

huangapple
  • 本文由 发表于 2020年7月30日 19:52:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定