Reputation: 445
I have created a Firebase, and I can write data into Authentication. But, I could not write data into realtime database.
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.4.1')
// Declare the dependency for the Realtime Database library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-database:20.0.2'
{
"rules": {
".read": true,
".write": true
}
}
I tried this to write data into Firebase but I get noting
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
// Write a message to the database
FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");
FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");
Upvotes: 1
Views: 1334
Reputation: 600110
The code for writing looks fine, so I think the problem may be elsewhere.
If you created the Firebase console after you downloaded the google-services.json
file, that file will not contain the correct URL, and the SDK may not be able to find your database on the server.
To solve that problem, you can either:
google-services.json
and use that in your app, orFirebaseDatabase database = FirebaseDatabase.getInstance("database URL here");
Also see: Google Firebase Real-time Database Not Working as everything is set correctly
Upvotes: 2
Reputation: 445
My code works this morning, but I did nothing except go to bed. As shown in the picture, the second step has changed. I clicked that Add the Realtime Database SDK to your app
button thousands of times, and it was still a button. Surprisingly, it has changed to a checked state.
I did the google-services.json
part as @FrankvanPuffelen said yesterday, but it didn't work.
I have restarted the Android Studio a lot of times and didn't work.
Maybe I should try close the computer lid next time.
But now I know that one should have the second step checked rather than keep it as a unchecked button.
Upvotes: 1