Flutter:如何将Firebase云消息传递与ChatApp集成 [英] Flutter: How Do I Integrate Firebase Cloud Messaging with a ChatApp

查看:174
本文介绍了Flutter:如何将Firebase云消息传递与ChatApp集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在聊天应用程序上,消息存储在Firestore中,我希望消息的接收者在发件人发送消息时得到通知.我知道通过Firebase Cloud Messaging实现此目标的唯一方法,但是我不知道该怎么做.

I'm actually working on a chat app, the messages are stored in Firestore, I want the receiver of the message to get a notification when a message is sent by the sender. I know this only way to achieve this is through Firebase Cloud Messaging but I don't know how to go about it.

我该怎么办?

注意:我是Flutter的新手.预先感谢.

NB: I'm new to Flutter. Thanks in advance.

推荐答案

FCM 是一种方法,但这不是唯一的方法-尽管如此,我认为这是一种好方法! :)

FCM is one way, but it's not the only way - nevertheless I think it's a good way! :)

看看 firebase_messaging 插件.

我认为最简单的方法是使聊天应用程序独立于FCM运行(就像您可能已经在做的那样),这意味着消息存储在Firestore中,并且您不使用FCM来传输消息.

I guess the easiest way to do it would be making your chat app working independently of FCM (as you probably are already doing), which means the messages are saved in firestore and you don't use FCM to transfer the messages.

然后,您可以在Cloud Function中设置一个Firestore触发器,以自动将通知发送给客户端.您将设置一个消息侦听器,并将消息添加到Firestore中后,云功能将发布通知.

Then you could set up a Firestore trigger in a Cloud Function to automatically send notifications to the clients. You would set up a listener to the messages, and as they get added to firestore the cloud function would post the notifications.

在应用程序中,仅当应用程序不在前台时才显示通知.处理onResumeonLaunch将应用指向正确的对话.

In the app, you would only show the notifications when the app is not in the foreground. Handling onResume and onLaunch to point the app to the right conversation.

在处理Firestore触发器的云功能中,可以通过其令牌ID检测到客户端,或者您可以使客户端订阅某些主题(例如,聊天室ID),并且触发器/功能可以将通知发布给他们.

In the cloud function that handles the Firestore trigger, the clients could be detected by their token IDs, or you can make the clients to subscribe to certain topics (the chat room ID, for instance), and the trigger/function would post the notification to them.

使用令牌要困难得多,因为您将必须在所有客户端中检测令牌ID,将其作为用户的一部分保存在firestore中,并随着时间的推移而不断更新.然后,要发送,您将必须向每个tokenID发布单独的通知.

Using the tokens is way harder, as you would have to detect the token ID in all clients, save them in firestore as part of the user and keep them updated, as they change through time. And then, to send, you would have to post a separate notification to every tokenID.

使用主题更容易,客户端应用程序只需使用

Using the topics is easier, the client app would only have to use the subscribeToTopic method to listen to notifications on a given topic (in this case chat room). So when a message is detected by the cloud function, you only post one notification to its topic and all subscribers would get notifications.

很抱歉,如果这是一个非常肤浅的解释,但确实不是一个非常简单的主题. :)

Sorry if it's a very superficial explanation, but it's indeed not a very simple subject. :)

这篇关于Flutter:如何将Firebase云消息传递与ChatApp集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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