TobiasBuchholz / Plugin.Firebase

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

Exception - Couldn't retrieve FCM token #335

Open wynston-champion-actabl opened 3 weeks ago

wynston-champion-actabl commented 3 weeks ago

I am receiving the error message "Couldn't retrieve FCM token" while awaiting await CrossFirebaseCloudMessaging.Current.GetTokenAsync();.

Current nuget: Plugin.Firebase.CloudMessaging - 3.0.0

My MauiProgram.cs file is as follows:

public static MauiApp CreateMauiApp()
{
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
                         .ConfigureServices()
                         .ConfigureViews()
                         .ConfigureViewModels()
                     .UseMauiCommunityToolkit()
                         .RegisterFirebaseServices()           
                        .ConfigureEssentials(essentials =>
                        {
                              essentials.UseVersionTracking();
                        })

                 return builder.Build();
}

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

        builder.Services.AddSingleton(_ => CrossFirebaseCloudMessaging.Current);

        return builder;
}

I have the following in my project file:

 <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
    <BundleResource Include="GoogleService-Info.plist" />
  </ItemGroup>

Confirmed that the Application ID matches what is listed as the Bundle ID in the GoogleService-Info.plist file.

Am I missing anything else that could be causing this error?

AndrzejKozik commented 3 weeks ago

Try adding line: FirebaseCloudMessagingImplementation.Initialize(); after CrossFirebase.Initialize(); in IOS section.

luisrr1590 commented 2 weeks ago

I have a similar issue.....without this line FirebaseCloudMessagingImplementation.Initialize() the app runs but the GetTokenAsync() throws NULL reference exception.

however, If I add the line then the app crashes with NULL reference exception in FirebaseCloudMessagingImplementation.RegisterForRemoteNotifications()

don't know what to do.