앱 알림을 보낸 후 알림 센터에서 앱 알림이 더 이상 관련이 없는 경우 제거하거나 지속 기간을 제어해야 할 수 있습니다. AppNotificationManager 클래스는 알림을 제거하는 메서드를 제공하고 AppNotification 클래스는 알림이 자동으로 만료되는 시기를 제어하기 위한 속성을 제공합니다.
앱 알림에 대한 자세한 내용은 앱 알림 개요를 참조하세요.
알림 센터에서 알림 제거
특정 알림을 제거하려면 먼저 태그 및그룹 값을 표시할 때 할당합니다. 태그는 특정 알림을 식별하고 그룹은 관련 알림 집합을 식별합니다. 예를 들어 메시징 앱은 채팅 스레드 ID를 그룹으로 사용하고 연락처 이름을 태그로 사용할 수 있습니다.
AppNotificationManager 는 알림 센터에서 알림을 제거하는 몇 가지 방법을 제공합니다.
| 메서드 | 설명 |
|---|---|
| RemoveByTagAsync | 지정된 태그를 사용하여 모든 알림을 제거합니다. |
| RemoveByGroupAsync | 지정된 그룹의 모든 알림을 제거합니다. |
| RemoveByTagAndGroupAsync | 지정된 태그 및 그룹을 사용하여 모든 알림을 제거합니다. |
| RemoveByIdAsync | 지정된 ID를 사용하여 알림을 제거합니다. |
| RemoveAllAsync | 앱에 대한 모든 알림을 제거합니다. |
다음 예제에서는 알림을 보낼 때 태그를 지정한 다음 나중에 제거하는 방법을 보여 줍니다. 이 시나리오에서 메시징 앱은 사용자가 대화를 읽은 후 그룹 채팅에서 모든 알림을 제거한 다음 사용자가 음소거한 후 특정 연락처에서 모든 알림을 제거합니다.
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
void SendNotification(string message, string contactTag, string chatGroup)
{
var notification = new AppNotificationBuilder()
.AddText(message)
.BuildNotification();
// Tag and group the notification so it can be removed later
notification.Tag = contactTag;
notification.Group = chatGroup;
AppNotificationManager.Default.Show(notification);
}
// Remove all notifications from a specific group chat
async Task RemoveGroupChatNotifications(string chatGroup)
{
await AppNotificationManager.Default.RemoveByGroupAsync(chatGroup);
}
// Remove all notifications from a specific contact across all groups
async Task RemoveContactNotifications(string contactTag)
{
await AppNotificationManager.Default.RemoveByTagAsync(contactTag);
}
// Remove all notifications for the app
async Task RemoveAllNotifications()
{
await AppNotificationManager.Default.RemoveAllAsync();
}
만료 시간 설정
콘텐츠가 특정 기간 동안만 관련된 경우 Expiration 속성을 사용하여 알림에 만료 시간을 설정합니다. 예를 들어 이벤트 미리 알림을 보내는 일정 앱은 만료를 이벤트의 끝으로 설정해야 합니다.
메모
알림의 기본 및 최대 만료 시간은 3일입니다.
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
var notification = new AppNotificationBuilder()
.AddText("Team standup in 15 minutes")
.AddText("Conference Room B")
.BuildNotification();
// Remove the notification from Notification Center after one hour
notification.Expiration = DateTimeOffset.Now.AddHours(1);
AppNotificationManager.Default.Show(notification);
다시 부팅 시 알림 만료
컴퓨터가 다시 시작될 때 Notification Center에서 알림을 제거하려면 ExpiresOnReboot 속성을 true 설정합니다. 이는 진행 중인 호출 또는 임시 미리 알림처럼 재부팅 후 더 이상 의미가 없는 경우에 시간에 민감한 알림에 유용합니다.
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
var notification = new AppNotificationBuilder()
.AddText("You're sharing your screen")
.BuildNotification();
notification.ExpiresOnReboot = true;
AppNotificationManager.Default.Show(notification);
참고하십시오
Windows developer