Reputation: 851
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:
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
Created a new provisioning profile, installed it on the device and binded it to the project settings.
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.
Enabled Remote Notifications on Info.plist and Entitlements.plist.
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?
Upvotes: 0
Views: 604
Reputation: 14519
You can refer to this issue:Need testing with iOS simulator.
It has the similar issue as yours. And according to the comment in it, this is because the GetTokenAsync()
runs before the plugin's initialization.
So you can add the following code in the MauiProgram.cs:
events.AddiOS(iOS => iOS.WillFinishLaunching((app, launchOptions) => { CrossFirebase.Initialize(app, launchOptions, CreateCrossFirebaseSettings()); return false; }));
And then call the code about getting token in the AppShell's OnHandlerChanged method.
Upvotes: 2