电话身份验证,登录,获取所有用户和发送消息似乎都很好。但是我无法从其他用户那里收到这些消息。哪里出错了?
ChatActivity:
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;
@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")));
userMessageList = (RecyclerView)findViewById(R.id.messageList);
messageAdapter = new MessageAdapter(messagesList);
linearLayoutManager = new LinearLayoutManager(this);
userMessageList.setHasFixedSize(true);
userMessageList.setLayoutManager(linearLayoutManager);
userMessageList.setAdapter(messageAdapter);
rootRef = FirebaseDatabase.getInstance().getReference();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
mAuth = FirebaseAuth.getInstance();
MessageSenderId = mAuth.getCurrentUser().getUid();
MessageRecieverId = getIntent().getExtras().get("visit_user_id").toString();
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) {
}
});
}
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("message",messageText);
messageTextBody.put("seen",false);
messageTextBody.put("type","text");
messageTextBody.put("time", ServerValue.TIMESTAMP);
messageTextBody.put("from", MessageSenderId);
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("");
}
});
}
}
}
JSON
{
"Messages" : {
"8hbPUUlw39OABqmqSJ63BAHFjan2" : {
"Savitha" : {
"-L4Vrd8ZrlgX94RbAS6W" : {
"from" : "8hbPUUlw39OABqmqSJ63BAHFjan2",
"message" : "Hello",
"seen" : false,
"time" : 1517748655103,
"type" : "text"
},
"-L4VrdyO1mdQdER9BuZ2" : {
"from" : "8hbPUUlw39OABqmqSJ63BAHFjan2",
"message" : "Nope?",
"seen" : false,
"time" : 1517748658463,
"type" : "text"
},
"-L4WUkbKnBU5GLLUOK3R" : {
"from" : "8hbPUUlw39OABqmqSJ63BAHFjan2",
"message" : "Oops",
"seen" : false,
"time" : 1517759171386,
"type" : "text"
}
}
},
"Akash" : {
"UXksdU8MBvMtO4Wsl9Bgg5JGsxI2" : {
"-L4VpxxLm2WwN-yogzrL" : {
"from" : "UXksdU8MBvMtO4Wsl9Bgg5JGsxI2",
"message" : "Hello",
"seen" : false,
"time" : 1517748215965,
"type" : "text"
},
"-L4VrZ6o7541T_gorBuB" : {
"from" : "UXksdU8MBvMtO4Wsl9Bgg5JGsxI2",
"message" : "Hiii",
"seen" : false,
"time" : 1517748634453,
"type" : "text"
}
}
},
"Savitha" : {
"8hbPUUlw39OABqmqSJ63BAHFjan2" : {
"-L4Vrd8ZrlgX94RbAS6W" : {
"from" : "8hbPUUlw39OABqmqSJ63BAHFjan2",
"message" : "Hello",
"seen" : false,
"time" : 1517748655103,
"type" : "text"
},
"-L4VrdyO1mdQdER9BuZ2" : {
"from" : "8hbPUUlw39OABqmqSJ63BAHFjan2",
"message" : "Nope?",
"seen" : false,
"time" : 1517748658463,
"type" : "text"
},
"-L4WUkbKnBU5GLLUOK3R" : {
"from" : "8hbPUUlw39OABqmqSJ63BAHFjan2",
"message" : "Oops",
"seen" : false,
"time" : 1517759171386,
"type" : "text"
}
}
},
"UXksdU8MBvMtO4Wsl9Bgg5JGsxI2" : {
"Akash" : {
"-L4VpxxLm2WwN-yogzrL" : {
"from" : "UXksdU8MBvMtO4Wsl9Bgg5JGsxI2",
"message" : "Hello",
"seen" : false,
"time" : 1517748215965,
"type" : "text"
},
"-L4VrZ6o7541T_gorBuB" : {
"from" : "UXksdU8MBvMtO4Wsl9Bgg5JGsxI2",
"message" : "Hiii",
"seen" : false,
"time" : 1517748634453,
"type" : "text"
}
}
}
},
"Users" : {
"8hbPUUlw39OABqmqSJ63BAHFjan2" : {
"Email" : "",
"Name" : "Akash",
"Quote" : "",
"Status" : ""
},
"UXksdU8MBvMtO4Wsl9Bgg5JGsxI2" : {
"Email" : "",
"Name" : "Savitha",
"Quote" : "",
"Status" : ""
}
} }
我猜数据库出了问题,就像我拿这个错误一样。
屏幕截图 - http://ibb.co/cwOQtx