使用 Firebase 实时数据库进行 Flutter 通知
我有一个与 Firebase 实时数据库集成的 Flutter 应用程序。 我想要一个通知(如果可能的话,实际上是一种警报类型的事情),当数据库中的某个项目(我们称之为警报)设置为“true”时。我总是看到 firebase_messaging 插件用于通知,但我不确定我是否应该使用这个插件,尽管我的应用程序与消息传递没有任何关系。
我对 Flutter 和 Firebase 都是全新的,您能告诉我如何在应用未运行的情况下监听数据库吗?
顺便说一句,我目前只为 Android 构建应用程序,但我想将来也为 IOS 构建它。 谢谢。
I have a Flutter app integrated with a Firebase Realtime Database.
I want a notification (actually an alarm kind of thing if possible), when an item (lets call it alarm) in the database is set to "true". I always see firebase_messaging plugin for notifications, but I am not sure if I'm supposed to use this plugin despite that my app doesn't have anything to do with messaging.
I am totally new to both Flutter and Firebase, can you tell me how to listen to the database even if the app is not running?
By the way, I am currently building the app for only Android, but I want to build it for IOS too in the future.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当用户没有主动使用应用程序时,没有可靠的方法可以继续侦听 Firebase 实时数据库中的更改。要在这种情况下通知用户数据库的更改,您需要在始终开启的环境中侦听这些更改,然后通过 Firebase Cloud Messaging 向用户发送消息。
Cloud Functions 是一种始终存在的环境,它也是 Firebase 的一部分,允许您在 Google 服务器上运行一小段 JavaScript 代码,以响应 Firebase 项目中发生的情况。 Cloud Functions for Firebase 文档作为示例,说明如何在出现有趣的情况时通知用户发生在数据库中:
When the user is not actively using the app there is no reliable way to continue to listen to changes in the Firebase Realtime Database. To notify the user of changes to the database in that situation, you'll need to listen for those change on an environment that is always on, and then send a message to the user through Firebase Cloud Messaging.
One environment that is always in is Cloud Functions, which is also part of Firebase, and allows you to run small snippets of JavaScript code on Google's servers in response to things that happen in your Firebase project. The documentation of Cloud Functions for Firebase as example of how to notify the user when something interesting happens in the database:
您可以使用/编写firebase云功能。使用 firebase 云函数,您可以观看任何文档/字段,并尝试编写触发逻辑,例如如果字段设置为 true,则此云函数将通过 firebase 消息传递发出通知。
https://firebase.flutter.dev/docs/functions/overview/
You can use/write firebase cloud fucntion. Using firebase cloud function you can watch any document/field and try to write trigger logic like if a field is set to true then this cloud function will throw an notification via firebase messaging.
https://firebase.flutter.dev/docs/functions/overview/