您可以使用 PendingUpdate 在快顯通知中建立多步驟互動。 例如,如下所示,您可以建立一系列提示,其中後續提示依賴於先前提示的回應。
Important
需要桌面 Fall Creators Update 和 Notifications 函式庫的 2.0.0 版本:您必須運行桌面版本 16299 或更新版本,才能查看擱置的更新工作。 您必須使用 2.0.0 版或更新版本的 UWP Community Toolkit Notifications NuGet 程式庫 來在按鈕上指派 PendingUpdate 。 PendingUpdate 僅在電腦桌面設備上受到支援,會在其他裝置上被忽略。
Prerequisites
** 本文假設讀者具有實務知識...
Overview
若要實作在啟用後行為中使用擱置更新的快顯通知...
在快顯通知背景啟用按鈕上,指定
PendingUpdate 的 AfterActivationBehavior傳送通知時,指派 標籤(也可選擇性指派 群組)。
當使用者按下您的按鈕時,您的背景工作將會被啟動,而快顯通知將會以擱置的更新狀態保持在畫面上。
傳送含有您的新內容的新快顯通知,並使用相同的 標籤 和 群組 在您的背景工作中。
Assign PendingUpdate
在您的背景啟用按鈕上,將 AfterActivationBehavior 設定為 PendingUpdate。 請注意,這只適用於 ActivationType 且 Background的按鈕。
new ToastContentBuilder()
.AddText("Would you like to order lunch today?")
.AddButton(new ToastButton("Yes", "action=orderLunch")
{
ActivationType = ToastActivationType.Background,
ActivationOptions = new ToastActivationOptions()
{
AfterActivationBehavior = ToastAfterActivationBehavior.PendingUpdate
}
});
在通知上使用標籤
為了稍後更換通知,我們必須在通知上指派 標籤 (以及選擇性地指派 群組)。
// Create the notification
var notif = new ToastNotification(content.GetXml())
{
Tag = "lunch"
};
// And show it
ToastNotificationManager.CreateToastNotifier().Show(notif);
以新內容取代快顯通知
為了回應使用者點擊您的按鈕,您的背景工作會被觸發,並且您需要用新內容取代通知訊息。 您只需傳送一個具有相同 標籤 和 群組的新快顯通知,即可替換現有的快顯通知。
強烈建議 將音訊設定為靜音,當用戶點擊按鈕與您的快顯通知互動時,因為用戶已經正在參與這個操作。
// Generate new content
ToastContent content = new ToastContent()
{
...
// We disable audio on all subsequent toasts since they appear right after the user
// clicked something, so the user's attention is already captured
Audio = new ToastAudio() { Silent = true }
};
// Create the new notification
var notif = new ToastNotification(content.GetXml())
{
Tag = "lunch"
};
// And replace the old one with this one
ToastNotificationManager.CreateToastNotifier().Show(notif);
Related topics
- GitHub 上 完整程式碼範例
- 傳送本機通知並處理啟用
- 快顯通知內容檔
- Toast 進度條