TobiasBuchholz / Plugin.Firebase

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

System.NullReferenceException: at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.GetTokenAsync() #229

Closed sreejith-pagematics closed 10 months ago

sreejith-pagematics commented 10 months ago

I am getting below exception on the iOS platform for creating FCM Token.

System.NullReferenceException: Object reference not set to an instance of an object. at Plugin.Firebase.CloudMessaging.FirebaseCloudMessagingImplementation.GetTokenAsync()

My FCM Token Code:

await CrossFirebaseCloudMessaging.Current.CheckIfValidAsync();
var token = await CrossFirebaseCloudMessaging.Current.GetTokenAsync();

I did below set up on iOS platform:

  1. On the apple developer website and choose the Certificates, Identifiers & Profiles. Opened Identifiers and choose the app’s identifier. Then added Push Notification service into the identifier
  2. Created a new provisioning profile, installed it on the device and binded it to the project settings.
  3. Opened the apple developer website and choosed the Certificates, Identifiers & Profiles. Opened Keys from that and created a new key.(.P8 file) Downloaded it and uploaded it into the FCM console.
  4. Enabled Remote Notifications on Info.plist and Entitlements.plist.
  5. Added the below codes on AppDelegate.cs file.

    using Plugin.Firebase.DynamicLinks;

    public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler) { FirebaseDynamicLinksImplementation.ContinueUserActivity(application, userActivity, completionHandler); return base.ContinueUserActivity(application, userActivity, completionHandler); }

    public override bool OpenUrl(UIApplication application, NSUrl url, NSDictionary options) { FirebaseDynamicLinksImplementation.OpenUrl(application, url, options); return base.OpenUrl(application, url, options); }

Is any other set up is pending on iOS platform? Is it a known issue?

sreejith-pagematics commented 10 months ago

Fixed this issue by replacing FinishedLaunching with WillFinishLaunching on MauiProgram.cs.

        private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
        {
            builder.ConfigureLifecycleEvents(events => {
#if IOS
            events.AddiOS(iOS => iOS.WillFinishLaunching((app, launchOptions) => {
                CrossFirebase.Initialize(CreateCrossFirebaseSettings());
                CrossFirebaseCrashlytics.Current.SetCrashlyticsCollectionEnabled(true);
                return false;
            }));
#else
            events.AddAndroid(android => android.OnCreate((activity, _) =>
            CrossFirebase.Initialize(activity, CreateCrossFirebaseSettings())));
            CrossFirebaseCrashlytics.Current.SetCrashlyticsCollectionEnabled(true);
#endif
            });

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