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

Xamarin iOS 15 Firebase后台推送仅在打开应用时接收问题求助

问题根因汇总
  • APNS设备token注册回调方法的Export特性配置错误,导致FCM无法获取到正确的APNS token,后续推送链路异常
  • UNUserNotificationCenter.Current.Delegate被重复赋值为自定义的MyUNUserNotificationCenterDelegate实例,你在AppDelegate中实现的IUNUserNotificationCenterDelegate相关方法(包括接收通知回调)不会被触发
  • Messaging.SharedInstance.Delegate重复赋值,存在逻辑冲突
  • 即使设置了content-available=1,iOS系统会根据应用的后台刷新权限、系统资源调度情况决定是否触发后台接收回调,低电量模式下会强制延迟回调
修复步骤

1 修正APNS token注册方法的签名和Export

你当前的DidRegisterForRemoteNotificationsWithDeviceToken方法的Export值错误,把UIKit的回调方法和Messaging的回调方法搞混了,替换成正确的实现:

// 移除原来错误的Export,替换成下面的
[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public override void DidRegisterForRemoteNotificationsWithDeviceToken(UIApplication application, NSData deviceToken)
{
    Messaging.SharedInstance.ApnsToken = deviceToken;
}

同时添加注册失败的回调方法,排查token获取失败的问题:

[Export("application:didFailToRegisterForRemoteNotificationsWithError:")]
public override void DidFailToRegisterForRemoteNotificationsWithError(UIApplication application, NSError error)
{
    Console.WriteLine($"注册APNS失败:{error.LocalizedDescription}");
}

2 移除重复的Delegate赋值逻辑

修改FinishedLaunching中的代码,删除重复的Delegate赋值:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    Firebase.Core.App.Configure();
    // 只赋值一次Messaging Delegate
    Messaging.SharedInstance.Delegate = this;
    
    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    {
        UNUserNotificationCenter.Current.RequestAuthorization(
            UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
            (granted, error) =>
            {
                Console.WriteLine($"通知权限授权结果:{granted}");
            });
        // 二选一:要么用AppDelegate作为Delegate,要么用自定义的MyUNUserNotificationCenterDelegate
        // 如果你要保留AppDelegate中的WillPresentNotification实现,就赋值为this
        UNUserNotificationCenter.Current.Delegate = this;
    }
    else
    {
        var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
        var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
        UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
    }
    
    Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
    UIApplication.SharedApplication.RegisterForRemoteNotifications();
    return true;
}

注:如果你选择保留自定义的MyUNUserNotificationCenterDelegate,就把AppDelegate中实现的WillPresentNotification等IUNUserNotificationCenterDelegate方法移到MyUNUserNotificationCenterDelegate类中即可

3 验证通知Payload格式

确认你发送的FCM通知Payload符合iOS要求,示例正确格式如下:

{
  "aps": {
    "alert": {
      "title": "测试标题",
      "body": "测试内容"
    },
    "sound": "default",
    "content-available": 1,
    "mutable-content": 1
  },
  "自定义字段": "自定义值"
}

4 检查设备权限

  • 打开设备的「设置」-「你的应用」-「通知」,确认允许通知权限已经开启
  • 确认「后台App刷新」权限已经开启
  • 测试时不要开启低电量模式,低电量模式会禁用后台刷新
  • 修复完成后先卸载设备上的旧应用,重新安装测试,避免旧的权限缓存影响结果

5 特殊配置确认

根据你补充的开发注意事项,确认你已经在Visual Studio 2022的iOS项目属性中,正确将「自定义权限」配置指向你创建的Entitlement.developer.plist,而不是默认的Entitlement.plist

内容的提问来源于stack exchange,提问作者C Morell

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 11:45:02