First, complete the installation and setup to connect your app to Firebase. Then from anywhere in your class, you can write:
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
It will write Hello, Wold!
into message
node, like seen below:
"your-project-parent" : {
"message" : "Hello, World!"
}
Explanation
FirebaseDatabase database = FirebaseDatabase.getInstance();
Above code will assign FirebaseDatabase
instance into database
object for further use.
DatabaseReference myRef = database.getReference("message");
Above code will reference myRef
object into "message"
child of your project's parent (in this example, it is "your-project-parent"
). So it is "your-project-parent/message"
myRef.setValue("Hello, World!");
Above code will set "Hello, World!"
into path referenced by myRef