问题描述 投票:0回答:6
-where

-kezmqxsdkcnffa432uc

-kfcewklg_y3nardiuiy

-kezfdsaadfasffs3221

-kasdf32324sdfasd1fsd1fsd1fsd1fsd1fsd1fsd1fsd1fs 我的问题是,我在“消息”节点中创建了一个ChildEventListener来接收新用户消息,但是我正在接收所有用户的所有新消息(我每个应用程序都登录一个用户),因为我的孩子列表在“消息”中节点。 正确的是,如果我在添加消息时有1000个用户,那么一条新消息会触及1000个用户? (假设在应用程序中,您可以检查该消息所属的用户)。 thanks! 如果您做的结构与此类似:

-chats - chatUID - members - userUID - lastMessageSent:messageUID - ... more properties -chatMessages - chatUID - messageUID - sentBy: userUID - messageDate:"" - messageTime:"" - message:"" -userChats - userUID - chatUID

您可以将侦听器附加到 /userChats /useruid,它将显示活动聊天,以及侦听器 /chatmessages /chatuid,它将为特定的聊天对话获取所有聊天消息。

这种方法更容易设置Firebase安全规则,用户只会接收他们与众不同的聊天消息。

thanks to @linxy寻求一个出色的答案

我已经创建了一个有关@linxy答案

的firebase数据库
android firebase firebase-realtime-database structure
6个回答
59
投票

在这里是完整的JSONExport

{ "Chats" : { "-Lsfsd234xda" : { "lastMessageSent" : "-LrDEBo1-Message", "members" : [ "-LrDEBoLokW-5mhaT3ys", "-LrDEBoLokW-5mhaT3yz" ], "more_properties" : "goes here" } }, "Users" : { "-LrDEBoLokW-5mhaT3ys" : { "id" : "-LrDEBoLokW-5mhaT3ys", "userDisplayName" : "Qadir Hussain", "userEmail" : "[email protected]", "userPhotoUrl" : "https://lh3.googleusercontent.com/a-/AAuE7XXXXXXXXX" }, "-LrDEBoLokW-5mhaT3yz" : { "id" : "-LrDEBoLokW-5mhaT3yz", "userDisplayName" : "Ishaq Bhojani", "userEmail" : "[email protected]", "userPhotoUrl" : "https://lh3.googleusercontent.com/a-/AAuE7mB3KTbXXXXXXXX" } }, "chatMessages" : { "-Lsfsd234xda" : { "-LrDEBo-MessageUID" : { "message" : "Hi there!", "messageDate" : "10/10/2019", "messageTime" : "10:16pm", "sentBy" : "-LrDEBoLokW-5mhaT3ys" }, "-LrDEBo1-MessageUID" : { "message" : "Hello", "messageDate" : "10/10/2019", "messageTime" : "10:17pm", "sentBy" : "-LrDEBoLokW-5mhaT3yz" } } }, "userChats" : { "-LrDEBoLokW-5mhaT3ys" : { "0" : "-Lsfsd234xda", "1" : "-Lsfsd234xda1", "chatUID" : "-Lsfsd234xda" } } }


7
投票
我知道回答已经很晚了,但是对于未来的读者来说,尽管Linxy的答案是整洁的,但我想指出一个更有效的读者已经尝试过两种结构:

ChatMessages smallerUID_biggerUID messageUID sentBy : userUID messageDate : "" message : "" . . . . UserChats userUID pairUID lastMessage : "" . . . .

在这样,我们可以直接搜索哪些用户应出现在我们的Active Chat选项卡中并获取Thouse用户的信息(用户名,ProfileLepicture)。这样做的原因是,如果我们知道要与之发送消息的用户ID,我们始终可以计算chatid。因此,对于“消息”选项卡,我们计算客户端中的chatid(smalluid_biggeruid),并在引用它时搜索消息。

为了构建数据库的顺序,请阅读此帖子:enter image description here为复杂的应用程序正确架构壁垒数据。您会在这里找到您问题的答案。 作为结论,请尽可能地将数据库平整(非正式化)。

希望它有帮助

这种结构不支持您想做的事情,最好通过使用频道之类的内容来更改它,其中频道包含两个人之间的消息,因此,当他们中的任何一个发送消息时,将通知另一个消息。 

5
投票

{ "users": { "userId": { "conversations": { "conversationId": { "unseenCount": 0 }, "conversationId2": { "unseenCount": 3 } } }, "conversations": { "conversationId": { "displayedMessage": "Message", "members": { "userId1": true, "userId2": true }, "messages": { "messageId": { "type": "text", "text": "Hello", "createdAt": "", "senderId": "userId", "status": "sent", "payload": "" } }, "lastMessage": "my last message" } }

}

ImportFirebaseadatabase


0
投票
{ messages: { A8Fcn28ak9ask46: { chat_id: "combination of sender and receivers number", sender_id: "person sending the message", receiver_id: "person send it to", text: "message that the user types", timestamp: "123981849404" }, ... } }

当您获得结果时,您可以通过向前和反向进行过滤,这将获得两个人之间的对话。

0
投票
希望它有帮助
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.