数据库应该如何用于firebase中的一对一聊天应用程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库应该如何用于firebase中的一对一聊天应用程序相关的知识,希望对你有一定的参考价值。

这是我目前的数据库结构,它不起作用,我已经知道它不是实现我想要的正确方法......


"Messages" : 
"+918105571584" : 
  "+919945342730" : 
    "-L5AFX_RMebwd3EiuWUm" : 
      "message" : "Hi",
      "seen" : false,
      "time" : 1518459824840,
      "type" : "text"
    ,
    "-L5AFah6msuwW2fCXnMg" : 
      "message" : "Hey",
      "seen" : false,
      "time" : 1518459841703,
      "type" : "text"
    ,
    "-L5AFbmLTctRBxXjAkDW" : 
      "message" : "Message",
      "seen" : false,
      "time" : 1518459846120,
      "type" : "text"
    
  
,
"+919945342730" : 
  "+918105571584" : 
    "-L5AFX_RMebwd3EiuWUm" : 
      "message" : "Hi",
      "seen" : false,
      "time" : 1518459824840,
      "type" : "text"
    ,
    "-L5AFah6msuwW2fCXnMg" : 
      "message" : "Hey",
      "seen" : false,
      "time" : 1518459841703,
      "type" : "text"
    ,
    "-L5AFbmLTctRBxXjAkDW" : 
      "message" : "Message",
      "seen" : false,
      "time" : 1518459846120,
      "type" : "text"
    
  

,
"Users" : 
"+918105571584" : 
  "Email" : "",
  "Name" : "Akash",
  "Quote" : "",
  "Status" : ""
,
"+919945342730" : 
  "Email" : "",
  "Name" : "Sav",
  "Quote" : "",
  "Status" : ""



首先是一个孩子的消息和里面的发件人ID(电话号码)接收者ID(电话号码)然后唯一的ID包含消息...所以如果这不是正确的方式来实现一对一的聊天应用程序可以有人打电话给我如何我数据库结构应该是实现的

聊天活动

public class Chat extends AppCompatActivity 


Button addImage;
Button sendButton;
EditText messageArea;
ScrollView scrollView;
TextView name;
FirebaseAuth mAuth;
String MessageSenderId;
String MessageRecieverId;
DatabaseReference rootRef;
RecyclerView userMessageList;
private final List<Messages> messagesList = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private MessageAdapter messageAdapter;
FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    sendButton = (Button) findViewById(R.id.send_message);
    messageArea = (EditText) findViewById(R.id.input_message);
    scrollView = (ScrollView) findViewById(R.id.scrollView);
    addImage = (Button)findViewById(R.id.add_image);
    name = (TextView) findViewById(R.id.name);

    name.setText((getIntent().getStringExtra("name")));

    messageAdapter = new MessageAdapter(messagesList);
    userMessageList = (RecyclerView)findViewById(R.id.messageList);
    linearLayoutManager = new LinearLayoutManager(this);

    userMessageList.setHasFixedSize(true);
    userMessageList.setLayoutManager(linearLayoutManager);
    userMessageList.setAdapter(messageAdapter);


    rootRef = FirebaseDatabase.getInstance().getReference();
    user  = FirebaseAuth.getInstance().getCurrentUser();
    mAuth = FirebaseAuth.getInstance();

    MessageSenderId = mAuth.getCurrentUser().getPhoneNumber();
    MessageRecieverId = getIntent().getStringExtra("visit_user_id");




    fetchMessages();


    sendButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            sendMessage();
        
    );


private void fetchMessages() 
    rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId)
            .addChildEventListener(new ChildEventListener() 
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) 

                    Messages messages = dataSnapshot.getValue(Messages.class);
                    messagesList.add(messages);
                    messageAdapter.notifyDataSetChanged();
                

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) 



                

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) 

                

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) 

                

                @Override
                public void onCancelled(DatabaseError databaseError) 
                    throw databaseError.toException();
                
            );



private void sendMessage() 
 String messageText = messageArea.getText().toString();
 if (TextUtils.isEmpty(messageText))
     Toast.makeText(getApplicationContext(),"Can't Send Blank Message",Toast.LENGTH_SHORT).show();
    
    else 
     String message_sender_ref = "Messages/" + MessageSenderId + "/" + MessageRecieverId;
     String message_reciver_ref = "Messages/" + MessageRecieverId + "/" + MessageSenderId;

     DatabaseReference user_message_key = rootRef.child("Messages").child(MessageSenderId)
                                                 .child(MessageRecieverId).push();
     String message_push_id = user_message_key.getKey();

     Map messageTextBody = new HashMap<>();
     messageTextBody.put("number",MessageSenderId);
     messageTextBody.put("message",messageText);
     messageTextBody.put("seen",false);
     messageTextBody.put("type","text");
     messageTextBody.put("time", ServerValue.TIMESTAMP);



     Map messageBodyDetails = new HashMap();

     messageBodyDetails.put(message_sender_ref + "/" + message_push_id,messageTextBody);
     messageBodyDetails.put(message_reciver_ref + "/" + message_push_id,messageTextBody);

     rootRef.updateChildren(messageBodyDetails, new DatabaseReference.CompletionListener() 
         @Override
         public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) 
             if (databaseError != null)
                 Log.d("Chat_Log", databaseError.getMessage().toString());
             
             messageArea.setText("");
         
     );

 

答案

试试这个:

  Messages
        uniqueid
           number:"+918105571584"
           "message" : "Hi",
           "seen" : false,
           "time" : 1518459824840,
           "type" : "text"
        uniqueid
            number: "+919945342730"
             "message" : "Hey",
             "seen" : false,
             "time" : 1518459841703,
             "type" : "text"

等等..

保持Users节点。

尝试以上操作,具有第一个号码的用户使用第二个号码向用户发送消息。发送消息后,此数据将显示在您的数据库中:

    uniqueid
        number:"+918105571584"
       "message" : "Hi",
       "seen" : false,
       "time" : 1518459824840,
       "type" : "text"

当其他用户看到该消息时,您可以将seen更新为true。

然后第二个用户发送一条消息,他的数据也将出现在数据库中。

     uniqueid
            number: "+919945342730"
             "message" : "Hey",
             "seen" : false,
             "time" : 1518459841703,
             "type" : "text"

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("messages").push();
ref.child("number").setValue(anumberhere);
ref.child("message").setValue(messagehere);
ref.child("seen").setValue(false);
....

以上是关于数据库应该如何用于firebase中的一对一聊天应用程序的主要内容,如果未能解决你的问题,请参考以下文章

用于聊天应用的 Firebase 实时数据库或 Firestore? [关闭]

使用 Firebase 的 Android 聊天应用程序,但未在 firebase 中存储数据

如何使用 Firebase 规则从一侧删除消息?

android firebase 一对一聊天应用

将数据从 FireBase 检索到颤振项目时出错

我应该或不应该如何一起使用 Cassandra 和 Redis 来构建可扩展的一对一聊天应用程序?