コレクションを使用したトースト通知のグループ化

コレクションを使用してアクション センターでアプリのトーストを整理します。 コレクションにより、ユーザーはアクション センター内での情報をより簡単に見つけることができ、開発者が通知をより適切に管理できるようになります。 下の API では、通知のコレクションの削除、作成、および更新が可能です。

重要

Creators Update が必要: トースト コレクションを使用するには、SDK 15063 をターゲットとし、ビルド 15063 以降を実行している必要があります。 関連する API には、Windows.UI.Notifications.ToastCollection および Windows.UI.Notifications.ToastCollectionManager が含まれます。

以下の例には、チャット グループに基づいた通知を分離するメッセージング アプリがあります。それぞれのタイル (Comp Sci 160A Project Chat、Direct Messages、Lacrosse Team Chat) は別のコレクションです。 すべて同じアプリからの通知である場合でも、別のアプリからの通知であるかのように、通知が明確にグループ化されているのがわかります。 通知を整理するより巧妙な方法を探している場合は、「トースト ヘッダー」を参照してください。
2 つ異なる通知のグループを持つコレクションの例

コレクションの作成

各コレクションを作成するときは、表示名とアイコンを指定する必要があります。表示名とアイコンは上のイメージに示すように、コレクションのタイトルの一部としてアクション センターの内部に表示されます。 また、ユーザーがコレクションのタイトルをクリックしたときにアプリがアプリ内の適切な場所に移動できるように、コレクションには起動引数が必要です。

コレクションの作成

// Create a toast collection
public async void CreateToastCollection()
{
	string displayName = "Work Email"; 
	string launchArg = "NavigateToWorkEmailInbox"; 
	Uri icon = new Windows.Foundation.Uri("ms-appx:///Assets/workEmail.png");

	// Constructor
	ToastCollection workEmailToastCollection = new ToastCollection(
		"MyToastCollection", 
		displayName,
 		launchArg, 
		icon);

	// Calls the platform to create the collection
	await ToastNotificationManager.GetDefault().GetToastCollectionManager().SaveToastCollectionAsync(workEmailToastCollection);  								
}

コレクションへの通知の送信

3 つの異なるトースト パイプライン (ローカル、スケジュール、プッシュ) からの通知の送信について説明します。 このそれぞれの例について、サンプルのトーストを作成して直後のコードと共に送信し、各パイプラインを介してコレクションにトーストを追加する方法について説明します。

トースト コンテンツの作成:

// Construct the content
var content = new ToastContentBuilder()
	.AddText("Adam sent a message to the group")
	.GetToastContent();

コレクションへのトーストの送信

// Create the toast
ToastNotification toast = new ToastNotification(content.GetXml());

// Get the collection notifier
var notifier = await ToastNotificationManager.GetDefault().GetToastNotifierForToastCollectionIdAsync("MyToastCollection");

// And show the toast
notifier.Show(toast);

コレクションへのスケジュールされたトーストの追加

// Create scheduled toast from XML above
ScheduledToastNotification scheduledToast = new ScheduledToastNotification(content.GetXml(), DateTimeOffset.Now.AddSeconds(10));

// Get notifier
var notifier = await ToastNotificationManager.GetDefault().GetToastNotifierForToastCollectionIdAsync("MyToastCollection");
    
// Add to schedule
notifier.AddToSchedule(scheduledToast);

コレクションへのプッシュ トーストの送信

プッシュ トーストでは、X-WNS-CollectionId ヘッダーを POST メッセージに追加する必要があります。

// Add header to HTTP request
request.Headers.Add("X-WNS-CollectionId", collectionId); 

コレクションの管理

トースト コレクション マネージャーの作成

この「コレクションの管理」セクションの残りのコード スニペットでは、以下の collectionManager を使用します。

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

すべてのコレクションを取得する

IReadOnlyList<ToastCollection> collections = await collectionManager.FindAllToastCollectionsAsync();

作成されたコレクション数の取得

int toastCollectionCount = (await collectionManager.FindAllToastCollectionsAsync()).Count;

コレクションの削除

await collectionManager.RemoveToastCollectionAsync("MyToastCollection");

コレクションの更新

同じ ID で新しいコレクションを作成し、コレクションの新しいインスタンスを保存して、コレクションを更新することができます。

string displayName = "Updated Display Name"; 
string launchArg = "UpdatedLaunchArgs"; 
Uri icon = new Windows.Foundation.Uri("ms-appx:///Assets/updatedPicture.png");

// Construct a new toast collection with the same collection id
ToastCollection updatedToastCollection = new ToastCollection(
	"MyToastCollection", 
	displayName,
	launchArg, 
	icon);

// Calls the platform to update the collection by saving the new instance
await collectionManager.SaveToastCollectionAsync(updatedToastCollection);  								

コレクション内でのトーストの管理

グループとタグのプロパティ

グループとタグのプロパティを合わせてコレクション内で一意に通知を識別します。 グループ (およびタグ) は、復号主キー (1 つ以上の識別子) として機能し、通知を識別します。 たとえば、通知を削除するか置換する必要がある場合、削除または置換する通知を指定できる必要があります。これは、タグとグループを指定することによって行います。 例として、メッセージング アプリがあります。 開発者は、会話 ID をグループとして、メッセージ ID をタグとして使用できます。

コレクションからのトーストの削除

タグとグループの ID を使用して個々のトーストを削除したり、コレクション内のすべてのトーストをクリアできます。

// Get the history
var collectionHistory = await ToastNotificationManager.GetDefault().GetHistoryForToastCollectionAsync("MyToastCollection");

// Remove toast
collectionHistory.Remove(tag, group); 

コレクション内のすべてのトーストのクリア

// Get the history
var collectionHistory = await ToastNotificationManager.GetDefault().GetHistoryForToastCollectionAsync("MyToastCollection");

// Remove toast
collectionHistory.Clear();

Notifications Visualizer のコレクション

Notifications Visualizer ツールを使用するとコレクションの設計に役立ちます。 次の手順に従います。

  • 右下隅にある歯車アイコンをクリックします。
  • [Toast collections] (トースト コレクション) を選択します。
  • トーストのプレビューの上に、[トースト コレクション] ドロップダウン メニューがあります。 [manage collections] (コレクションの管理) を選択します。
  • [コレクションの追加] をクリックし、コレクションの詳細を入力し、保存します。
  • コレクションを追加するか、または [コレクションの管理] ボックスをオフにしてメイン画面に戻ることができます。
  • [トースト コレクション] ドロップダウン メニューからトーストを追加するコレクションを選択します。
  • トーストを生成するときに、アクション センターの適切なコレクションに追加されます。

その他の詳細

作成するトースト コレクションは、ユーザーの通知の設定にも反映されます。 ユーザーは、個々のコレクションの設定を切り替えて、これらのサブグループをオンまたはオフにすることができます。 アプリの最上位で通知がオフになると、すべてのコレクションの通知もオフになります。 さらに、各コレクションでは、既定でアクション センターに 3 つの通知が表示され、ユーザーはこれを展開して最大 20 の通知を表示することができます。