你可以使用 PendingUpdate 在應用程式通知中建立多步驟互動。 例如,你可以建立一系列通知,讓後續通知依賴於前一則通知的回應。
欲了解更多關於應用程式通知的資訊,請參閱 應用程式通知總覽。
概觀
要實現一個通知,在啟用後以待更新作為其行為:
- 在背景啟用按鈕上,指定 afterActivationBehavior 為 pendingUpdate。
- 發送通知時,請指定一個標籤(也可以選擇群組)。
- 當使用者點擊按鈕時,你的背景任務會被啟動,通知會以待更新狀態顯示在螢幕上。
- 在你的背景任務中,用相同的 標籤 和 群組 發送新通知,並加入新內容,以取代待處理的通知。
設定待更新行為
備註
AppNotificationButton 目前不支援 AfterActivationBehavior。 直接使用 XML 載荷利用 AppNotification 建構函式,在按鈕上設置 afterActivationBehavior="pendingUpdate"。
在背景啟動按鈕上,設 afterActivationBehavior 為 pendingUpdate。 這僅適用於包含 activationType="background" 的按鈕。
using Microsoft.Windows.AppNotifications;
string xml = @"
<toast>
<visual>
<binding template='ToastGeneric'>
<text>Would you like to order lunch today?</text>
</binding>
</visual>
<actions>
<action
content='Yes'
arguments='action=orderLunch'
activationType='background'
afterActivationBehavior='pendingUpdate'/>
<action
content='No'
arguments='action=cancelLunch'
activationType='background'/>
</actions>
</toast>";
var notification = new AppNotification(xml);
notification.Tag = "lunch";
AppNotificationManager.Default.Show(notification);
用新內容取代通知
使用者點擊按鈕後,您的背景任務會被觸發,您將會以相同的 標籤 和 群組 取代舊通知,且發送新通知。 使用 AppNotificationBuilder.MuteAudio 以在使用者按下按鈕時,靜音替換中的音訊,因為使用者已經透過按鈕點擊與通知互動。
var notification = new AppNotificationBuilder()
.AddText("Ordering your lunch...")
.MuteAudio()
.BuildNotification();
notification.Tag = "lunch";
AppNotificationManager.Default.Show(notification);