简体   繁体   中英

Firebase : How to get other user's uid?

I've made an chat app just for me and my friend.

When I message them, I want to store their Uid .

How do I do that ? Also mention if its needed to reconstruct the entire structure.

I have thought of String[] uidList = DatabaseReference.child("Users").getKey();

The Messages are not created yet. I want to create them with friend_user_id which I can't retrieve at this moment.

Below is how I want My Firebase Database structure to look like this :

app-id:

    Users:
        my_user_id:
            name:"Me"
            last_seen:"MyLastSeenTimeValue"

        friend_user_id:
            name:"Friend"
            last_seen:"FriendLastSeenTimeValue"

    Messages:

        my_user_id:
            friend_user_id:(How to retrieve this ?)
                random_msg_id_1:
                    text:"Hi"
                    msg_type:"text"
                    time:"msgTime"

                random_msg_id_2:
                    text:"Hello"
                    msg_type:"text"
                    time:"msgTime"

        friend_user_id:(How to retrieve this ?)
            my_user_id:
                random_msg_id_1:
                    text:"Hi"
                    msg_type:"text"
                    time:"msgTime"

                random_msg_id_2:
                    text:"Hello"
                    msg_type:"text"
                    time:"msgTime"

Not sure about the Json output but as a guess, it should be something like following codes:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
       myRef.child("Messages").child("my_user_id").addValueEventListener(new ValueEventListener() {
                     @Override
                     public void onDataChange(DataSnapshot dataSnapshot) {

                        for(DataSnapshot item_snapshot:dataSnapshot.getChildren()) {

                        // get the data here and loop through the friend_user_id to get the nodes like following
                        Log.d("User id ", item_snapshot.toString());

                    }
                  }

            @Override
             public void onCancelled(DatabaseError databaseError) {
                     // handle error
              }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM