アプリ通知コレクション

コレクションを使用して、Notification Center でアプリの通知を整理します。 コレクションは、ユーザーが情報をより簡単に見つけ、開発者が通知をより適切に管理できるようにするのに役立ちます。

たとえば、メッセージング アプリでは、チャット グループごとに通知を分離できます。 各グループ タイトル ("Comp Sci 160A Project Chat"、"Direct Messages"、"Lacrosse Team Chat") は個別のコレクションです。 通知は、すべて同じアプリから送信された場合でも、別のアプリの場合と同じようにグループ化されます。 通知を整理するより微妙な方法については、「 アプリの通知ヘッダー」を参照してください。

2 つの異なる通知グループを含むコレクションの例

この記事のコード例では、Microsoft.Windows.AppNotifications 名前空間を使用して、コレクション管理用の通知コンテンツと Windows.UI.Notifications 名前空間を構築します。 これら 2 つの名前空間は、同じアプリで一緒に使用できます。

アプリ通知の詳細については、「アプリ通知 の概要」を参照してください。

コレクションの作成

コレクションを作成するときは、表示名とアイコンを指定します。アイコンは、コレクションのタイトルの一部として Notification Center に表示されます。 ユーザーがコレクション タイトルをクリックしたときにアプリが適切な場所に移動できるように、コレクションには起動引数も必要です。 SaveToastCollectionAsync を呼び出してコレクションを作成します。

using Windows.UI.Notifications;

var collection = new ToastCollection(
    "MyToastCollection",
    "Work Email",
    "NavigateToWorkEmailInbox",
    new Uri("ms-appx:///Assets/workEmail.png"));

await ToastNotificationManager.GetDefault()
    .GetToastCollectionManager()
    .SaveToastCollectionAsync(collection);

コレクションに通知を送信する

AppNotificationBuilder を使用して通知コンテンツを構築し、GetToastNotifierForToastCollectionIdAsync を呼び出して、コレクションにスコープ指定された notifier を取得します。

using Microsoft.Windows.AppNotifications.Builder;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

// Build notification content with Windows App SDK
var payload = new AppNotificationBuilder()
    .AddText("Adam sent a message to the group")
    .BuildNotification()
    .Payload;

// Deliver to a collection using the WinRT API
var doc = new XmlDocument();
doc.LoadXml(payload);
var toast = new ToastNotification(doc);

var notifier = await ToastNotificationManager.GetDefault()
    .GetToastNotifierForToastCollectionIdAsync("MyToastCollection");
notifier.Show(toast);

すべてのコレクションを一覧表示する

FindAllToastCollectionsAsync を呼び出して、アプリ用に作成されたすべてのコレクションを取得します。

var collectionManager = ToastNotificationManager.GetDefault().GetToastCollectionManager();
var collections = await collectionManager.FindAllToastCollectionsAsync();

コレクションを更新する

同じ ID を持つ新しい ToastCollection インスタンスを作成し、 SaveToastCollectionAsync を呼び出して、コレクションを更新します。

var collectionManager = ToastNotificationManager.GetDefault().GetToastCollectionManager();

var updatedCollection = new ToastCollection(
    "MyToastCollection",
    "Updated Display Name",
    "UpdatedLaunchArgs",
    new Uri("ms-appx:///Assets/updatedPicture.png"));

await collectionManager.SaveToastCollectionAsync(updatedCollection);

コレクションを削除する

コレクション ID を指定して RemoveToastCollectionAsync を 呼び出してコレクションを削除します。 コレクション内のすべての通知も通知センターから削除されます。

var collectionManager = ToastNotificationManager.GetDefault().GetToastCollectionManager();
await collectionManager.RemoveToastCollectionAsync("MyToastCollection");

コレクション内の通知を削除する

Tag プロパティと Group プロパティを使用して、Remove を呼び出してコレクション内の個々の通知を識別および削除するか、Clear ですべての通知を一度にクリアします。

var collectionHistory = await ToastNotificationManager.GetDefault()
    .GetHistoryForToastCollectionAsync("MyToastCollection");

// Remove a specific notification
collectionHistory.Remove(tag, group);

// Or clear all notifications in the collection
collectionHistory.Clear();

こちらも参照ください