定期通知概觀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 為應用程式輪詢磚或徽章更新的統一資源識別元 (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 會傳送一個 HTTP GET 要求到 URI,下載回應要求時提供的要求磚或徽章內容 (使用 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.

請注意,定期更新不可以與快顯通知搭配使用。Note that periodic updates cannot be used with toast notifications. 最適合提供快顯通知的方式是透過排程推播通知。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)、解除安裝 app 或移除磚 (次要磚) 後才會停止。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. 否則,即使不再啟動您的 app,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 現為無效或是傳回的裝載參考本機影像已不存在,則舊的 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. 最佳做法是使用一個對您的 app 或通知有意義的時間,在所有的定期磚和徽章通知上設定明確的到期時間,以確保內容不超過內容的時效性。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. 這樣可以避免佇列包含 2 則相同股票的通知,而且其中較舊的是已過期的通知。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). 在 app 存留期間只需要執行啟用佇列一次,但也可以在每次 app 啟動時進行呼叫。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 方法,通知佇列中一次可提供最多 5 個 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. 每個輪詢的 URI 可以傳回本身的到期時間與標記值。Each polled URI can return its own expiration and tag value.