如何将已打开房间的聊天存储在Firebase中的独特位置,然后检索它们? [英] How can I store the opened room's chat at unique locations in Firebase and then retrieve them?

查看:108
本文介绍了如何将已打开房间的聊天存储在Firebase中的独特位置,然后检索它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做:假设有5个聊天室,用户可以通过按下相应的按钮进入任何聊天室聊天。所以,他应该只显示那个房间的聊天,而不是整个聊天。在Firebase数据库的参考文献中,应该存储这些聊天记录,以便每当用户进入房间时,他只会显示该聊天室的内容? 是通过生成时间戳,并将其存储在Firebase数据库下的聊天,然后从那里获取聊天,但是当我试图这样一个新的时间戳得到每一次生成和前一个聊天存储在数据库中是迷失方向。



以下是我如何生成时间戳:

  Long tsLong = System.currentTimeMillis()/ 1000; 
String ts = tsLong.toString();

以下是我在其下存储聊天的方式:

  mFirebaseDatabaseReference.child(chatmodel)。child(ts).setValue(model); 

我该如何做到这一点?请帮助我。

解决方案

为了达到这个目的,我建议你重新构建你的数据库。避免过多的嵌套,并发布聊天室/聊天请求,如下所示:



所以在这里您可以看到两个聊天室由生成的唯一标识。 push()在发布请求时。用关键字 uniqueCharacter 请求一个唯一标识符。

  {
-HFSSFG5647DDHD:{
name:Some Name,
someOtherinfo:test,
timestamp:12443567,
uniqueCharacter:Request2
},
-YYTH6637FRFSHt5:{
name:Some other name,
someOtherinfo:test,
timestamp:12443567,
uniqueCharacter:Request3
}





$ b现在,其他用户看到这两个请求,一旦他点击了一些 你得到推送时生成的唯一ID,并使用如下过滤器为特定的推送ID创建单独的聊天室:



如果用户选择 request3 ,那么您的数据库引用将是:

  databaseRef.orderByChild( uniqueCharacter)equalTo( Request3); 

这会让您访问您可以分享的特定推送ID所有那些谁选择了这一点,并创建一个共同的聊天室的所有人相同的ID。



希望你在这里得到基本的想法实现你想要的。



这不是一个生产例子,只是给你一个关于实现你的目标的更好方法的基本概念。


I want to do something like this: suppose there are 5 chat rooms and the user can enter and chat in any chat room by pressing the respective button. So, he should be shown the chats of that room only and not overall chat. At what reference in the Firebase Database do these chats should be stored so that every time when user enters a room, he is shown only that room's chat?

One way I thought is by generating timestamp and storing the chat under it in Firebase Database and then fetching the chats from there, but when I'm trying to this a new timestamp is getting generated every time and the previous one under which chats are stored in the database is getting lost.

Here's how I'm generating the timestamp:

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();

Here's how I'm storing chat under it:

mFirebaseDatabaseReference.child("chatmodel").child(ts).setValue(model);

How do I achieve this? Please help me.

解决方案

To achieve this I would suggest you to re-structure your database. Avoid too much nesting and post the chat rooms/chat requests like this:

So here you see two chat rooms under unique ids generated by .push() at the time of posting request. With one unique identifier for request with key uniqueCharacter.

{
      "-HFSSFG5647DDHD" : {
        "name" : "Some Name",
        "someOtherinfo" : "test",
        "timestamp" : 12443567,
        "uniqueCharacter" : "Request2"
      },
      "-YYTH6637FRFSHt5" : {
        "name" : "Some other name",
        "someOtherinfo" : "test",
        "timestamp" : 12443567,
        "uniqueCharacter" : "Request3"
      }
    }

Now the other user sees both these requests and once he clicks on some you get the unique id generated at the time of pushing and create a separate chat room for that particular push id using filters like this:

If user selected request3 then your database ref will be:

databaseRef.orderByChild("uniqueCharacter").equalTo("Request3");

This will give you access to that particular push id which you can share among all those who chose this and create a common chat room with same id for all of them.

Hope you get the basic idea here on achieving what you want.

This is not a production example but just a basic one to give you an idea about better way of achieving your goal.

这篇关于如何将已打开房间的聊天存储在Firebase中的独特位置,然后检索它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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