是使用电话身份验证开发聊天应用程序所需的朋友功能

时间:2018-03-20 19:10:35

标签: java android firebase firebase-realtime-database

我在youtube,google和github上看过很多关于如何制作聊天应用程序的聊天应用程序示例。我使用了相同的逻辑并创建了一个应用程序,但无法接收来自其他用户的消息。喜欢它被保存在数据库中但无法检索它。它只是让我大吃一惊,我看到的几乎所有例子都有发送和接受好友请求的功能。是使用firebase制作应用程序的必修课。就像为什么我无法检索其他用户发送的消息一样?

请帮助我,我已经被困在这几个月了

如果你想看看我如何检索信息......这里是它的代码

private void fetchMessages() {
    rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ListView listView = (ListView) findViewById(R.id.messageList);
            ArrayAdapter<String> adapter;
            adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, messagesList);
            String message = dataSnapshot.getValue().toString();
            message = dataSnapshot.child("message").getValue().toString();
//                Log.d("TAG",message );
            messagesList.add(message);
            adapter.notifyDataSetChanged();
            listView.setAdapter(adapter);
        }

        @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) {

        }
    });

}

数据库

{
"Messages" : {
"8hbPUUlw39OABqmqSJ63BAHFjan2" : {
  "OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
    "-L7tuwuIKTkaskiJxU0c" : {
      "message" : "Hii",
      "seen" : false,
      "time" : 1521390178351,
      "type" : "text"
    },
    "-L7u3Z8fnmwKpyvf-0Qy" : {
      "message" : "hi",
      "seen" : false,
      "time" : 1521392698442,
      "type" : "text"
    },
    "-L7u3oT5nGW1181Hfh0g" : {
      "message" : "Ufff",
      "seen" : false,
      "time" : 1521392765184,
      "type" : "text"
    },
    "-L7uFBt6kECyA7FvUmhm" : {
      "message" : "hhfjgivugihihjihycvyuhuhuhi",
      "seen" : false,
      "time" : 1521395748801,
      "type" : "text"
    }
  }
},
"OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
  "8hbPUUlw39OABqmqSJ63BAHFjan2" : {
    "-L7tuwuIKTkaskiJxU0c" : {
      "message" : "Hii",
      "seen" : false,
      "time" : 1521390178351,
      "type" : "text"
    },
    "-L7u3Z8fnmwKpyvf-0Qy" : {
      "message" : "hi",
      "seen" : false,
      "time" : 1521392698442,
      "type" : "text"
    },
    "-L7u3oT5nGW1181Hfh0g" : {
      "message" : "Ufff",
      "seen" : false,
      "time" : 1521392765184,
      "type" : "text"
    },
    "-L7uFBt6kECyA7FvUmhm" : {
      "message" : "hhfjgivugihihjihycvyuhuhuhi",
      "seen" : false,
      "time" : 1521395748801,
      "type" : "text"
    }
  },
  "OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
    "-L7u8PbR_UNNxQT93sEt" : {
      "message" : "Hi",
      "seen" : false,
      "time" : 1521393970012,
      "type" : "text"
    }
  }
}
},
"Users" : {
"8hbPUUlw39OABqmqSJ63BAHFjan2" : {
  "Image" : "default",
  "Name" : "Akash",
  "Status" : "Hey",
  "Thumb_image" : "default",
  "Unique" : "8hbPUUlw39OABqmqSJ63BAHFjan2"
},
"OcAXeb0KAzfkXSNXyDddAOpBdyh1" : {
  "Image" : "default",
  "Name" : "Sh",
  "Status" : "Hey",
  "Thumb_image" : "default",
  "Unique" : "OcAXeb0KAzfkXSNXyDddAOpBdyh1"
}
}
}

所有用户活动

atabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");

    ValueEventListener eventListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            ListView listView = (ListView) rootView.findViewById(R.id.usersList);
            ArrayAdapter<String> adapter;
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, al);
            for(DataSnapshot ds : dataSnapshot.getChildren()) {

                unique = ds.child("Unique").getValue().toString();
                name = ds.child("Name").getValue().toString();
                list_user_id = ds.getKey();


                al.add(name);



            }
            listView.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(),"Please Check Your Internet Connection",Toast.LENGTH_SHORT).show();
        }
    };
    usersRef.addListenerForSingleValueEvent(eventListener);

     usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            UserDetails.chatWith = al.get(position);
            String user_id = al.get(position);
            Intent intent = new Intent(getActivity(), Chat.class);
            intent.putExtra("visit_user_id",unique);
            intent.putExtra("user_name",name);
            startActivity(intent);
            Log.d("TAG",unique);
        }
    });
    return rootView;

1 个答案:

答案 0 :(得分:0)

首先要做的是停止忽略错误:

public void onCancelled(DatabaseError databaseError) { 
  throw databaseError,toException(); 
} 

这可能会发现有关您的应用无法正常运作的重要信息。

接下来尝试在调试器下运行代码。你的onChildAdded被触发了吗?如果是这样,消息是否会获得您期望它拥有的价值?

第三步是将创建适配器的代码移出onChildAdded。现在你正在为每条消息创建一个新的适配器,这意味着你最多只能显示一条消息。

ListView listView = (ListView) findViewById(R.id.messageList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, messagesList);
listView.setAdapter(adapter);

rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String message = dataSnapshot.child("message").getValue().toString();
        messagesList.add(message);
        adapter.notifyDataSetChanged();
    }
    ...