You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS平台MAUI Plugin.Firebase获取FCM Token时出现空引用异常求助

iOS MAUI 使用 Plugin.Firebase 获取 FCM Token 空引用异常排查

抛出的异常

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

我的FCM Token代码:

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

可能遗漏的iOS配置项

  • Firebase初始化验证
    确保在MauiProgram.cs中正确初始化Cloud Messaging模块:

    builder.AddFirebaseCloudMessaging();
    

    同时确认GoogleService-Info.plist已添加到iOS项目,且生成操作设置为BundleResource,文件内的GCM_SENDER_ID、PROJECT_ID与FCM控制台完全一致。

  • AppDelegate补充FCM回调实现
    目前仅添加了DynamicLinks的处理代码,还需补充FCM相关的通知回调:

    using Plugin.Firebase.CloudMessaging;
    
    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        FirebaseCloudMessagingImplementation.RegisteredForRemoteNotifications(application, deviceToken);
    }
    
    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        FirebaseCloudMessagingImplementation.FailedToRegisterForRemoteNotifications(application, error);
    }
    
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        FirebaseCloudMessagingImplementation.DidReceiveRemoteNotification(application, userInfo, completionHandler);
    }
    

    另外需要在FinishedLaunching中主动请求通知权限:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => {
            if (granted)
            {
                UIApplication.SharedApplication.RegisterForRemoteNotifications();
            }
        });
        return base.FinishedLaunching(app, options);
    }
    
  • 版本兼容性检查
    确认Plugin.Firebase使用最新稳定版,同时MAUI SDK版本与插件版本兼容,旧版本插件存在iOS平台初始化不完整导致空引用的问题。

是否为已知问题

该空引用异常是Plugin.Firebase在iOS平台上的常见问题,多数情况与Firebase Cloud Messaging实例未正确初始化、缺少必要的系统通知回调、或GoogleService-Info.plist配置错误有关,补充上述配置后通常可解决。

内容的提问来源于stack exchange,提问作者Matthew Pans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 08:43:34