HarmonyOS开发:四类基本通知在HarmonyOS 开发涉及到多种通知类型,其包括文本、图文、进度条、横幅等类型。
本文概述
- 在HarmonyOS 开发涉及到多种通知类型,其包括文本、图文、进度条、横幅等类型。文章就使用角度给出详细说明及完整代码。
- 通知类型分为通道类型及具体通知类型
通知通道
-
社交类型:SOCIAL_COMMUNICATION
- 状态栏中显示通知图标,有横幅和提示音
-
服务类型:SERVICE_INFORMATION
- 状态栏中显示通知图标,没有有横幅但是有提示音
-
内容类型:CONTENT_INFORMATION
- 状态栏中显示通知图标,没有横幅或者提示音
-
其他类型:OTHER_TYPES
- 状态栏中不显示通知图标,没有横幅或者提示音
文本类型通知
基本说明
-
完整代码:文本类型通知
-
概述:这是最基本的通知类型,在通知触发时是有声音的
-
运行效果:
-
HarmonyOS 3.1:这里确实会有“通知附加内容”,但是在HarmonyOS 3.1 是没有的
-
HarmonyOS 4.0:
-
核心代码:
-
构建通知请求对象
// 通知请求的对象nfRequest let notificationRequest : notification.NotificationRequest = { id: 1, // 通知的ID slotType: notification.SlotType.SERVICE_INFORMATION,//通道类型 content: { // 通知内容 contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 通知类型:普通文本 normal: { // 通知的内容区域 title: '文本通知的标题', text: '文本通知的详情信息', additionalText: '通知附件内容点' // 通知的一个附件内容而已,对通知内容的补充 } } }
-
构建通知发布动作及异常处理
notification.publish(notificationRequest).then(() => { // 把Request给 发布通知 出去 console.info("通知成功") }).catch((err) => { console.info(`通知错误 错误码:${err.code}, 错误文本:${err.message}`) })
图文类型通知
基本说明
-
完整代码:图文类型通知
-
概述:图文类型,在通知触发时是没有声音的
-
运行效果:
-
HarmonyOS 3.1:类似于这种效果,但是HarmonyOS 4.0 时,无法展开
-
HarmonyOS 4.0:
-
核心代码:
-
将本地图标资源 转化为 PixelMap对象
- 注意:不能直接使用 .png 等格式的图片资源
let resouceManager = getContext(this).resourceManager let imageArray = await resouceManager.getMediaContent($r('app.media.bg').id) let imageResource = image.createImageSource(imageArray.buffer) let pixelMap = await imageResource.createPixelMap() // 图标资源对应的PixelMap对象
-
构建通知请求对象
// 通知请求的对象notificationRequest let notificationRequest : notification.NotificationRequest = { id: 1, // 通知的ID content: { contentType: notification.ContentType.NOTIFICATION_CONTENT_PICTURE, // 通知类型:图文类型 picture: { title: '图文通知的标题', // 通知的标题 text: '图文通知的内容', // 通知的内容 expandedTitle: '图文通知的展开内容', // 通知展开的内容的标题 briefText: '图文通知概要', // 通知的概要内容,是对通知的内容的总结一样 picture: pixelMap // 通知的和想 通知的图片内容 } } }
-
构建通知发布动作及异常处理
notification.publish(notificationRequest).then(() => { // 把Request给 发布通知 出去 console.info("通知成功") }).catch((err) => { console.info(`通知错误 错误码:${err.code}, 错误文本:${err.message}`) })
进度条类型通知
基本说明
-
完整代码:进度条类型通知
-
运行效果:
-
HarmonyOS 3.1:类似于这种效果,但是HarmonyOS 4.0 时,无法展开
-
HarmonyOS 4.0
-
核心代码
-
构建模板:可选
let template = { name: 'downloadTemplate',//模板名字 data: { progressValue: 67, // 当前的进度值 progressMaxValue: 100 // 最大的进度值 } }
-
模板检测:可选
// 相当于if 检测环节而已 notification.isSupportTemplate('downloadTemplate').then((data) => { let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持 })
-
构造notificationRequest 对象
let notificationRequest : notification.NotificationRequest = { id: 1, // 通知的ID content: { contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: '文件下载: 如瓮.mp3', text: '南吹先生', additionalText: '66%' }, }, template: template }
-
通知发布及异常处理
notification.publish(notificationRequest).then(() => { // 把Request给 发布通知 出去 console.info("通知成功") }).catch((err) => { console.info(`通知错误 错误码:${err.code}, 错误文本:${err.message}`) })
横幅通知及清楚通知
基本说明
-
完整代码:横幅类型通知
-
概述:横幅类型,横幅就是弹窗,在通知触发时是有声音的
-
运行效果:
-
这个动图有20s 的时间,请耐心查看
-
横幅若未弹出,建议重启手机
-
核心代码
-
构造notificationRequest 对象
let nfRequest : notification.NotificationRequest = { id: 1, // 通知的ID slotType: notification.SlotType.SOCIAL_COMMUNICATION, // 社交通信的类型 content: { contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: '通知横幅信息', text: '敬请期待', } }, }
-
通知发布及异常处理
notification.publish(nfRequest).then(() => { // 把Request给 发布通知 出去 console.info("通知成功") }).catch((err) => { console.info(`通知错误 错误码:${err.code}, 错误文本:${err.message}`) })
-
通知操作
Button('构建通知横幅') .onClick(() => { this.publishNotification() }) Button('通过通知ID取消已经发布的通知') .onClick(() => { notification.cancel(1) // 1, // 通知的ID }).margin({top: 60}) Button('取消所有已发布的通知') .onClick(() => { notification.cancelAll() }).margin({top: 60})
转载自:https://juejin.cn/post/7378084350128717851