如何排程快顯通知 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

注意  不是使用 JavaScript?請參閱如何排程快顯通知 (XAML)

 

這個主題說明如何排程以在特定時間顯示快顯通知。

您必須知道的事

技術

  • Windows Runtime

先決條件

指示

步驟 1: 指定範本

您必須先建立通知,才能指定傳遞時間。


var template = Windows.UI.Notifications.ToastTemplateType.toastText02;                        
var toastXml = Windows.UI.Notifications.ToastNotificationManager.getTemplateContent(template);

步驟 2: 提供快顯通知內容

我們不會在這裡涵蓋此內容,因為排程快顯通知與非排程快顯通知的內容相同。如需詳細資訊,請參閱快速入門:傳送快顯通知

步驟 3: 指定應該傳遞快顯通知的時間。

以下範例指定應該在 3 秒內顯示通知。以下範例使用 JavaScript Date 物件來擷取目前的時間。


var currentTime = new Date();
var startTime = new Date(currentTime.getTime() + 3 * 1000);

步驟 4: 建立排程快顯通知物件

將快顯通知內容與排程的傳遞時間傳送至建構函式。

var scheduledToast = new Windows.UI.Notifications.ScheduledToastNotification(toastXml, startTime);

步驟 5: 選用:指定排程快顯通知的識別碼

這個識別碼的長度最多 16 個字元。如果您想要取消通知,可以稍後再使用。

scheduledToast.id = "Future_Toast";

步驟 6: 將快顯通知新增至排程。

建立用以將通知新增至排程的 ToastNotifier 物件。


var toastNotifier = Windows.UI.Notifications.ToastNotificationManager.createToastNotifier();
toastNotifier.addToSchedule(scheduledToast);

新增會以特定間隔重複的快顯通知

下列程式碼會顯示單一快顯通知 5 次,每次間隔一分鐘。為求簡潔,此處省略填入範本中的程式碼。


var template = Windows.UI.Notifications.ToastTemplateType.toastText02;
var toastXml = Windows.UI.Notifications.ToastNotificationManager.getTemplateContent(template);

// TO DO: Fill in the template with your notification content here. 
 
var currentTime = new Date();
var startTime = new Date(currentTime.getTime() + 1000);
 
var recurringToast = new Windows.UI.Notifications.ScheduledToastNotification(toastXml, startTime, 60 * 1000, 5);
recurringToast.id = "Recurring_Toast";

var toastNotifier = Windows.UI.Notifications.ToastNotificationManager.createToastNotifier();
toastNotifier.addToSchedule(recurringToast);