首页 > 解决方案 > 当应用程序在后台时,UWP 会阻止一些通知

问题描述

我想屏蔽一些通知。当应用程序处于前台时,我可以使用以下代码执行此操作:

var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += Channel_PushNotificationReceived;

然后:

private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
    if (args?.NotificationType == PushNotificationType.Toast)
    {
        if (string.IsNullOrWhiteSpace(args.ToastNotification?.Content?.InnerText))
        {
            args.Cancel = true;
        }
    }
}

但是我不知道当应用程序关闭时该怎么做。我尝试了通知侦听器,但只有在通知已经出现时才能访问它们。

我的问题是,当应用程序在后台时,如何阻止一些通知?

标签: uwppush-notification

解决方案


您可以使用原始推送通知(请参阅文档)。这些是没有关联 UI 的特殊类型的通知,但您可以创建一个后台任务,该任务在此类通知到达时触发。在此任务中,您可以决定是否要显示通知,并在这种情况下直接在代码中创建和显示通知。


推荐阅读