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

.NET MAUI iOS推送通知:WillPresentNotification等方法未触发求助

解决方案

一、完善MAUI项目核心配置(MauiProgram.cs)

在CreateMauiApp方法中补充通知服务注册与iOS权限初始化逻辑,确保MAUI框架正确对接APNs:

builder.Services.AddSingleton<INotificationService, NotificationService>();

// iOS 专属配置
builder.ConfigureLifecycleEvents(events =>
{
#if IOS
    events.AddiOS(ios => ios
        .FinishedLaunching((app, options) =>
        {
            // 请求通知权限并注册远程通知
            var center = UNUserNotificationCenter.Current;
            center.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Badge, (granted, error) =>
            {
                if (granted)
                {
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                }
            });
            return true;
        }));
#endif
});

二、补全AppDelegate的APNs关键回调

你已实现的通知展示/响应逻辑没问题,但需补充Token注册的回调方法,确认Token能正常接收并处理:

[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    // 格式化Token(去除尖括号与空格)
    var token = deviceToken.Description.Trim('<', '>').Replace(" ", "");
    SentrySdk.CaptureMessage($"APNs Token获取成功: {token}");
    // 将Token同步至你的自有APN服务器
}

[Export("application:didFailToRegisterForRemoteNotificationsWithError:")]
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
    SentrySdk.CaptureMessage($"Token注册失败: {error.LocalizedDescription}");
}

三、修复Azure DevOps Pipeline的证书与签名问题

1. 确保证书与配置文件匹配

  • 推送证书(APS证书)必须与Provisioning Profile绑定的App ID关联,且配置文件已在Apple Developer后台启用推送权限
  • 下载最新的配置文件上传至Azure DevOps,避免旧配置缺失权限

2. 调整DotNetCoreCLI构建参数

在构建任务中明确指定签名信息,确保推送证书被正确调用:

- task: DotNetCoreCLI@2
  displayName: '构建MAUI iOS应用'
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: '**/*.csproj'
    arguments: '-f net7.0-ios -c Release /p:ArchiveOnBuild=true /p:BuildIpa=true /p:CodesignKey="Apple Development: 你的名称 (XXXXXXX)" /p:CodesignProvision="你的配置文件名称" /p:TeamId="你的团队ID"'

3. 正确导入证书至构建环境

在构建任务前添加证书导入步骤,确保推送证书与开发证书都导入临时钥匙串并赋予访问权限:

- task: InstallAppleCertificate@2
  displayName: '导入APNs推送证书'
  inputs:
    certSecureFile: '你的推送证书文件.p12'
    certPwd: '$(PushCertificatePassword)'
    keychain: 'temp'
    keychainPassword: '$(KeychainPassword)'

- task: InstallAppleCertificate@2
  displayName: '导入开发证书'
  inputs:
    certSecureFile: '你的开发证书文件.p12'
    certPwd: '$(DevCertificatePassword)'
    keychain: 'temp'
    keychainPassword: '$(KeychainPassword)'

四、验证自有APN服务器配置

  1. 环境匹配:开发环境使用api.development.push.apple.com:443,生产环境使用api.push.apple.com:443,确保Token环境与服务器一致
  2. 负载格式:Apple Pass更新的静默推送负载需包含content-available字段:
{
  "aps": {
    "content-available": 1
  },
  "pass": {
    "passTypeIdentifier": "你的Pass类型ID",
    "serialNumber": "Pass序列号"
  }
}
  1. 证书有效性:用openssl验证推送证书是否可用:
openssl pkcs12 -in push-cert.p12 -out push-cert.pem -nodes
openssl s_client -connect api.development.push.apple.com:443 -cert push-cert.pem

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 07:35:19