我的应用程序具有一些功能,其中之一是警报,重新启动后,所有警报都会被取消。我正在尝试在重新启动时重置它们,但是从IntentService
类检索数据时出现问题,如下所示。
我的参考和数据库结构
rootRef = FirebaseDatabase.getInstance().getReference();
userRef = rootRef.child(getString(R.string.db_user));
userDataRef = userRef.child(firebaseUser.getUid());
alarmRef = userDataRef.child(getString(R.string.db_alarm));
// ...
reminderRef = userDataRef.child(getString(R.string.db_reminder));
我的
IntentService
课:class BootService extends IntentService {
public BootService() {
super("BootService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
FirebaseApp.initializeApp(this);
DatabaseReference alarmsRef = FirebaseDatabase.getInstance().getReference()
.child(getString(R.string.db_user))
.child()
// How can I retrieve userId here if user is not logged-in yet?
// Application may have many accounts in one phone
// I need to retrieve them all not only one
.child(getString(R.string.db_alarm));
}
}
第一个问题是注释中提到的
userId
,第二个问题是创建引用实际上会触发onChildAdded事件吗?任何实现此目的的更好方法都值得欢迎,在此先感谢
最佳答案
如果用户尚未登录,如何在此处检索userId?
你不能。如果该用户尚未登录,则意味着您不知道哪个用户将登录到您的应用程序以使用该特定ID。
创建引用实际上会触发onChildAdded事件吗?
不,仅创建引用并不意味着将触发onChildAdded()
方法。仅当您在该引用上附加侦听器时,才会触发此方法。
关于java - 使用Firebase Realtime数据库在重启时重置警报,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50205528/