定期通知概述Periodic notification overview

定期通知(也称为轮询通知)通过从云服务下载内容,以固定间隔更新磁贴和锁屏提醒。Periodic notifications, which are also called polled notifications, update tiles and badges at a fixed interval by downloading content from a cloud service. 若要使用定期通知,客户端应用代码需要提供两部分信息:To use periodic notifications, your client app code needs to provide two pieces of information:

  • Windows 为你的应用轮询磁贴或锁屏提醒更新的 Web 位置的统一资源标识符 (URI)The Uniform Resource Identifier (URI) of a web location for Windows to poll for tile or badge updates for your app
  • URI 的轮询频率How often that URI should be polled

定期通知让你的应用只需极少的云服务和客户端投资即可提供动态磁贴更新。Periodic notifications enable your app to get live tile updates with minimal cloud service and client investment. 定期通知是向广泛受众分配相同内容的出色传递方法。Periodic notifications are a good delivery method for distributing the same content to a wide audience.

注意   要了解详细信息,可以下载 Windows 8.1 的推送和定期通知示例,然后在 Windows 10 应用中重复使用其源代码。Note   You can learn more by downloading the Push and periodic notifications sample for Windows 8.1 and re-using its source code in your Windows 10 app.

 

工作原理How it works

定期通知要求应用托管一个云服务。Periodic notifications require that your app hosts a cloud service. 该服务将由所有安装了该应用的用户定期进行轮询。The service will be polled periodically by all users who have the app installed. Windows 在每个轮询间隔(例如一小时一次)向 URI 发送 HTTP GET 请求,下载响应请求而提供的磁贴或锁屏提醒内容(如 XML),并在应用磁贴上显示此内容。At each polling interval, such as once an hour, Windows sends an HTTP GET request to the URI, downloads the requested tile or badge content (as XML) that is supplied in response to the request, and displays the content on the app's tile.

请注意,定期更新不能与 Toast 通知配合使用。Note that periodic updates cannot be used with toast notifications. Toast 最好通过计划推送通知来传递。Toast is best delivered through scheduled or push notifications.

URI 位置和 XML 内容URI location and XML content

任何有效的 HTTP 或 HTTPS 网址均可用作要轮询 URI。Any valid HTTP or HTTPS web address can be used as the URI to be polled.

云服务器的响应包括已下载的内容。The cloud server's response includes the downloaded content. 从 URI 返回的内容必须符合磁贴锁屏提醒 XML 架构规格,并且必须为 UTF-8 编码。The content returned from the URI must conform to the Tile or Badge XML schema specification, and must be UTF-8 encoded. 可使用已定义的 HTTP 标头来指定通知的到期时间或标记。You can use defined HTTP headers to specify the expiration time or tag for the notification.

轮询行为Polling Behavior

可调用以下方法之一开始轮询:Call one of these methods to begin polling:

调用一个方法时,URI 将立即进行轮询,且磁贴或锁屏提醒将根据所接收的内容进行更新。When you call one of these methods, the URI is immediately polled and the tile or badge is updated with the received contents. 初次轮询后,Windows 继续按照所要求的间隔提供更新。After this initial poll, Windows continues to provide updates at the requested interval. 轮询不断进行,直到(使用 TileUpdater.StopPeriodicUpdate)将其显式阻止、卸载应用或删除磁贴(适用于辅助磁贴的情况)。Polling continues until you explicitly stop it (with TileUpdater.StopPeriodicUpdate), your app is uninstalled, or, in the case of a secondary tile, the tile is removed. 否则,即使应用从此再不启动,Windows 也将继续轮询磁贴或锁屏提醒更新。Otherwise, Windows continues to poll for updates to your tile or badge even if your app is never launched again.

重复出现的间隔The recurrence interval

指定重复出现的间隔,作为上面所列方法的一个参数。You specify the recurrence interval as a parameter of the methods listed above. 请注意,虽然 Windows 尽量根据请求进行轮询,但此间隔不是很准确。Note that while Windows makes a best effort to poll as requested, the interval is not precise. 所请求的轮询间隔最长可能延迟 15 分钟,具体取决于 Windows。The requested poll interval can be delayed by up to 15 minutes at the discretion of Windows.

开始时间The start time

你可以选择指定一天中的某个特定时间开始轮询。You optionally can specify a particular time of day to begin polling. 考虑应用一天仅更改一次磁贴内容。Consider an app that changes its tile content just once a day. 在此情况下,我们建议你将轮询开始时间设置为接近更新云服务的时间。In such a case, we recommend that you poll close to the time that you update your cloud service. 例如,如果一家日常购物网站每天早上 8 点发布当日的商品与服务,则在早上 8 点不久之后轮询新磁贴内容。For example, if a daily shopping site publishes the day's offers at 8 AM, poll for new tile content shortly after 8 AM.

如果提供开始时间,则首次调用该方法时即会立即轮询内容。If you provide a start time, the first call to the method polls for content immediately. 然后,在指定开始时间的 15 分钟之内开始定期轮询。Then, regular polling starts within 15 minutes of the provided start time.

自动重试行为Automatic retry behavior

只能在设备联机时轮询 URI。The URI is polled only if the device is online. 如果网络可用,但因某种原因无法联系 URI,则轮询间隔此次迭代将被跳过,并将在下个间隔再次轮询 URI。If the network is available but the URI cannot be contacted for any reason, this iteration of the polling interval is skipped, and the URI will be polled again at the next interval. 如果达到轮询间隔时,该设备处于关闭、睡眠或休眠状态,则设备从关闭或睡眠状态返回时,将轮询 URI。If the device is in an off, sleep, or hibernated state when a polling interval is reached, the URI is polled when the device returns from its off or sleep state.

处理应用更新Handling app updates

如果发布更改轮询 URI 的应用更新,你应该添加使用新 URI 调用 StartPeriodicUpdate 的每日时间触发器后台任务,以确保磁贴使用新的 URI。If you release an app update that changes your polling URI, you should add a daily time trigger background task which calls StartPeriodicUpdate with the new URI to ensure your tiles are using the new URI. 否则,如果用户收到应用更新但未启动应用,他们的磁贴仍将使用旧 URI,如果 URI 已无效或者返回负载引用了本地不再存在的图像,磁贴可能会无法显示。Otherwise, if users receive your app update but don't launch your app, their tiles will still be using the old URI, which may fail to display if the URI is now invalid or if the returned payload references local images that no longer exist.

磁贴和锁屏提醒通知过期时间Expiration of tile and badge notifications

默认情况下,定期磁贴和锁屏提醒通知在从下载时刻算起的三天后过期。By default, periodic tile and badge notifications expire three days from the time they are downloaded. 通知到期时,此内容将从锁屏提醒、磁贴或队列中删除,且不再向用户显示。When a notification expires, the content is removed from the badge, tile, or queue and is no longer shown to the user. 最佳做法是,使用一个对于你的应用或通知合理的时间,在所有定期磁贴和锁屏提醒通知上设置显式过期时间,以确保该内容不会在它不相关时仍存在。It is a best practice to set an explicit expiration time on all periodic tile and badge notifications, using a time that makes sense for your app or notification, to ensure that the content does not persist longer than it is relevant. 对于具有已定义的使用寿命的内容来说,一个显式过期时间是必需的。An explicit expiration time is essential for content with a defined life span. 它还可确保在无法连接云服务或用户长时间断开网络时删除过时的内容。It also assures the removal of stale content if your cloud service becomes unreachable, or if the user disconnects from the network for an extended period of time.

你的云服务通过在响应负载中包括 X-WNS-Expires HTTP 标头来为通知设置过期日期和时间。Your cloud service sets an expiration date and time for a notification by including the X-WNS-Expires HTTP header in the response payload. X-WNS-Expires HTTP 标头符合 HTTP-date 格式The X-WNS-Expires HTTP header conforms to the HTTP-date format. 有关详细信息,请参阅 StartPeriodicUpdateStartPeriodicUpdateBatchFor more information, see StartPeriodicUpdate or StartPeriodicUpdateBatch.

例如,股票市场活跃交易日期间,你可将股票价格更新到期时间设置为轮询间隔的两倍(例如,如果是每半小时轮询一次,则将股票价格更新到期时间设置为一小时)。For example, during a stock market's active trading day, you can set the expiration for a stock price update to twice that of your polling interval (such as one hour after receipt if you are polling every half-hour). 另一个示例是,新闻应用可确定每日新闻磁贴更新的适当到期时间为一天。As another example, a news app might determine that one day is an appropriate expiration time for a daily news tile update.

通知队列中的定期通知Periodic notifications in the notification queue

你可将定期磁贴更新与通知循环结合使用。You can use periodic tile updates with notification cycling. 默认情况下,“开始”屏幕中的磁贴会显示单条通知的内容,直到有新的通知替换当前的通知。By default, a tile on the Start screen shows the content of a single notification until it is replaced by a new notification. 启用循环之后,队列中最多会保留 5 条通知,磁贴会循环显示这些通知。When you enable cycling, up to five notifications are maintained in a queue and the tile cycles through them.

如果队列达到了 5 条通知的容量上限,则下一条新通知将替换队列中最早的通知。If the queue has reached its capacity of five notifications, the next new notification replaces the oldest notification in the queue. 但是,通过在通知上设置标记,你可以影响队列的替换策略。However, by setting tags on your notifications, you can affect the queue's replacement policy. 标记是特定于应用的字符串,区分大小写,最多包含 16 个字母数字字符,可在响应负载的 X-WNS-Tag HTTP 标头中指定。A tag is an app-specific, case-insensitive string of up to 16 alphanumeric characters, specified in the X-WNS-Tag HTTP header in the response payload. Windows 将传入通知的标记与队列中已存在的所有通知的标记进行比较。Windows compares the tag of an incoming notification with the tags of all notifications already in the queue. 如果发现匹配,则新通知将替换队列中标记相同的通知。If a match is found, the new notification replaces the queued notification with the same tag. 如果发现不匹配,则应用默认替换规则,新通知将替换队列中最早的通知。If no match is found, the default replacement rule is applied and the new notification replaces the oldest notification in the queue.

你可使用通知队列和标记来实现各种丰富的通知方案。You can use notification queuing and tagging to implement a variety of rich notification scenarios. 例如,股票应用可发送 5 条通知,每条通知关注一支不同的股票并以相应股票的名称作为标记。For example, a stock app could send five notifications, each about a different stock and each tagged with a stock name. 这可以防止队列包含两条有关同一股票的通知,其中较早的通知为过时通知。This prevents the queue from ever containing two notifications for the same stock, the older of which is out of date.

有关详细信息,请参阅使用通知队列For more information, see Using the notification queue.

启用通知队列Enabling the notification queue

若要实现通知队列,请首先启用磁贴队列(请参阅如何使用具有本地通知的通知队列)。To implement a notification queue, first enable the queue for your tile (see How to use the notification queue with local notifications). 在应用的生命周期内,调用启用队列必须一次性完成,但在每次启动应用时均调用一次也无妨。The call to enable the queue needs to be done only once in your app's lifetime, but there is no harm in calling it each time your app is launched.

你可以一次轮询多条通知。Polling for more than one notification at a time

你必须为希望 Windows 为磁贴下载的每条通知提供唯一的 URI。You must provide a unique URI for each notification that you'd like Windows to download for your tile. 你可使用 StartPeriodicUpdateBatch 方法一次最多提供五个 URI 与通知队列配合使用。By using the StartPeriodicUpdateBatch method, you can provide up to five URIs at once for use with the notification queue. 将在相同时间或接近该时间轮询每个 URI 以获得单个通知负载。Each URI is polled for a single notification payload, at or near the same time. 每个轮询的 URL 还可以返回各自的到期时间和标记值。Each polled URI can return its own expiration and tag value.