快顯通知上的自訂時間戳記Custom timestamps on toasts

根據預設,快顯通知上的時間戳記 (顯示在控制中心) 會設定為傳送通知的時間。By default, the timestamp on toast notifications (visible within Action Center) is set to the time that the notification was sent.

Toast with custom timestamp

您可選擇以您自己的自訂日期和時間來覆寫時間戳記,讓時間戳記代表訊息/資訊/內容的實際建立時間,而不是傳送通知的時間。You can optionally override the timestamp with your own custom date and time, so that the timestamp represents the time the message/information/content was actually created, rather than the time that the notification was sent. 這也可確保您的通知以正確的順序出現在控制中心 (依照時間排序)。This also ensures that your notifications appear in the correct order within Action Center (which are sorted by time). 我們建議大部分應用程式指定自訂時間戳記。We recommend that most apps specify a custom timestamp.

重要

需要 Desktop Creators Update 和 Notifications 程式庫 1.4.0:您必須執行組建 15063 或更新版本,才能看見自訂時間戳記。Requires Creators Update and 1.4.0 of Notifications library: You must be running build 15063 or higher to see custom timestamps. 您必須使用版本 1.4.0 或更高版本的 UWP Community Toolkit Notifications NuGet 程式庫,以便在快顯通知內容上指派時間戳記。You must use version 1.4.0 or higher of the UWP Community Toolkit Notifications NuGet library to assign the timestamp on your toast's content.

若要使用自訂時間戳記,只需在 ToastContent 上指派 DisplayTimestamp 屬性。To use a custom timestamp, simply assign the DisplayTimestamp property on your ToastContent.

var content = new ToastContent()
    .AddCustomTimeStamp(new DateTime(2017, 04, 15, 19, 45, 00, DateTimeKind.Utc))
    ...

如果您使用 XML,日期必須使用 ISO 8601 的格式。If you are using XML, the date must be formatted in ISO 8601.

注意

您最多只能在秒數使用 3 個小數位數 (但提供這麼細微的值沒有任何實際價值)。You can only use at most 3 decimal places on the seconds (although realistically there's no value in providing anything that granular). 如果提供更多位數,承載將會無效,而您會收到「新通知」通知。If you provide more, the payload will be invalid and you will receive the "New notification" notification.

用法指導方針Usage guidance

一般而言,我們建議大部分應用程式指定自訂時間戳記。In general, we recommend that most apps specify a custom timestamp. 這樣可確保無論是否存在網路延遲、飛航模式或定期背景工作的固定間隔,通知的時間戳記都能準確代表訊息/資訊/內容的產生時間。This ensures that the notification's timestamp accurately represents when the message/information/content was generated, regardless of network delays, airplane mode, or the fixed interval of periodic background tasks.

例如,新聞應用程式可能會每隔 15 分鐘執行一次背景工作,檢查是否有新文章並顯示通知。For example, a news app might run a background task every 15 minutes that checks for new articles and displays notifications. 在自訂時間戳記前,時間戳記對應到快顯通知的產生時間 (因此一律是 15 分鐘間隔)。Before custom timestamps, the timestamp corresponded to when the toast notification was generated (therefore always in 15 minute intervals). 不過,現在應用程式可以將時間戳記設定為文章的實際發佈時間。However, now the app can set the timestamp to the time the article was actually published. 同樣地,如果電子郵件應用程式和社交網路應用程式將類似的定期提取模式用於通知,便也能受惠於此功能。Similarly, email apps and social network apps can benefit from this feature if a similar pattern of periodic pulling is used for their notifications.

此外,提供自訂時間戳記還能確保即使使用者中斷網際網路連線,時間戳記也能正確無誤。Additionally, providing a custom timestamp ensures that the timestamp is correct even if the user was disconnected from the internet. 例如,當使用者開啟電腦而您的背景工作開始執行,您最終可以確保您通知的時間戳記代表傳送訊息的時間,而不是使用者開啟電腦的時間。For example, when the user turns their computer on and your background task runs, you can finally ensure that the timestamp on your notifications represents the time that the messages were sent, rather than the time the user turned on their computer.

預設時間戳記Default timestamp

如果您不提供自訂時間戳記,我們會使用傳送通知的時間。If you don't provide a custom timestamp, we use the time that your notification was sent.

如果您透過 WNS 傳送推播通知,我們會使用 WNS 伺服器收到通知的時間 (因此延遲傳送通知到裝置不會影響時間戳記)。If you sent a push notification through WNS, we use the time when the notification was received by WNS server (so any latency on delivering the notification to the device won't impact the timestamp).

如果您傳送本機通知,我們會使用通知平台收到通知的時間 (這應該是立即發生)。If you sent a local notification, we use the time when the notification platform received the notification (which should be immediately).