使用 Firebase 实时数据库进行 Flutter 通知

发布于 2025-01-13 08:12:03 字数 324 浏览 3 评论 0原文

我有一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2025-01-20 08:12:03

当用户没有主动使用应用程序时,没有可靠的方法可以继续侦听 Firebase 实时数据库中的更改。要在这种情况下通知用户数据库的更改,您需要在始终开启的环境中侦听这些更改,然后通过 Firebase Cloud Messaging 向用户发送消息。

Cloud Functions 是一种始终存在的环境,它也是 Firebase 的一部分,允许您在 Google 服务器上运行一小段 JavaScript 代码,以响应 Firebase 项目中发生的情况。 Cloud Functions for Firebase 文档作为示例,说明如何在出现有趣的情况时通知用户发生在数据库中

开发人员可以使用 Cloud Functions 来保持用户的参与度并及时了解应用的相关信息。例如,考虑一款允许用户在应用程序中跟踪彼此活动的应用程序。每次用户将自己添加为另一个用户的关注者时,实时数据库中都会发生写入。然后,此写入事件可以触发一个函数来创建 Firebase 云消息传递 (FCM) 通知,让相应的用户知道他们已经获得了新的关注者。

显示下述应用流程的图表

  1. 该函数会在写入存储关注者的实时数据库路径时触发。
  2. 该函数编写一条消息并通过 FCM 发送。
  3. FCM 将通知消息发送到用户的设备。

要查看工作代码,请参阅发送 FCM 通知 .

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:

Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.

Diagram showing the app flow described below

  1. The function triggers on writes to the Realtime Database path where followers are stored.
  2. The function composes a message to send via FCM.
  3. FCM sends the notification message to the user's device.

To review working code, see Send FCM notifications.

没有心的人 2025-01-20 08:12:03

您可以使用/编写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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文