Search code examples
javafirebasefirebase-realtime-databasefirebase-adminintellij-plugin

Firebase Database values not saving


I am trying to use the Firebase Admin SDK to save values to a Firebase real-time database, but no matter what I do, none of the values are being saved. Unlike the tutorials, I am starting out with a completely empty database:

enter image description here

  • I am connected to the database (verified using .info/connected reference, and ref.addValueEventListener)
  • Security rules are allow both read and write
  • Using a service account, and the json file for it seems to be parsed and loaded correctly
  • If I create a root reference object, and print its value, it is correctly pointing to the database url

Firestore has a collection and document created, and if I switch to the real-time database from the Firestore view, the database also has this collection and document.

enter image description here

But if I press on the root link, the document and collection disappears. I am also constantly seeing "data" as null. Is data a document? A field? Is it just saying the database as a whole is empty? Sometimes it alternates between "data: null" and "coconut-xxx:null" which is odd.

I have also tried to reference /telemetry/data and write to it, but that didn't work. When I add a CompletionListener to setValue, the CompletionListener never seems to be called, so the connection is hanging or something?

My code:

String url = databaseUrl.getProtocol() + "://" + databaseUrl.getHost();

InputStream stream = getClass().getClassLoader().getResourceAsStream("firebaseCredentials.json");
GoogleCredentials credentials = GoogleCredentials.fromStream(stream);

FirebaseOptions options = new FirebaseOptions.Builder()
                                             .setCredentials(credentials)
                                             .setDatabaseUrl(url)
                                             .build();

FirebaseApp.initializeApp(options);

DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.setValueAsync("12345");

I have also tried to send a Map like they do in the tutorial, but that doesn't work either.

I guess the only other thing that might be an issue is that I am doing this inside of an IntelliJ Android Studio plugin. That seems highly unlikely as a problem though..

Any help is appreciated. Thanks!


Solution

  • I found the problem. I don't know how this happened, but there was a mismatch between the service account JSON file in my IntelliJ resources folder, and the service account being used for the coconut project...

    Anyway, the real-time database is working now!