Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
After you send an app notification, you may need to remove it from Notification Center when it's no longer relevant, or control how long it persists. The AppNotificationManager class provides methods for removing notifications, and the AppNotification class provides properties for controlling when notifications expire automatically.
For more information about app notifications, see App notifications overview.
Remove notifications from Notification Center
To remove specific notifications, first assign Tag and Group values when you show them. A Tag identifies a specific notification, and a Group identifies a set of related notifications. For example, a messaging app could use the chat thread ID as the Group and the contact name as the Tag.
AppNotificationManager provides several methods for removing notifications from Notification Center:
| Method | Description |
|---|---|
| RemoveByTagAsync | Removes all notifications with the specified tag. |
| RemoveByGroupAsync | Removes all notifications in the specified group. |
| RemoveByTagAndGroupAsync | Removes all notifications with the specified tag and group. |
| RemoveByIdAsync | Removes the notification with the specified ID. |
| RemoveAllAsync | Removes all notifications for the app. |
The following example shows how to tag notifications when sending them, then remove them later. In this scenario, a messaging app removes all notifications from a group chat after the user reads the conversation, and then removes all notifications from a specific contact after the user mutes them.
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();
}
Set an expiration time
Set an expiration time on your notification by using the Expiration property if the content is only relevant for a certain period of time. For example, a calendar app sending an event reminder should set the expiration to the end of the event.
Note
The default and maximum expiration time for a notification is 3 days.
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);
Expire notifications on reboot
Set the ExpiresOnReboot property to true if you want a notification to be removed from Notification Center when the computer restarts. This is useful for time-sensitive notifications that are no longer meaningful after a reboot, such as an in-progress call or a temporary reminder.
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);
See also
Windows developer