【鸿蒙HarmonyOS开发笔记】通知模块之发布进度条类型通知

概述

进度条通知也是常见的通知类型,主要应用于文件下载、事务处理进度显示。HarmonyOS提供了进度条模板,发布通知应用设置好进度条模板的属性值,如模板名、模板数据,通过通知子系统发送到通知栏显示。

目前系统模板仅支持进度条模板,通知模板NotificationTemplate中的data参数为用户自定义数据,用于显示与模块相关的数据,效果示意如下图所示


使用方法速食版

注意事项

1.查询是否支持进度条模板可以在生命周期钩子中查询,避免重复查询浪费性能

2.进度条通知本身是静态的,想要在通知中动态显示进度需要不断的发送通知请求,同一ID会顶替掉,可以呈现动态的进度条样式


详细介绍版

接口说明

isSupportTemplate()是查询模板是否支持接口,目前仅支持进度条模板。

typescript 复制代码
isSupportTemplate(templateName: string, callback: AsyncCallback<boolean>): void
查询模板是否存在

开发步骤

导入模块

typescript 复制代码
import NotificationManager from '@ohos.notificationManager';

查询系统是否支持进度条模板,查询结果为支持downloadTemplate模板类通知。

typescript 复制代码
NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {
  console.info(`[ANS] isSupportTemplate success`);
  let isSupportTpl: boolean = data;
   // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持
  // ...
}).catch((err) => {
  console.error(`[ANS] isSupportTemplate failed, error[${err}]`);
});

说明

查询系统支持进度条模板后,再进行后续的步骤操作。


构造进度条模板对象,并发布通知

typescript 复制代码
let template = {
  name:'downloadTemplate',
  data: {
    title: '标题:',
    fileName: 'music.mp4',
    progressValue: 30,
    progressMaxValue:100,
  }
}
//构造NotificationRequest对象
let notificationRquest = {
  id: 1,
  slotType: notify.SlotType.OTHER_TYPES,
  template: template,
  content: {
    contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
    normal: {
      title: template.data.title + template.data.fileName,
      text: "sendTemplate",
      additionalText: "30%"
    }
  },
  deliveryTime: new Date().getTime(),
  showDeliveryTime: true
}
notify.publish(notificationRquest).then(() => {
  console.info(`[ANS] publish success `);
}).catch((err) => {
  console.error(`[ANS] failed to publish, error[${err}]`);
});
相关推荐
沐风听雨_A2 小时前
雄迈IP摄像头配置笔记
笔记
沐风听雨_A4 小时前
有人串口转Wifi模块配置笔记
笔记
90后的晨仔4 小时前
鸿蒙ArkUI如何使用RGB、十六进制等设置颜色值?
harmonyos
小智RE0-走在路上5 小时前
Python学习笔记(11) --数据可视化
笔记·python·学习
音浪豆豆_Rachel6 小时前
Flutter鸿蒙化之深入解析Pigeon多参数接口设计:multiple_arity.dart全解
flutter·harmonyos
音浪豆豆_Rachel6 小时前
Flutter鸿蒙文件选择器深度解析:文本文件处理与跨平台挑战
flutter·harmonyos
么么...7 小时前
在 Ubuntu 上安装 Docker 并部署 MySQL 容器
linux·运维·经验分享·笔记·mysql·ubuntu·docker
云川之下7 小时前
【网络】华为交换机S3700与S5700详解
服务器·网络·华为
Asus.Blogs7 小时前
SSE + Resty + Goroutine + Channel 完整学习笔记
笔记·学习·golang
雍凉明月夜8 小时前
深度学习网络笔记Ⅱ(常见网络分类1)
人工智能·笔记·深度学习