TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

Wrong events sequencing order when initializing the Plugin on iOS #166

Closed Kapusch closed 10 months ago

Kapusch commented 1 year ago

Hi @TobiasBuchholz !

I'm now migrating my app to MAUI, and I have noticed a different behavior between Android & iOS when initializing your Plugin. Currently, you're suggesting to register Firebase services in MauiProgram.cs using ConfigureLifecycleEvents():

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

However, from the documentation, iOS.FinishedLaunching() means the app has already launched, while I understand android.OnCreate() it means the activity is created, but startup is NOT completed.

I've been doing some tests and I think the equivalent of android.OnCreate() for iOS is iOS.WillFinishLaunching(). Indeed, this solved an issue in my app where "Firebase is not yet configured" while I'm fetching Remote settings from the App.xaml.cs OnStart() method.

One more clue is that, while working with the Xamarin.Forms version of your Plugin, we had to initialize the Firebase services from AppDelegate.cs on iOS, inside FinishedLaunching() method, NOT after.

Am I wrong doing this ?

angelru commented 1 year ago

I think the same thing happens to me on iOS on android it seemed to work fine..., the plugin "takes time to start". Sometimes I got an exception when using this code at startup:

CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
CrossFirebaseCloudMessaging.Current.SubscribeToTopicAsync("Topic");

Because firebase was not started. I had to put the above code in the OnAppearingof one of my views and problem solved. Are you experiencing the same problem?

TobiasBuchholz commented 1 year ago

Hey @Kapusch, I didn't have any problems when initializing the plugin the way I've described in the Readme, but I think you are completely right that initializing it in iOS.WillFinishLaunching() is more similar to the startup process on android and also to the plugin version before Maui 👍

If there is noone who has a case against putting the initialization call in iOS.WillFinishLaunching() I will update the Readme accordingly.

tranb3r commented 1 year ago

I'm also using WillFinishLaunching instead of FinishedLaunching.