skip to Main Content

I am using Plugin.Firebase (v2.0.0) and i am using the below firebase recommended code in Maui.App.CreateBuilder and getting errors in Lambda expressions:

CS1503 Errors are:

Argument 2: cannot convert from Android.OS.Bundle to Plugin.Firebase.Bundled.Shared.CrossFirebaseSettings

Argument 1: cannot convert from UIKit.UIApplication to Plugin.Firebase.Bundled.Shared.CrossFirebaseSettings

Argument 2: cannot convert from Foundation.NSDictionary to Firebase.Core.Options

Tried using casts, but that does work for these types. Any suggestions?


using Plugin.Firebase.Auth;

#if IOS
using Plugin.Firebase.Bundled.Platforms.iOS;
#elif ANDROID
using Plugin.Firebase.Bundled.Platforms.Android;
#endif

public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCompatibility()
            .UseMauiCommunityToolkit()
            .ConfigureSyncfusionCore()
            .RegisterFirebaseServices()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });
    
        return builder.Build();
    }

private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
    {
        builder.ConfigureLifecycleEvents(events => {
#if IOS
            events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
                CrossFirebase.Initialize(app, launchOptions);
                return false;
            }));
#else
            events.AddAndroid(android => android.OnCreate((activity,state) =>
                CrossFirebase.Initialize(activity,  state)));
#endif
        });

        builder.Services.AddSingleton(_ => CrossFirebaseAuth.Current);
        return builder;
    }
}




3

Answers


  1. First of all, you’ll need to make sure that you’re only targeting iOS and Android, because the Plugin doesn’t support MacCatalyst and Windows, so you’ll need to update the target frameworks in your project file or add conditional package includes:

    <ItemGroup Condition="'$(TargetFramework)' == 'net6.0-ios' OR '$(TargetFramework)' == 'net6.0-android'">
        <PackageReference Include="Plugin.Firebase" Version="1.2.0" />
    </ItemGroup>
    

    Then, you’ll need to make sure that you’ve also placed preprocessor directives around the using ... statements in the file where you also have the RegisterFirebaseServices() method:

    #if IOS
    using Plugin.Firebase.iOS;
    #elif ANDROID
    using Plugin.Firebase.Android;
    #endif
    

    Depending on the .NET version you’re on, you may need to change the net6.0-ios and net6.0-android targets to net7.0-ios and net7.0-android (or higher, for future readers).

    That’s all more or less described in the documentation, as well.

    Login or Signup to reply.
  2. The setup instructions in the documentation had a mistake, the CrossFirebase.Initialize method doesn’t require any arguments on iOS and only an Activity on android, so your code should actually look like this:

    #if IOS
                events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
                    CrossFirebase.Initialize();
                    return false;
                }));
    #else
                events.AddAndroid(android => android.OnCreate((activity, state) =>
                    CrossFirebase.Initialize(activity)));
    #endif
            });
    

    The mistake in the readme got fixed accordingly.

    Login or Signup to reply.
  3. From the sample code MauiProgram.cs, we can find that the code of function RegisterFirebaseServices has used CreateCrossFirebaseSettings

       private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
        {
            builder.ConfigureLifecycleEvents(events => {
    #if IOS
                events.AddiOS(iOS => iOS.FinishedLaunching((app, launchOptions) => {
                    CrossFirebase.Initialize(CreateCrossFirebaseSettings());
                    FirebaseAuthFacebookImplementation.Initialize(app, launchOptions, "151743924915235", "Plugin Firebase Playground");
                    return false;
                }));
    #else
                events.AddAndroid(android => android.OnCreate((activity, _) =>
                    CrossFirebase.Initialize(activity, CreateCrossFirebaseSettings())));
    #endif
            });
    
             //other code
    
            return builder;
        }
    

    And the code of CrossFirebaseSettings just as follows:

    private static CrossFirebaseSettings CreateCrossFirebaseSettings()
        {
            return new CrossFirebaseSettings(
                isAnalyticsEnabled: true,
                isAuthEnabled: true,
                isCloudMessagingEnabled: true,
                isDynamicLinksEnabled: true,
                isFirestoreEnabled: true,
                isFunctionsEnabled: true,
                isRemoteConfigEnabled: true,
                isStorageEnabled: true,
                googleRequestIdToken: "537235599720-723cgj10dtm47b4ilvuodtp206g0q0fg.apps.googleusercontent.com");
        }
    

    For more information, you can check the sample code here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search