Firebase Android聊天实时功能无法正常工作

时间:2016-11-26 12:18:24

标签: android firebase firebase-realtime-database

我目前正在开发Firebase聊天应用程序。我下载了一个演示并尝试完成它(http://myapptemplates.com/simple-android-chat-app-tutorial-firebase-integration/

我能够发送和接收消息,但问题是我必须实际退出聊天框并再次加载以查看新消息。另一件事是所有注册用户都发送和接收相同的消息,而不是仅发送和接收一个接收者和发送者。我是Firebase的新手,所以我真的很难修复它。

以下是sendMessage()功能:

    private void sendMessage() {
    if (txt.length() == 0)
        return;

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(txt.getWindowToken(), 0);

    String s = txt.getText().toString();

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if(user != null) {
        final Conversation conversation = new Conversation(s,
                Calendar.getInstance().getTime(),
                user.getUid(),
                buddy.getId(),
                "");
        conversation.setStatus(Conversation.STATUS_SENDING);
        convList.add(conversation);
        final String key = FirebaseDatabase.getInstance()
                .getReference("messages")
                .push().getKey();
        FirebaseDatabase.getInstance().getReference("messages").child(key)
                .setValue(conversation)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                           @Override
                                           public void onComplete(@NonNull Task<Void> task) {
                                               if (task.isSuccessful()) {
                                                   convList.get(convList.indexOf(conversation)).setStatus(Conversation.STATUS_SENT);
                                               } else {
                                                   convList.get(convList.indexOf(conversation)).setStatus(Conversation.STATUS_FAILED);
                                               }
                                               FirebaseDatabase.getInstance()
                                                       .getReference("messages")
                                                       .child(key).setValue(convList.get(convList.indexOf(conversation)))
                                                       .addOnCompleteListener(new
                                                                                      OnCompleteListener<Void>() {
                                                                                          @Override
                                                                                          public void onComplete(@NonNull Task<Void> task) {

                                                                                              adp.notifyDataSetChanged();
                                                                                          }
                                                                                      });

                                           }
                                       }
                );
    }
    adp.notifyDataSetChanged();
    txt.setText(null);
}

以下是loadConversationList()

    FirebaseDatabase.getInstance(). getReference("messages").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if(user != null) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    Conversation conversation = ds.getValue(Conversation.class);
                    if ((conversation.getReceiver().contentEquals(user.getUid()) && conversation.getSender().contentEquals(buddy.getId()))
                            || (conversation.getSender().contentEquals(user.getUid()) && (conversation.getReceiver().contentEquals(buddy.getId())))) {
                        convList.add(conversation);
                        if (lastMsgDate == null
                                || lastMsgDate.before(conversation.getDate()))
                            lastMsgDate = conversation.getDate();

                        adp.notifyDataSetChanged();

                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

您已添加SingleValueListener。这将只读取一次数据。要获得实时功能,您应该添加ValueEventListener,如下所示:

FirebaseDatabase.getInstance(). getReference("messages").addValueEventListener(new ValueEventListener() {