我正在使用firebase在android上开发一个聊天应用程序

时间:2017-02-20 11:06:41

标签: android firebase chat

我希望通过此命令保存两个用户之间的聊天

mFirebaseDatabaseReferenceMessage.child("messages").child(mEmail).child(currentReceiverEmail).push().setValue(messages);

消息对象

private String id;
private String text;
private String senderEmail;
已创建

消息节点但发件人的 mEmail 电子邮件和接收者的 currentReceiverEmail 电子邮件不是。我希望在两个用户第一次相互聊天时创建这两个节点,并在此节点中保存以后的聊天。有什么方法可以做到。我正在使用这种结构,以便我可以轻松地检索任何两个用户之间的聊天。 如果有更好的方法在firebase上的两个用户之间存储聊天,请提及它。

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:-1)

使用此

   public class Activity_Message extends AppCompatActivity implements View.OnClickListener {


EditText msg;
String To_Name,To_Id;
String My_Name,My_Id;
Firebase reference1, reference2;



@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.message_list);

    Firebase.setAndroidContext(this);
    reference1 = new Firebase("https://chatapp-e1eabdgdfg.firebaseio.com/Message/" + My_Name + "_" + To_Name);
    reference2 = new Firebase("https://chatapp-e1eabgfggf.firebaseio.com/Message/" + To_Name + "_" + My_Name);
    initViews();

    loadData();
}



private void initViews() {


    msg = (EditText)findViewById(R.id.msg_edit);
    ImageView send = (ImageView)findViewById(R.id.send_btn);
    send.setOnClickListener(this);




    reference1.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            //get data here


        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            //Log.d("fdfddhjChild Changed",dataSnapshot.getValue().toString());
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }


    });



}

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.send_btn:

            String messageText = msg.getText().toString();
            if(!messageText.equals("")){
                Map<String, String> map = new HashMap<String, String>();
                map.put("message", messageText);
                map.put("from_id", My_Id);
                map.put("to_id", To_Id);
                map.put("date_time", currDate);

                reference1.push().setValue(map);
                reference2.push().setValue(map);

            }

            msg.setText("");

            break;

    }
}

}

答案 2 :(得分:-1)

public class Activity_Message extends AppCompatActivity implements View.OnClickListener {


EditText msg;
String To_Name,To_Id;
String My_Name,My_Id;
Firebase reference1, reference2;



@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.message_list);

    Firebase.setAndroidContext(this);
    reference1 = new Firebase("https://chatapp-e1eabdgdfg.firebaseio.com/Message/" + My_Name + "_" + To_Name);
    reference2 = new Firebase("https://chatapp-e1eabgfggf.firebaseio.com/Message/" + To_Name + "_" + My_Name);
    initViews();

    loadData();
}



private void initViews() {


    msg = (EditText)findViewById(R.id.msg_edit);
    ImageView send = (ImageView)findViewById(R.id.send_btn);
    send.setOnClickListener(this);




    reference1.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            //get data here


        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            //Log.d("fdfddhjChild Changed",dataSnapshot.getValue().toString());
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }


    });



}

@Override
public void onClick(View v) {
    switch (v.getId()){

        case R.id.send_btn:

            String messageText = msg.getText().toString();
            if(!messageText.equals("")){
                Map<String, String> map = new HashMap<String, String>();
                map.put("message", messageText);
                map.put("from_id", My_Id);
                map.put("to_id", To_Id);
                map.put("date_time", currDate);

                reference1.setValue(map);
                reference2.setValue(map);





            }

            msg.setText("");

            break;

    }
}
}

试试这个代码......