Rimuovere le notifiche delle app

Dopo aver inviato una notifica dell'app, potrebbe essere necessario rimuoverla dal Centro notifiche quando non è più rilevante o controllare per quanto tempo viene mantenuta. La classe AppNotificationManager fornisce metodi per la rimozione delle notifiche e la classe AppNotification fornisce proprietà per il controllo della scadenza automatica delle notifiche.

Per altre informazioni sulle notifiche delle app, vedere Panoramica delle notifiche delle app.

Rimuovere le notifiche dal Centro notifiche

Per rimuovere notifiche specifiche, assegnare prima i valori tag e gruppo quando vengono visualizzati. Un tag identifica una notifica specifica e un gruppo identifica un set di notifiche correlate. Ad esempio, un'app di messaggistica potrebbe usare l'ID del thread di chat come gruppo e il nome del contatto come tag.

AppNotificationManager offre diversi metodi per rimuovere le notifiche dal Centro notifiche:

metodo Descrizione
RemoveByTagAsync Rimuove tutte le notifiche con il tag specificato.
RemoveByGroupAsync Rimuove tutte le notifiche nel gruppo specificato.
RemoveByTagAndGroupAsync Rimuove tutte le notifiche con il tag e il gruppo specificati.
RemoveByIdAsync Rimuove la notifica con l'ID specificato.
RemoveAllAsync Rimuove tutte le notifiche per l'app.

Nell'esempio seguente viene illustrato come contrassegnare le notifiche durante l'invio, quindi rimuoverle in un secondo momento. In questo scenario, un'app di messaggistica rimuove tutte le notifiche da una chat di gruppo dopo che l'utente legge la conversazione e quindi rimuove tutte le notifiche da un contatto specifico dopo che l'utente li disattiva.

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();
}

Impostare un'ora di scadenza

Impostare un'ora di scadenza per la notifica usando la proprietà Expiration se il contenuto è rilevante solo per un determinato periodo di tempo. Ad esempio, un'app del calendario che invia un promemoria dell'evento deve impostare la scadenza alla fine dell'evento.

Annotazioni

La scadenza predefinita e massima per una notifica è di 3 giorni.

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);

Scadenza delle notifiche al riavvio

Impostare la proprietà ExpiresOnReboot su true se si desidera rimuovere una notifica dal Centro notifiche al riavvio del computer. Ciò è utile per le notifiche sensibili al tempo che non sono più significative dopo un riavvio, ad esempio una chiamata in corso o un promemoria temporaneo.

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);

Vedere anche