Get the pushed ID for specific value in firebase android

firebase get value of child android
firebase update specific field android
firebase get data by key android
get push id firebase javascript
updatechildren firebase android
firebase get all child keys android
write to firebase database android
firebase push

I want to retrive the id that generated by firebase when I pushed value to it like next

I want to retrieve "-KGdKiPSODz7JXzlgl9J" this id for that email I tried by getKey() but it return "users" and when user get value it return the whole object from the id to profile picture and that won't make me get it as User object in my app

how solve this ?

    Firebase users = myFirebaseRef.child("users");
    users.orderByChild("email").equalTo("z@m.com").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            dataSnapshot.getKey();
            Log.d("User",dataSnapshot.getRef().toString());
            Log.d("User",dataSnapshot.getValue().toString());
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

            Log.d("User",firebaseError.getMessage() );
        }
    });

You can read the key from push() without pushing the values. Later you can create a child with that key and push the values for that key.

// read the index key
String mGroupId = mGroupRef.push().getKey();
....
....
// create a child with index value
mGroupRef.child(mGroupId).setValue(new ChatGroup());

mGroupId contains the key which is used to index the value you're about to save.

Saving Data, For basic write operations, you can use setValue() to save data to a specified reference, replacing any Get Post object and use the values to update the UI Get the pushed ID for specific value in firebase android Hot Network Questions Does Iran's sanction play a pivotal role in rejecting papers from a journal?

UPDATE 1: it can obtain also by one line

String key = mDatabase.child("posts").push().getKey();

//**************************************************************//

after searching and trying a lot of things i came to 2 ways to do that . 1. first one to get the key when i upload the post to the server via this function

 public void uploadPostToFirebase(Post post) {
      DatabaseReference mFirebase = mFirebaseObject
            .getReference(Constants.ACTIVE_POSTS_KEY)
            .child(post.type);
      mFirebase.push().setValue(post);
      Log.d("Post Key" , mFirebase.getKey());
 }
  1. i used it in my code to get the key after i have already pushed it to node for it in my database

    public void getUserKey(String email) {
    
        Query queryRef = databaseRef.child(Constants.USERS_KEY)
            .orderByChild(Constants.USERS_EMAIL)
            .equalTo(email);
    
        queryRef.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated
          }
    
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated;
          }
    
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
            //TODO auto generated;
          }
    
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            //TODO auto generated
          }
    
          @Override
          public void onCancelled(DatabaseError databaseError) {
            //TODO auto generated 
          }
      });
     }
    

Read and Write Data on Android, The database structure: For each dbRef of different sports, I have a child node “​contacts”. made for the children of “contacts”, in order to parse the data in a specific format. Can we use Firebase for a one to one chat application in Android? This include pushing specific data that contains info that require the front end system to display in the right section of the app. Let’s start by creating a new flutter project. Setting up FCM

When you fire a Firebase query there can potentially be multiple results. So when you ask for the value of a query, Firebase returns a list of items. Even if there is only one matching item, it will be a list of one item.

So you will have to handle this list in your code:

users.orderByChild("email").equalTo("z@m.com").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot child: dataSnapshot.getChildren()) {
            Log.d("User key", child.getKey());
            Log.d("User ref", child.getRef().toString());
            Log.d("User val", child.getValue().toString());
        }
    }

How to read all child key values of a child from Firebase database in , Every Firebase project has its own particular devoted Realtime Database items After writing the data into Firebase now it's time to read what we have written. the DatabaseReference class we can set the object value to Firebase, as follows: //Setting firebase unique key for Hashmap list String userId = mDbRef.push(). Every client connected to a Firebase database maintains its own internal version of any active data. When data is written, it's written to this local version first. The Firebase client then synchronizes that data with the remote database servers and with other clients on a "best-effort" basis. As a result,

Reading and writing to Realtime Database, As with all realtime database implementations, an app must first obtain a The push() method of the database reference allows child nodes to be key which may then be used to store a corresponding value. Using Firebase to generate unique keys in this way is also of particular use  This video shows how to get any specific data from firebase database to a TextView in Sketchware project.

I solved it to get id you need to use firebase url

String uid = firebaseRef.child("users").push().getKey();
Log.i("uid", uid);

this will give you the key KG.....

Working with Firebase Realtime Database Lists, The Firebase JavaScript SDK uses promises for async operations… ref.update​(obj); // Updates only the specified attributes You'd have two users trying to push to /somePath/501 at the same time — a colliding keys disaster. The value event fires once for an initial data load and again whenever any data changes. For more on this type of read operation, see the Firebase documentation on reading values. If you instead listen on /ZONES/ZONE_1, you will get a DataSnapshot of the entire node with all its properties. You then use DataSnapshot.child() to get the ZNAME from it:

Save and Query Firebase Data, And, while we'll continue to save restaurants under a unique push ID, we'll also a method of identifying the specific restaurant to remove from a user's node. Since Firebase creates this value, we cannot assign it to the object until later on, Now that we have added user authentication to our app, we need to update how  Reading Data using a ValueEventListener - Android Firebase In this video I show you how to read data from a Firebase Database. There are many ways you DON’T want to get the data.

Connecting Users and Object Data, Ah I see, yeah I'm calling it with a value which returns a promise on native, but returns the reference object on web. See the following screenshots: The code: Here, dbRef is the database reference to the parent node. The database structure: For each dbRef of different sports, I have a child node “contacts”.

Get Ref Key after push? · Issue #147 · invertase/react-native-firebase, How do you get specific firebase data without knowing the unique id? I have some data in my firebase database that I want to retrieve. I did one big push so that  Per "Firebase's: Save Data on Android": vous pouvez utiliser la référence aux nouvelles données retournées par la méthode push() pour obtenir la valeur de la clé générée automatiquement par l'enfant ou des données de jeu pour l'enfant. Appeler getKey() sur une référence push renvoie la valeur de la clé générée automatiquement.

Comments
  • Would you consider iterating over the users dataSnapshot and using an identifier value (like the email for example) to see if you have a match and retrieve the values of that matching snapshot?
  • yes to get the id that match this value
  • For next time: You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do.
  • ok thanks for your comment i will do that next time
  • This helped, even though I am using JavaScript.
  • Yes i know that but that doesn't answer my question for every child there exist a unique key for it i want to extract this Key if i use child.getKey() it return me "users"
  • Can you set up a jsbin/jsfiddle that reproduces this behavior?
  • what do you mean by that ?
  • @MiDo_FCIcu : Yes. it returns "users". Have u found anything ?
  • @NandanKaushikDutta here is my answer to the question after i found how to solve it [link] (stackoverflow.com/a/39492322/3553843)
  • what happens if the object I want to save is not composed of string-only keys and/or values?
  • firebase.google.com/docs/database/android/read-and-write demonstrates what kind of data that firebase can handle... it's all JSON, and includes String, Long, Double, Boolean, Map<String, Object>, List<Object>... the above example corresponds to Map<String, Object>
  • Worked for me! But only if you use FirebaseUI library
  • A little bit of explanatory text would improve this answer.