Get Firebase UID when push, then push again with the given UID in Java

H3lltronik :

Hi I have the following code written in Javascript and I need to do the same in Java, there's the code:

firebase.database().ref('notificacionesAdmin/').push(notificacion).then(res => {
                    firebase.database().ref('notificacionesAdmin/' + res.key).child('idNotificacion').set(res.key)           

It's a simple push but it waits untill the first push is done, then it push again inside the last pushed child and inside of it write the UID of the node
Something like this:

uniqueid :{
    idNotificacion: uniqueid
    ....
}


I tried doing something like this but it doesn't return any UID or I just missing something

database = FirebaseDatabase.getInstance().getReference("notificacionesAdmin/");
database.push().setValue(notificacion).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        // How to get the pushed child uid?
    }
});

Thanks for your help

Nurbol :

Firebase's databaseRef.push() returns again DatabaseReference object with a random key. So it is possible to get key like this:

database = FirebaseDatabase.getInstance().getReference("notificacionesAdmin/");
final String key = database.push().getKey();
database.child(key).setValue(notificacion).addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        // Here use the key!
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155249&siteId=1