HarmonyOS 鸿蒙Next通知怎么显示时间?

HarmonyOS 鸿蒙Next通知怎么显示时间? 请问在代码中加什么可以在通知上显示时间?谢谢大佬!

cke_212.png

cke_7635.png

cke_597.png


更多关于HarmonyOS 鸿蒙Next通知怎么显示时间?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

NotificationRequest文档
deliveryTime : 通知发送时间。

更多关于HarmonyOS 鸿蒙Next通知怎么显示时间?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙Next)中,通知显示时间的功能可以通过系统默认设置实现。当应用发送通知时,系统会自动在通知的右侧或底部显示通知接收的时间。开发者无需额外编写代码来显示时间,系统会根据用户设备的设置自动处理时间格式和显示位置。

如果需要自定义通知的时间显示,可以使用NotificationTemplate中的NotificationBasicTemplateNotificationLongTextTemplate,并通过NotificationRequest设置通知的时间戳(deliveryTime)。系统会根据该时间戳显示相应的通知时间。

例如:

let notificationRequest: NotificationRequest = {
    id: 1,
    content: {
        contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "通知标题",
            text: "通知内容",
            additionalText: "附加信息"
        }
    },
    deliveryTime: new Date().getTime() // 设置通知的时间戳
};

以上代码中,deliveryTime用于指定通知的时间戳,系统会根据该时间戳显示通知的接收时间。

回到顶部