Toast 上的自定义时间戳Custom timestamps on toasts

默认情况下,Toast 通知(操作中心内可见)上的时间戳设置为发送通知的时间。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.

重要

需要创意者更新和通知库 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 社区工具包通知 NuGet 库来分配 Toast 的内容上的时间戳。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 8601If 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. 在使用自定义时间戳之前,时间戳对应 Toast 通知生成的时间(因此始终间隔 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.

此外,提供自定义时间戳可以确保时间戳是正确的,即使用户断开与 Internet 的连接。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).