如何计划磁贴通知 (HTML)

[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]

注意  不使用 JavaScript?请参阅如何计划磁贴通知 (XAML)

 

此主题显示了如何将磁贴通知安排在特定时间出现。

你需要了解的内容

技术

  • Windows Runtime

先决条件

说明

步骤 1: 指定模板

在指定传递时间前,必须创建通知。

注意  在 Windows 8 系统中调用 getTemplateContent 时,它会返回版本 1 模板。在 Windows 8.1 系统中调用此方法时,它会返回版本 2 模板版本 3 模板(仅适用于电话模板的情况下)。但是,如果应用在其清单中指定了 Windows 8 兼容性,则无论 Windows 是什么版本,该方法都会返回版本 1 模板。本主题中使用版本 2 模板。

 


var template = Windows.UI.Notifications.Tile.tileSquare150x150Text01;                        
var tileXml = Windows.UI.Notifications.TileUpdateManager.getTemplateContent(template);

步骤 2: 提供通知内容

我们将不会在这里对此进行介绍,因为计划通知和非计划通知的内容相同。有关详细信息,请参阅快速入门:发送磁贴更新

步骤 3: 指定应传递通知的时间

此示例指定通知应在 3 秒内出现。此示例使用 JavaScript 日期对象检索当前时间。


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

步骤 4: 创建计划的磁贴通知对象

发送磁贴通知内容和计划传递时间至构造函数。

var scheduledTile = new Windows.UI.Notifications.ScheduledTileNotification(tileXml, startTime);

步骤 5: 可选:为计划的磁贴通知赋予 ID

此 ID 不能超过 16 个字符。

scheduledTile.id = "Future_Tile";

步骤 6: 向计划中添加磁贴通知。

创建 TileUpdater 对象,该对象反过来用于向计划中添加通知。


var tileUpdater = Windows.UI.Notifications.TileUpdateManager.createTileUpdaterForApplication();
tileUpdater.addToSchedule(scheduledTile);

相关主题

应用磁贴和锁屏提醒示例