您好,我有以下用 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?
}
});
谢谢你的帮助
最佳答案
Firebase 的 databaseRef.push()
再次返回带有随机键的 DatabaseReference
对象。所以有可能得到这样的 key :
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!
}
});
关于javascript - 推送时获取 Firebase UID,然后在 Java 中使用给定的 UID 再次推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56979523/