嗨,我有以下用Javascript编写的代码,我需要在Java中做同样的事情,有代码:
firebase.database().ref('notificacionesAdmin/').push(notificacion).then(res => {
firebase.database().ref('notificacionesAdmin/' + res.key).child('idNotificacion').set(res.key)
这是一个简单的推送,但要等到第一次推送完成后,再在最后推送的子节点中再次推送,并在其中写入节点的UID
类似这样的东西:
uniqueid :{
idNotificacion: uniqueid
....
}
我尝试做类似的事情,但是它没有返回任何UID或我只是丢失了一些东西
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?
}
});
感谢您的帮助
答案 0 :(得分:1)
Firebase的databaseRef.push()
使用随机密钥再次返回DatabaseReference
对象。因此可以这样获得密钥:
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!
}
});