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

.NET 8 MAUI Windows推送通知后台任务报错修复求助

.NET 8 MAUI Windows 后台推送通知问题修复方案

一、解决System.ArgumentException错误

直接注册后台任务时未指定正确的任务入口点且缺少清单配置,需按以下步骤调整:

1. 重构后台任务类

添加COM可见属性与唯一GUID,确保系统能识别该任务类:

using Windows.ApplicationModel.Background;
using System.Runtime.InteropServices;

namespace YourAppName.Platforms.Windows
{
    [ComVisible(true)]
    [Guid("生成唯一GUID,如:12345678-1234-1234-1234-1234567890AB")]
    public sealed class PushBackgroundTask : IBackgroundTask
    {
        private const string TASK_NAME = "notif_push_task";

        public static void Register()
        {
            // 避免重复注册任务
            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                if (task.Value.Name == TASK_NAME) return;
            }

            var builder = new BackgroundTaskBuilder();
            builder.TaskEntryPoint = typeof(PushBackgroundTask).FullName;
            builder.Name = TASK_NAME;
            builder.SetTrigger(new PushNotificationTrigger());
            // 可选:添加网络可用条件,确保推送能正常接收
            builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
            builder.Register();
        }

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            // 获取延迟对象,防止任务提前终止
            var deferral = taskInstance.GetDeferral();
            try
            {
                // 在这里编写推送通知处理逻辑,例:解析推送内容并显示Toast
                // var triggerDetails = taskInstance.TriggerDetails as PushNotificationTriggerDetails;
                // 处理triggerDetails中的推送数据
            }
            finally
            {
                deferral.Complete();
            }
        }
    }
}

二、修复应用清单验证错误(DEP0700)

错误提示需配置ActivatableClassId,需手动编辑Platforms/Windows/Package.appxmanifest:

1. 添加进程内可激活类配置

在<Package>节点下的<Extensions>中添加:

<Extensions>
  <uap:Extension Category="windows.activatableClass.inProcessServer">
    <uap:InProcessServer>
      <uap:ActivatableClass ActivatableClassId="YourAppName.Platforms.Windows.PushBackgroundTask" ThreadingModel="both" />
    </uap:InProcessServer>
  </uap:Extension>
</Extensions>

2. 添加后台任务声明

在<Application>节点内的<Extensions>中添加:

<Extensions>
  <uap:Extension Category="windows.backgroundTasks" EntryPoint="YourAppName.Platforms.Windows.PushBackgroundTask">
    <uap:BackgroundTasks>
      <uap:Task Type="pushNotification" />
    </uap:BackgroundTasks>
  </uap:Extension>
</Extensions>

三、配置必要的权限(Capabilities)

在<Capabilities>节点下添加以下权限:

<Capabilities>
  <Capability Name="internetClient" />
  <DeviceCapability Name="internetClientServer" />
  <!-- 如需监听用户通知,添加此项 -->
  <uap:Capability Name="userNotificationListener" />
</Capabilities>

四、正确注册后台任务

在OnLaunched中先申请后台权限,再注册任务:

protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
    base.OnLaunched(args);

    // 请求后台任务运行权限
    var accessStatus = await BackgroundExecutionManager.RequestAccessAsync();
    if (accessStatus is BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity or BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
    {
        PushBackgroundTask.Register();
    }
}

额外注意事项

  • GUID需唯一,可通过VS工具生成(工具→创建GUID,选择注册表格式并去除大括号)。
  • Run方法必须获取Deferral,否则任务会被系统强制终止。
  • 与Azure Notification Hub集成时,需确保已在Azure Portal配置Windows平台的WNS凭据(包SID与密钥)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 06:05:02