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

.NET MAUI+Shiny iOS推送通知无法接收,Android正常求排查方案

问题描述

开发面向iOS 13+和Android Target 27+的.NET MAUI 8应用,采用Shiny.Push实现推送功能,Android端运行完全正常,但iOS端无法接收通知。已完成以下配置:

  • Apple端:创建应用并配置Bundle ID、开启Push Notifications权限、生成沙盒/生产环境推送证书并转换为.p12文件用于推送API
  • 应用配置:Entitlements.plist添加aps-environment=development,Info.plist配置remote-notification后台模式
  • 启动配置:注册Shiny服务及DefaultPushDelegate,iOS端代码已通过断点确认在设备上执行
  • AppDelegate实现IUNUserNotificationCenterDelegate,可成功获取Device Token,但无其他推送相关事件触发
  • Push Delegate为基础实现,iOS端未收到任何回调
  • 推送API为成熟稳定版本,旧Java Android应用、新.NET MAUI Android应用、旧Swift iOS应用均可正常接收推送,仅MAUI iOS端失败,已尝试切换开发/生产环境证书
排查建议

1. 简化Shiny iOS初始化逻辑

iOS端无需手动注册PushManager,Shiny内部会自动处理该服务。可简化iOS端服务注册代码:

#if IOS
builder.Services.AddPush();
#endif

同时确认AppDelegate中未手动设置UNUserNotificationCenter.Current.Delegate——Shiny会自动接管该委托,手动覆盖会导致Shiny无法接收推送回调。

2. 确认通知权限已正确授予

iOS 10+需明确请求通知权限,即使能获取Device Token,用户未授权通知权限也会导致推送无法展示。可在应用启动时主动触发权限请求:

var push = Shiny.ShinyHost.Resolve<Shiny.Push.IPushManager>();
var status = await push.RequestAccess();
if (status != Shiny.Push.AccessStatus.Granted)
{
    // 处理权限未授予场景
}

3. 验证推送Payload格式与环境匹配

iOS对APNs推送Payload有严格规范,需确保:

  • 开发环境推送地址为gateway.sandbox.push.apple.com:2195,生产环境为gateway.push.apple.com:2195,与证书环境一致
  • Payload必须包含aps字典,非静默推送需包含alert、sound等字段,示例格式:
{
  "aps": {
    "alert": "测试通知内容",
    "sound": "default",
    "badge": 1
  },
  "customData": "自定义业务数据"
}

4. 确认Entitlements.plist关联正确

在MAUI iOS项目中,需确保Entitlements.plist已正确关联到项目配置:

  • 右键iOS项目 → 属性 → iOS Bundle Signing → 确认Entitlements文件已选中,且Debug/Release配置对应正确的aps-environment值(Debug用development,Release用production)

5. 检查APNs反馈信息

查看推送API的返回结果,或使用Apple官方工具排查APNs响应错误:

  • InvalidToken:Device Token无效,可能是证书环境与推送环境不匹配(如用生产证书推送沙盒环境)
  • BadDeviceToken:Device Token格式错误,需确保无多余空格或非法字符

6. 确认AppDelegate生命周期方法未阻断Shiny逻辑

如果AppDelegate重写了DidReceiveRemoteNotification,必须调用基类方法,否则Shiny无法处理推送:

public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
    base.DidReceiveRemoteNotification(application, userInfo, completionHandler);
    // 自定义逻辑
}

7. 用本地通知验证基础通知能力

先测试本地通知,确认应用能正常展示通知,排除系统设置或权限层面的问题:

var center = UNUserNotificationCenter.Current;
var content = new UNMutableNotificationContent();
content.Title = "本地测试通知";
content.Body = "验证应用是否能接收并展示通知";
content.Sound = UNNotificationSound.Default;
var request = UNNotificationRequest.FromIdentifier("test-local", content, UNTimeIntervalNotificationTrigger.CreateTrigger(5, false));
await center.AddNotificationRequestAsync(request);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 00:13:17