Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE FirebaseApp name [DEFAULT] already exists

I got a problem related to Firebase and Java EE.

I'm currently writing some Java servlets for my project and I'm using Firebase the first time because I wanted to try something new.

My actual problem is the following: I got a servlet which is responsible for exchanging an iOS device token in an user database. This is necessary for sending Remote Push Notifications to a device. I've done this like in the google tutorials, but I'm getting the following exception:

java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!

The way I'm accessing the Firebase Database is through the Java SDK.

I do this with the following code:

connect method

    // gets called by the servlet to configure Firebase
    public static void connect() {
      try {
        // for authentication purpose
        Map<String, Object> auth = new HashMap<>();
        auth.put("uid", "my-service-account");

        // Setting up the FirebaseOptions object
        // constant FIREBASE_DATABASE_URL = "url to my database"
        // constant FIREBASE_KEY_PATH = "path to my json key"
        options = new FirebaseOptions.Builder()
                  .setDatabaseUrl(FIREBASE_DATABASE_URL)
                  .setServiceAccount(new FileInputStream(FIREBASE_KEY_PATH))
                  .setDatabaseAuthVariableOverride(auth)
                  .build();

        FirebaseApp.initializeApp(options);

        // calling the method for exchanging the token
        exchangeIosDeviceToken("[email protected]", "5bf53173c9ef0a37638f3ddaa59cf2c0687c14ca0dcd47ccf57f9f09bd6368ab");
      } catch (FileNotFoundException ex) {
        ex.printStackTrace();
      }
    }

exchangeIosDeviceToken method

    public static boolean exchangeIosDeviceToken(String email, String newDeviceToken) {
      FirebaseDatabase database = FirebaseDatabase.getInstance();

      // getting a reference to my "employee" child
      DatabaseReference employeeReference = database.getReference("/employee");

      Map<String, Object> employeeUpdates = new HashMap<>();
      // updating the device token with child "iosDeviceToken" of "employee"
      employeeUpdates.put(email+"/iosDeviceToken", newDeviceToken);

      // update the actual children
      employeeReference.updateChildren(employeeUpdates);
      return true;
    }

The funny part is when I'm trying to execute this code in a standalone main class (replacing the connect method, with the main method), the code is working.

Before you're saying things like "there are tons of questions related to this topic"... They are nearly all related to Android and questions related to my problem seldom got answered.

Regards

like image 624
schmidi000 Avatar asked Jun 13 '16 10:06

schmidi000


3 Answers

this for the future users, You can check whether the default app is initialized or not like this.

    FirebaseApp firebaseApp = null;
    List<FirebaseApp> firebaseApps = FirebaseApp.getApps();
    if(firebaseApps!=null && !firebaseApps.isEmpty()){
        for(FirebaseApp app : firebaseApps){
            if(app.getName().equals(FirebaseApp.DEFAULT_APP_NAME))
                firebaseApp = app;
        }
    }
    else
        firebaseApp = FirebaseApp.initializeApp(options);   
like image 144
Kanishk Gupta Avatar answered Oct 16 '22 20:10

Kanishk Gupta


This exception appear because you are trying to create the [DEFAULT] FirebaseApp again, simply you can add a validation to check if it exist or not before the initialization, like this:

if(FirebaseApp.getInstance(FirebaseApp.DEFAULT_APP_NAME) != null) {
    return;
}
like image 24
Ebraheem Alrabeea Avatar answered Oct 16 '22 21:10

Ebraheem Alrabeea


Solved the problem.

The problem was: I've called the connect method everytime a request was incoming.

Solution: Call the connect method only once. (ServletContextListener)

like image 11
schmidi000 Avatar answered Oct 16 '22 19:10

schmidi000