共用方式為


将快显通知归入集合

使用集合來整理應用程式的通知於動作中心。 集合可協助使用者更輕鬆地在控制中心內尋找資訊,並讓開發人員更妥善地管理其通知。 下列 API 允許移除、建立和更新通知集合。

Important

需要 Creators Update:您必須鎖定 SDK 15063 並執行 15063 或更新版本,才能使用快顯通知集合。 相關 API 包括 Windows.UI.Notifications.ToastCollectionWindows.UI.Notifications.ToastCollectionManager

您可以使用即時通訊應用程式查看下列範例,根據聊天群組分類通知,每個標題(Comp Sci 160A Project Chat, 私人訊息, 棍網球隊聊天)都是個別的集合。 請注意通知的分組方式,就像是來自個別應用程式一樣,即使這些通知都是來自同一個應用程式的通知。 如果您要尋找一種更微妙的方式來管理通知,請參閱 彈出通知標頭
具有兩個不同通知群組的 集合範例

建立集合

建立每個集合時,您必須提供顯示名稱和圖示,這些圖示會顯示在控制中心內做為集合標題的一部分,如上圖所示。 集合也需要啟動參數,以協助應用程式在使用者點擊集合標題時,導航至應用程式內的正確位置。

建立集合

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

將通知傳送至資料集合

我們將涵蓋透過三個不同的快顯通知管道傳送通知的方式:本機、排程和推送。 針對上述每個範例,我們將建立範例快顯通知,以便立即使用下列程式代碼傳送,然後我們將著重於如何透過每個管線將快顯通知新增至集合。

設計彈出提示內容:

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

更新集合

您可以建立具有相同標識碼的新集合,並儲存集合的新實例,以更新集合。

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

管理集合中的通知提示

群組和標記屬性

群組和標記屬性在一起可唯一識別集合內的通知。 群組(和標記)可作為複合主鍵(多個標識符)來識別您的通知。 例如,如果您想要移除或取代通知,您必須指定 您想要移除/取代的通知;您可以藉由指定標籤和群組來執行此動作。 例如傳訊應用程式。 開發人員可以使用交談標識碼作為群組,並將訊息標識碼當做標籤。

從集合中移除提示

您可以使用標記和群組標識碼來移除個別快顯通知,或清除整個集合中的所有快顯通知。

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

通知可視化檢視中的集合

您可以使用 通知可視化檢視 工具來協助設計您的集合。 請遵循下列步驟:

  • 請在右下角點擊齒輪圖示。
  • 選取 [Toast 通知集合]。
  • 在快顯通知預覽上方,有 [快顯通知集合] 下拉功能表。 選取 [管理集合]。
  • 按兩下 [新增集合],填寫集合的詳細數據,然後儲存。
  • 您可以新增更多集合,或按兩下 [管理集合] 方塊以返回主畫面。
  • 從 [Toast 通知集合] 下拉功能表中選取您要新增 toast 通知的集合。
  • 當您引發快顯通知時,該快顯通知將會新增至控制中心的適當集合。

其他詳細資料

您建立的彈跳訊息集合也會反映在使用者的通知設定中。 使用者可以切換每個個別集合的設定,以開啟或關閉這些子群組。 如果應用程式最上層關閉通知,則會關閉所有集合通知。 此外,每個集合預設會在控制中心顯示 3 個通知,而且使用者可以展開它以顯示最多 20 個通知。