Trying to use Firebase Realtime Database. I have :
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mMessagesDatabaseReference;
In onCreate
of main activity I’m trying to write record:
mFirebaseDatabase = FirebaseDatabase.getInstance();
mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("settings");
mMessagesDatabaseReference.push().setValue("aaaa").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TAG", "Data written successfully");
} else {
Log.d("TAG", "Failed with: " + task.getException().getMessage());
}
}
}).addOnFailureListener(e -> {
Log.d("TAG", "Failed with: " + e.getMessage()); });
But still have NULL in the Firebase console in the DB browser.
build.gradle
has lines:
implementation(platform("com.google.firebase:firebase-bom:32.7.2"))
implementation("com.google.firebase:firebase-database:20.3.0")
What did I do wrong?
UPD
Changed code by adding onComplete
and OnFailure
listeners.
None of events are called.
Just to have this out of the way and that is probably not the problem: Do you have the correct database rules, that you are actually allowed to write data?
setValue
can fail. You’re not checking the result for errors.To detect the failures Doug talks about, you can add a completion listener as shown here: firebase.google.com/docs/database/android/…
The the completion listener is not called, the client likely can’t reach the server. The most common cause of that these days is that you downloaded the
google-services.json
file before you created the database, so the file doesn’t contain its URL. If that is the case, you can either re-download the file and rebuild the app with it, or you can specify the URL in your code:mFirebaseDatabase = FirebaseDatabase.getInstance("your full database URL here");
. For more on this, see stackoverflow.com/a/68179677