Default FirebaseApp is not initialized in this process,Make sure to call FirebaseApp.initializeApp(Context) first

Aayush Rawat 0 Reputation points
2025-04-08T05:58:34.1733333+00:00

I recently implemented firebase push notification into my maui app and facing a strange issue,

sometime the application works fine and sometime it trows exception,saying:

Default FirebaseApp is not initialized in this process,Make sure to call FirebaseApp.initializeApp(Context) first

I guess it is something related to firebase initialization, below is my code:

public static class MauiProgram
  {
      public static MauiApp CreateMauiApp()
      {
          var builder = MauiApp.CreateBuilder();
          builder
              .UseMauiApp<App>()
              .RegisterFirebaseServices()
              .UseMauiCommunityToolkit()

   private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
        {
            builder.ConfigureLifecycleEvents(events => {
            #if IOS
                    events.AddiOS(iOS => iOS.WillFinishLaunching((_, __) => {
                        CrossFirebase.Initialize();
                        FirebaseCloudMessagingImplementation.Initialize();
                        return false;
                    }));
#if ANDROID
                        events.AddAndroid(android => android.OnCreate((activity, _) =>
                        CrossFirebase.Initialize(activity)));
#endif
                            });

            return builder;
        }


I recently implemented firebase push notification into my maui app and facing a strange issue,

sometime the application works fine and sometime it trows exception,saying:

Default FirebaseApp is not initialized in this process,Make sure to call FirebaseApp.initializeApp(Context) first

I guess it is something related to firebase initialization, below is my code:

public

and on button handler, where exception is being thrown while obtaining fcm token:

if (CrossFirebaseCloudMessaging.Current != null)
  {
      await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
      
          var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();
          if (!string.IsNullOrEmpty(token))
          {
              Preferences.Set("deviceId", token);
          }
          else
          {
              return;
          }

I have followed these tutorial:

https://maxmannstein.com/index.php/2025/01/19/implementing-firebase-cloud-messaging-in-net-maui-ios/

and

https://maxmannstein.com/index.php/2025/01/19/implementing-firebase-cloud-messaging-in-net-maui-android/

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jeff Coggin 0 Reputation points
    2025-04-08T06:09:26.7166667+00:00

    It sounds like you're encountering an issue with Firebase initialization in your .NET MAUI app. The error message "Default FirebaseApp is not initialized in this process. Make sure to call FirebaseApp.initializeApp(Context) first" indicates that the FirebaseApp instance is not being properly initialized before you attempt to use Firebase services.

    Possible Fixes

    1. Ensure Proper Initialization: Make sure that you are calling FirebaseApp.initializeApp(Context) in the OnCreate method of your MainActivity. This is crucial for initializing Firebase before any Firebase services are used.
      
         public class MainActivity : MauiAppCompatActivity
      
         {
      
             protected override void OnCreate(Bundle savedInstanceState).          more.        It looks like you're encountering an issue with Firebase initialization in your .NET MAUI app. The error message "Default FirebaseApp is not initialized in this process. Make sure to call FirebaseApp.initializeApp(Context) first" suggests that the FirebaseApp instance is not being properly initialized before you attempt to use Firebase services.
      
      

    Possible Causes and Fixes

    1. Ensure Proper Initialization: Make sure that you are calling FirebaseApp.initializeApp(Context) in the OnCreate method of your MainActivity. This is crucial for initializing Firebase before any Firebase services are used.
      
         public class MainActivity : MauiAppCompatActivity
      
         {
      
             protected override void OnCreate(Bundle savedInstanceState)
      
             {
      
                 base.OnCreate(savedInstanceState);
      
                 Firebase.FirebaseApp.InitializeApp(this);
      
                 // Other initialization code
      
             }
      
         }
      
      
    2. Check google-services.json: Ensure that the google-services.json file is correctly placed in the Resources directory of your Android project and that its build action is set to GoogleServicesJson.
    3. Match Package Names: Verify that the package name in your AndroidManifest.xml matches the package name specified in the google-services.json file.
    4. Clean and Rebuild: Sometimes, cleaning and rebuilding the project can resolve initialization issues. Make sure to clean your solution and rebuild it.
    5. Check Dependencies: Ensure that you have the correct versions of Firebase dependencies in your project. Sometimes, downgrading or upgrading the Google services plugin can resolve the issue.
      
         dependencies {
      
             classpath 'com.android.tools.build:gradle:3.3.0'
      
             classpath 'com.google.gms:google-services:4.0.0' // or the version that works for you
      
         }
      
      

    Insights from the Web

    From the web search results, here are some common solutions that have worked for others:

    • Explicit Initialization: Explicitly call FirebaseApp.initializeApp(Context) in the OnCreate method of your MainActivity12.
    • Build Action: Ensure the google-services.json file's build action is set to GoogleServicesJson2.
    • Version Compatibility: Sometimes, downgrading or upgrading the Google services plugin version can resolve the issue2.

    Additional Information

    In some cases, the issue might be related to the timing of the initialization. Ensure that the CheckIfValidAsync and GetTokenAsync methods are called after the FirebaseApp has been initialized. If the issue persists, you might want to check for any specific platform-related configurations or updates that might be required.

    If you have any specific configurations or additional details about your setup, feel free to share them, and I can provide more tailored assistance.

       {
    
           base.OnCreate(savedInstanceState);
    
           Firebase.FirebaseApp.InitializeApp(this);
    
           // Other initialization code
    
       }
    

    }

    
    2. **Check google-services.json**:
    
       Ensure that the `google-services.json` file is correctly placed in the `Resources` directory of your Android project and that its build action is set to `GoogleServicesJson`.
    
    3. **Match Package Names**:
    
       Verify that the package name in your `AndroidManifest.xml` matches the package name specified in the `google-services.json` file.
    
    4. **Clean and Rebuild**:
    
       Sometimes, cleaning and rebuilding the project can resolve initialization issues. Make sure to clean your solution and rebuild it.
    
    5. **Check Dependencies**:
    
       Ensure that you have the correct versions of Firebase dependencies in your project. Sometimes, downgrading or upgrading the Google services plugin can resolve the issue.
    
       ```gradle
    
       dependencies {
    
           classpath 'com.android.tools.build:gradle:3.3.0'
    
           classpath 'com.google.gms:google-services:4.0.0' // or the version that works for you
    
       }
    
    

    Additional Insights

    From the web search results, here are some common solutions that have worked for others:

    • Explicit Initialization: Explicitly call FirebaseApp.initializeApp(Context) in the OnCreate method of your MainActivity12.
    • Build Action: Ensure the google-services.json file's build action is set to GoogleServicesJson2.
    • Version Compatibility: Sometimes, downgrading or upgrading the Google services plugin version can resolve the issue2.

    If you have any specific configurations or additional details about your setup, feel free to share them, and I can provide more tailored assistance. Copilot gave this. Based on the provided code and the error message you're encountering, it seems that the FirebaseApp is not being properly initialized before you attempt to use Firebase services. Let's simulate the issue and find a fix.

    Provided Code

    
    if (CrossFirebaseCloudMessaging.Current != null)
    
    {
    
        await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
    
        
    
        var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();
    
        if (!string.IsNullOrEmpty(token))
    
        {
    
            Preferences.Set("deviceId", token);
    
        }
    
        else
    
        {
    
            return;
    
        }
    
    }
    
    

    Simulating the Issue

    The error "Default FirebaseApp is not initialized in this process. Make sure to call FirebaseApp.initializeApp(Context) first" typically occurs when the FirebaseApp instance is not initialized before calling Firebase services. This can happen if the initialization code is not placed correctly or if there are issues with the configuration files.

    Possible Fixes

    1. Ensure Proper Initialization in MainActivity: Make sure that you are initializing Firebase in the OnCreate method of your MainActivity.
      
         public class MainActivity : MauiAppCompatActivity
      
         {
      
             protected override void OnCreate(Bundle savedInstanceState)
      
             {
      
                 base.OnCreate(savedInstanceState);
      
                 Firebase.FirebaseApp.InitializeApp(this);
      
                 // Other initialization code
      
             }
      
         }
      
      
    2. Check google-services.json: Ensure that the google-services.json file is correctly placed in the Resources directory of your Android project and that its build action is set to GoogleServicesJson.
      
         <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'">
      
             <GoogleServicesJson Include="Resources\google-services.json" />
      
         </ItemGroup>
      
      
    3. Match Package Names: Verify that the package name in your AndroidManifest.xml matches the package name specified in the google-services.json file.
      
         <manifest package="com.yourcompany.yourapp">
      
             <!-- Other configurations -->
      
         </manifest>
      
      
    4. Check Dependencies: Ensure that you have the correct versions of Firebase dependencies in your project. Sometimes, downgrading or upgrading the Google services plugin can resolve the issue.
      
         dependencies {
      
             classpath 'com.android.tools.build:gradle:3.3.0'
      
             classpath 'com.google.gms:google-services:4.0.0' // or the version that works for you
      
         }
      
      
    5. Initialization Timing: Ensure that the CheckIfValidAsync and GetTokenAsync methods are called after the FirebaseApp has been initialized. You can add a check to ensure FirebaseApp is initialized before calling these methods.
      
         if (CrossFirebaseCloudMessaging.Current != null)
      
         {
      
             if (Firebase.FirebaseApp.Instance == null)
      
             {
      
                 Firebase.FirebaseApp.InitializeApp(Android.App.Application.Context);
      
             }
      
             await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
      
             
      
             var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();
      
             if (!string.IsNullOrEmpty(token))
      
             {
      
                 Preferences.Set("deviceId", token);
      
             }
      
             else
      
             {
      
                 return;
      
             }
      
         }
      
      

    Additional Insights from the Web

    From the web search results, here are some common solutions that have worked for others:

    • Explicit Initialization: Explicitly call FirebaseApp.initializeApp(Context) in the OnCreate method of your MainActivity12.
    • Build Action: Ensure the google-services.json file's build action is set to GoogleServicesJson2.
    • Version Compatibility: Sometimes, downgrading or upgrading the Google services plugin version can resolve the issue2.

    By following these steps, you should be able to resolve the initialization issue and ensure that Firebase services are properly initialized before being used in your .NET MAUI app. If you have any specific configurations or additional details about your setup, feel free to share them, and I can provide more tailored assistance. Based on the provided code and the error message you're encountering, it seems that the FirebaseApp is not being properly initialized before you attempt to use Firebase services. Let's simulate the issue and find a fix.

    Provided Code

    
    if (CrossFirebaseCloudMessaging.Current != null)
    
    {
    
        await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
    
        
    
        var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();
    
        if (!string.IsNullOrEmpty(token))
    
        {
    
            Preferences.Set("deviceId", token);
    
        }
    
        else
    
        {
    
            return;
    
        }
    
    }
    
    

    Simulating the Issue

    The error "Default FirebaseApp is not initialized in this process. Make sure to call FirebaseApp.initializeApp(Context) first" typically occurs when the FirebaseApp instance is not initialized before calling Firebase services. This can happen if the initialization code is not placed correctly or if there are issues with the configuration files.

    Possible Fixes

    1. Ensure Proper Initialization in MainActivity: Make sure that you are initializing Firebase in the OnCreate method of your MainActivity.
      
         public class MainActivity : MauiAppCompatActivity
      
         {
      
             protected override void OnCreate(Bundle savedInstanceState)
      
             {
      
                 base.OnCreate(savedInstanceState);
      
                 Firebase.FirebaseApp.InitializeApp(this);
      
                 // Other initialization code
      
             }
      
         }
      
      
    2. Check google-services.json: Ensure that the google-services.json file is correctly placed in the Resources directory of your Android project and that its build action is set to GoogleServicesJson.
      
         <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'">
      
             <GoogleServicesJson Include="Resources\google-services.json" />
      
         </ItemGroup>
      
      
    3. Match Package Names: Verify that the package name in your AndroidManifest.xml matches the package name specified in the google-services.json file.
      
         <manifest package="com.yourcompany.yourapp">
      
             <!-- Other configurations -->
      
         </manifest>
      
      
    4. Check Dependencies: Ensure that you have the correct versions of Firebase dependencies in your project. Sometimes, downgrading or upgrading the Google services plugin can resolve the issue.
      
         dependencies {
      
             classpath 'com.android.tools.build:gradle:3.3.0'
      
             classpath 'com.google.gms:google-services:4.0.0' // or the version that works for you
      
         }
      
      
    5. Initialization Timing: Ensure that the CheckIfValidAsync and GetTokenAsync methods are called after the FirebaseApp has been initialized. You can add a check to ensure FirebaseApp is initialized before calling these methods.
      
         if (CrossFirebaseCloudMessaging.Current != null)
      
         {
      
             if (Firebase.FirebaseApp.Instance == null)
      
             {
      
                 Firebase.FirebaseApp.InitializeApp(Android.App.Application.Context);
      
             }
      
             await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
      
             
      
             var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();
      
             if (!string.IsNullOrEmpty(token))
      
             {
      
                 Preferences.Set("deviceId", token);
      
             }
      
             else
      
             {
      
                 return;
      
             }
      
         }
      
      

    Additional Insights from the Web

    From the web search results, here are some common solutions that have worked for others:

    • Explicit Initialization: Explicitly call FirebaseApp.initializeApp(Context) in the OnCreate method of your MainActivity12.
    • Build Action: Ensure the google-services.json file's build action is set to GoogleServicesJson2.
    • Version Compatibility: Sometimes, downgrading or upgrading the Google services plugin version can resolve the issue2.

    By following these steps, you should be able to resolve the initialization issue and ensure that Firebase services are properly initialized before being used in your .NET MAUI app. If you have any specific configurations or additional details about your setup, feel free to share them, and I can provide more tailored assistance.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.