Xamarin.Forms iOS端Firebase推送收不到通知,OnNotificationReceived未触发
iOS推送通知收不到问题排查方案
一、配置细节再核对
- 确认
GoogleService-Info.plist的Build Action是BundleResource,文件不要嵌套在子文件夹里,避免读取失败 - 检查
Entitlements.plist:除了开启推送通知,还要确认APS Environment配置和测试环境匹配——开发环境选development,生产环境选production;如果用到App Groups也要确保配置正确 Info.plist里设了FirebaseAppDelegateProxyEnabled为false后,必须手动处理APNs回调,不能依赖Firebase自动代理- 后台模式除了勾选「远程通知」,如果需要静默推送,还要勾选「后台获取」
二、AppDelegate代码逻辑检查
禁用Firebase代理后,必须手动实现APNs回调并转发给插件,关键代码要覆盖这些场景:
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { // 初始化Firebase和推送插件 Firebase.Core.App.Configure(); CrossFirebasePushNotification.Current.Initialize(); // 请求推送权限 UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => { if (granted) { UIApplication.SharedApplication.RegisterForRemoteNotifications(); } }); // 其他初始化代码... return base.FinishedLaunching(app, options); } // 注册APNs成功后转发Token给插件 public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { CrossFirebasePushNotification.Current.RegisterDeviceToken(deviceToken); } // iOS 9及以下后台/前台接收通知 public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) { CrossFirebasePushNotification.Current.OnNotificationReceived(userInfo); completionHandler(UIBackgroundFetchResult.NewData); } // iOS 10+ 前台显示通知回调 [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")] public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) { CrossFirebasePushNotification.Current.OnNotificationReceived(notification.Request.Content.UserInfo); // 前台要显示通知必须设置这个选项 completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound); } // iOS 10+ 用户点击通知回调 [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")] public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) { CrossFirebasePushNotification.Current.OnNotificationReceived(response.Notification.Request.Content.UserInfo); completionHandler(); }
- 检查是否漏写了上述任意回调方法,尤其是iOS 10+的两个
UNUserNotificationCenter相关方法
三、推送测试方式验证
- 推送消息格式必须符合APNs+Firebase要求,示例JSON(可通过Firebase API发送):
{ "to": "你的iOS设备Token", "notification": { "title": "测试标题", "body": "测试内容" }, "data": { "custom_key": "custom_value" }, "apns": { "payload": { "aps": { "alert": { "title": "测试标题", "body": "测试内容" }, "sound": "default" } } } }
- 区分前台/后台场景:APP在前台时,必须在
WillPresentNotification中设置显示选项,否则通知不会弹窗;后台推送如果需要静默处理,要在aps里加"content-available": 1 - 确认Firebase控制台的iOS应用配置中,上传的APNs证书和测试环境匹配——开发环境用开发证书,生产环境用生产证书
四、日志深度排查
- 在AppDelegate的所有推送相关回调里加日志输出,比如:
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) { Console.WriteLine($"APNs Token获取成功: {deviceToken}"); CrossFirebasePushNotification.Current.RegisterDeviceToken(deviceToken); } public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) { Console.WriteLine($"收到APNs原始消息: {userInfo}"); CrossFirebasePushNotification.Current.OnNotificationReceived(userInfo); completionHandler(UIBackgroundFetchResult.NewData); }
- 打开Xcode的设备日志(Window > Devices and Simulators > 选中设备 > View Device Logs),搜索
APNs、Firebase关键词,找隐藏的错误(比如证书不匹配、消息格式错误) - 查看Firebase控制台「Cloud Messaging」>「Reports」,看推送的送达状态,如果显示「已发送但未送达」,大概率是APNs证书问题
五、版本兼容性检查
- 确认Plugin.FirebasePushNotification 3.4.35和Xamarin.Forms 5.0.0.2515的兼容性,尝试更新插件到最新稳定版,或者回退到已知兼容的版本
- 检查Firebase相关NuGet包(如
Xamarin.Firebase.iOS.CloudMessaging、Xamarin.Firebase.iOS.Core)的版本,必须和插件版本匹配,避免版本冲突
内容的提问来源于stack exchange,提问作者raito tec
相关产品推荐
相关产品推荐

