Flutter Firestore 消息通知

Flutter firestore message notifications

我已经使用 Cloud firestore 实现了一个聊天应用程序。

流程是这样的:

  1. 有一个聊天记录集,其中保存了两个用户 ID。和聊天集合有内部消息集合,其中保存了所有消息。
  2. 两个用户可以立即互相发送消息。
  3. 而且任何用户都可以发起与任何其他用户的聊天。

我想知道什么?

我想在发送新消息时在接收方手机上发送通知。如果有人能理解这个问题。帮助将不胜感激<3。 谢谢。

我找到了this article that you might find useful, it is very detailed an even includes the code, basically the trick is to use Cloud Firebase Functions to listen any changes on the collection, in your specific use case, try to listen the onCreate trigger, that will be fired each time a user creates a new message (document) on the collection, more information on the Database Triggers can be found here

然后您只需从 onCreate 侦听器中触发 sendToDevice method 即可将消息推送到设备中。

Firebase 云函数可用于根据您自己的业务逻辑创建和触发函数。我假设您已经配置了 flutter 应用程序并将其与 firebase 连接,如果没有,下面的步骤也将指导您完成。

  1. This link here will guide you on how to start with cloud functions basically what all you need to do to set up the environment.

  2. This link is a complete example which demonstrates on how to push notifications to a particular user device using the device token whenever a new document is added to a collection.

  3. This is a youtube video for the above links.

请浏览链接并帮助自己。