Xamarin iOS中FirebasePushNotificationManager初始化致应用冻结求助
Xamarin.Forms iOS 初始化Firebase推送通知导致应用UI冻结问题
问题描述
使用Xamarin(C#)开发应用时,Android端在Application.cs中调用FirebasePushNotificationManager.Initialize(options, true);可正常接收通知,但iOS端在AppDelegate.cs的FinishedLaunching方法中添加该行代码后,应用出现UI冻结(按钮、输入框点击无响应),移除该行代码后iOS应用恢复正常。
用户提供的AppDelegate.cs代码如下:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { ServicePointManager .ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; global::Xamarin.Forms.Forms.Init(); var iosClientHandler = new NSUrlSessionHandler(); Services.HttpClientService.HttpClientHandler = iosClientHandler; LoadApplication(new App()); //This line is causing the issue FirebasePushNotificationManager.Initialize(options, true); return base.FinishedLaunching(app, options); } public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken); } public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error) { FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error); } // To receive notifications in foreground on iOS 9 and below // To receive notifications in background in any iOS version public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) { // If you are receiving a notification message while your app is in the background, // this callback will not be fired 'till the user taps on the notification launching the application. // If you disable method swizzling, you'll need to call this method. // This lets FCM track message delivery and analytics, which is performed // automatically with method swizzling enabled FirebasePushNotificationManager.DidReceiveMessage(userInfo); // Do your magic to handle the notification data System.Console.WriteLine(userInfo); completionHandler(UIBackgroundFetchResult.NewData); } }
解决方法
1. 调整初始化时机与顺序
尝试修改Firebase初始化代码的位置,避免在主线程加载应用后立即执行阻塞操作:
- 方案A:将初始化移至
LoadApplication之前
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { ServicePointManager .ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; global::Xamarin.Forms.Forms.Init(); var iosClientHandler = new NSUrlSessionHandler(); Services.HttpClientService.HttpClientHandler = iosClientHandler; // 先初始化Firebase,再加载应用 FirebasePushNotificationManager.Initialize(options, false); LoadApplication(new App()); return base.FinishedLaunching(app, options); }
- 方案B:将初始化移至
base.FinishedLaunching调用之后
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { ServicePointManager .ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; global::Xamarin.Forms.Forms.Init(); var iosClientHandler = new NSUrlSessionHandler(); Services.HttpClientService.HttpClientHandler = iosClientHandler; LoadApplication(new App()); var result = base.FinishedLaunching(app, options); // 在base方法执行完成后初始化 FirebasePushNotificationManager.Initialize(options, true); return result; }
2. 禁用延迟注册
将初始化的第二个参数enableDelayedRegistration设为false,避免延迟注册逻辑阻塞主线程:
FirebasePushNotificationManager.Initialize(options, false);
确保推送注册的回调方法(RegisteredForRemoteNotifications、FailedToRegisterForRemoteNotifications)已正确实现,保证注册流程正常执行。
3. 后台线程执行初始化
如果初始化操作存在同步阻塞,可将其放入后台线程执行,避免占用主线程:
LoadApplication(new App()); // 后台线程执行Firebase初始化 Task.Run(() => { FirebasePushNotificationManager.Initialize(options, true); }); return base.FinishedLaunching(app, options);
4. 检查版本兼容性
确认Xamarin.Forms版本与Plugin.FirebasePushNotification版本兼容,尝试更新到最新稳定版或回退至已知兼容版本,避免版本冲突导致主线程阻塞。
5. 验证iOS项目配置
- 确认
GoogleService-Info.plist已添加到iOS项目,且Build Action设置为BundleResource - 检查Info.plist中是否配置了推送权限:
<key>UIBackgroundModes</key> <array> <string>remote-notification</string> </array> <key>NSRemoteNotificationUsageDescription</key> <string>需要推送通知权限来接收消息</string>
内容的提问来源于stack exchange,提问作者Edward0524
相关产品推荐
相关产品推荐

