使用Firebase向自定义用户发送消息 [英] Sending message to custom user using firebase

查看:70
本文介绍了使用Firebase向自定义用户发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Id喜欢在两个自定义用户之间进行聊天.我需要向自定义用户发送一条消息.情况:

  1. User1从我的服务器获取User2 ID
  2. 我的服务器从Firebase获取User2 ID(可能使用Firebase管理员)
  3. 我的服务器将User2 ID返回给User1
  4. User1通过Firebase向User2发送消息

有可能做到吗?我必须使用Firebase Admin + FCM还是其他工具?

解决方案

[在此处为您提供简单的指导和逻辑]

最简单的实现方法之一是使用 Cloud Firestore .

您需要创建 2个表,一个用于 users ,另一个用于 chat .用户将用于存储用户信息,包括 UID 电子邮件朋友(数组,用于存储朋友列表), chats (数组,用于存储聊天列表)等.而聊天将用于存储所有聊天.(您可以设计自己的数据库,可以在此处找到一些指南)/p>

当用户使用Firebase Auth登录时,您可以查询数据库以从Firestore获取详细信息.假设您将所有内容都放在某个UI元素( recyclerview )中,则一旦单击用户的朋友(朋友存储在上述Cloud Firestore中),就会在聊天数据库/中创建一个新的聊天文档/表,并在用户表/数据库中的另一个用户(用户2)文档记录下,使用新的文档ID更新聊天数组.对于User 2 UI界面,将创建/更新新的聊天,因为Firebase具有类似onChangeListener()的功能来更新您的客户端.

您将需要为聊天设计UI,以允许用户发送消息.UI将需要从聊天文档中获取所有聊天记录.当任何一方发送新消息时,请将其更新为聊天文档.

所以基本上这是一个粗略的主意,您可以从Firebase Doc中获取所有这些引用.

有关通知,您可以使用Firebase Cloud Messaging通知用户!

文档中规定的FCM的实际功能:

使用FCM,您可以通知客户端应用程序新电子邮件或其他数据正在可同步.您可以发送通知消息来吸引用户重新参与和保留.对于即时消息等用例,一条消息最多可以将4KB的有效负载传输到客户端应用程序.

因此,对于某些事件,它不是针对用户的,而是针对应用程序管理员的用户.

对于Admin,建议不要始终为生产消费者应用程序调用Admin.

P/s:我不是随便喂代码,而是鼓励人们学习自己的研究.

希望有帮助!

Id like to make a chat between two custom users. I need to send an message to custom user. scenario:

  1. User1 get User2 ID from my server
  2. My server get User2 ID from Firebase (probably using Firebase admin)
  3. My server return User2 ID to User1
  4. User1 send message to User2 via firebase

Is it possible to make? Do I have to use Firebase Admin + FCM or something else?

解决方案

[Giving you a simple guideline and logic here]

One of the simplest ways to implement it is by using Cloud Firestore.

You need to create 2 tables, one for the users and one for the chat. The users will be used to store the users' info including UID, emails, friends (array, to store a list of friends), chats (array, to store a list of chats), etc. While the chat will be used to store all the chats. (You can design your own database, some guideline can be found here)

When the user signed-in with Firebase Auth, you can query the database to get the details from the Firestore. Assuming you put everything in some UI element (recyclerview), once you click on the user's friend (friends are stored in the Cloud Firestore mentioned above), a new chat document will be created in the chat database/table, and under the other user (User 2) document record in the users table/database, update the chat array with the new document ID. For the User 2 UI interface, new chat will be created/updated as Firebase has something like onChangeListener() to update your client.

You will need to design your UI for the chat allowing the user to send messages. The UI will need to fetch all the chat records from the chat document. When any party sends a new message, update the message to the chat document.

So basically that's the rough idea, you can get all those references from Firebase Doc.

For notification, you can use Firebase Cloud Messaging to notify the user!

The actual function of FCM as stated in the doc:

Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.

Therefore, it is not intended user to user but is for the App admin to the user for certain events.

For Admin, it is not encouraged to call Admin all the time for a production consumer app.

P/s: I don't spoon-feed with code but to encourage people to learn from their own research.

Hope it helps!

这篇关于使用Firebase向自定义用户发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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