Example of Firebase.push() in Java.

UPDATED: 22 September 2016
Google firebase push


Add to a list of data in the database. Every time you push a new node onto a list, your database generates a unique key, like items/<unique-item-id>/<data>

Push is basically used when you have collection of data like items, users, notifications and you want to generate unique id for each record in collection.

In previous example we've seen How to save data in Firebase?, It saves data as you provided within given path where push will generate unique id as root node of your data.

Source code (Item.java)
public class Item {

    private Long id;
    private String name;
    private Double price;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id = id;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public Double getPrice() {
        return price;

    public void setPrice(Double price) {
        this.price = price;

Source code (FirebasePushObject.java)
package com.javaquery.google.firebase;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.javaquery.bean.Item;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.concurrent.CountDownLatch;

 * Example of Firebase push.
 * @author javaQuery
 * @date 22nd September, 2016
 * @Github: https://github.com/javaquery/Examples
public class FirebasePushObject {
    public static void main(String[] args) {
        Item item = new Item();
        // You can use List<Item> also.
        new FirebasePushObject().saveUsingPush(item);
    private FirebaseDatabase firebaseDatabase;

     * initialize firebase.
    private void initFirebase() {
        try {
            // .setDatabaseUrl("https://fir-66f50.firebaseio.com") - Firebase project url.
            // .setServiceAccount(new FileInputStream(new File("filepath"))) - Firebase private key file path.
            FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
                    .setServiceAccount(new FileInputStream(new File("C:\\Users\\Vicky\\Documents\\NetBeansProjects\\Examples\\src\\com\\javaquery\\google\\firebase\\Firebase-30f95674f4d5.json")))

            firebaseDatabase = FirebaseDatabase.getInstance();
        } catch (FileNotFoundException ex) {

     * Save item object in Firebase.
     * @param item 
    private void saveUsingPush(Item item) {
        if (item != null) {
            /* Get database root reference */
            DatabaseReference databaseReference = firebaseDatabase.getReference("/");
            /* Get existing child or will be created new child. */
            DatabaseReference childReference = databaseReference.child("items");

             * The Firebase Java client uses daemon threads, meaning it will not prevent a process from exiting.
             * So we'll wait(countDownLatch.await()) until firebase saves record. Then decrement `countDownLatch` value
             * using `countDownLatch.countDown()` and application will continues its execution.
            CountDownLatch countDownLatch = new CountDownLatch(1);
             * push()
             * Add to a list of data in the database. Every time you push a new node onto a list, 
             * your database generates a unique key, like items/unique-item-id/data
            childReference.push().setValue(item, new DatabaseReference.CompletionListener() {

                public void onComplete(DatabaseError de, DatabaseReference dr) {
                    System.out.println("Record saved!");
                    // decrement countDownLatch value and application will be continues its execution.
            try {
                //wait for firebase to saves record.
            } catch (InterruptedException ex) {

Record saved!
Check Database in Firebase console.