Home > Back-end >  Got The default FirebaseApp already exists
Got The default FirebaseApp already exists

Time:09-21

I make an C# API to push notification to Flutter app.

My API basically like code below :

public async Task < IActionResult > pushNotification () {
  FirebaseApp.Create(new AppOptions() {
    Credential = GoogleCredential.FromFile("file.json")
  });
  .....
} 

I past url "domain.com/api/controller/pushNotification" on my browser, the first time it's work. My flutter receive notification perfectly. But the second time I got :

System.ArgumentException: The default FirebaseApp already exists.
   at FirebaseAdmin.FirebaseApp.Create(AppOptions options, String name)
   at FirebaseAdmin.FirebaseApp.Create(AppOptions options)

Why ? And how can I fix that ?

CodePudding user response:

Firebase manages its own client internally, you can reference it by calling methods for Firebase which make a request to the defined or default "App" since you can technically have multiple Apps.

You most likely only want to initiate the Firebase App only if one does not already exist. I believe you can do this by checking the FirebaseApp.apps length - you should

CodePudding user response:

the first time it's work. My flutter receive notification perfectly. But the second time I got

Your error message is saying that you're trying to initialize an app that already exists.

To fix this, you need to first check if the app has been initialized. Before running your piece of code that initializes the Firebase app, add a simple check, for example:

public async Task < IActionResult > pushNotification () {
if(appDoesNotExistYet) {
  FirebaseApp.Create(new AppOptions() {
    Credential = GoogleCredential.FromFile("file.json")
  });
}

  .....
} 
  • Related