【鸿蒙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}]`);
});
相关推荐
翰德恩咨询1 天前
DSTE咨询洞见:华为战略之道(二):力出一孔,拒绝战略力量分散
华为·dste
fanged1 天前
STM32(3)--时钟/定时中断/ADC(TODO)
笔记
菱玖1 天前
Linux终端命令提示行变单行
经验分享·笔记
不爱吃糖的程序媛1 天前
KuiklyUI 运行到鸿蒙平台的实践
华为·harmonyos
Pith_1 天前
模式识别与机器学习复习笔记(中)
人工智能·笔记·机器学习
哈哈你是真的厉害1 天前
React Native 鸿蒙跨平台开发:Steps 步骤条 鸿蒙实战
react native·react.js·harmonyos
Simon席玉1 天前
纯血鸿蒙中的HashMap不支持首选项存储,但是Record就支持
华为·harmonyos
黄美美分享1 天前
全新升级,小米手机互联服务电脑通用版 V1.1.0.60 新版分享!支持相册/笔记云服务/剪贴板共享!一键同步
笔记·智能手机·电脑
你要飞1 天前
考研线代第五课:特征值基础与相似对角化
笔记·线性代数·考研·矩阵
丝斯20111 天前
AI学习笔记整理(48)——大模型中的向量工程RAG系统
人工智能·笔记·学习