openharmony 鸿蒙 progress-bar-notification

2022-12-13 浏览 (1401)

发布进度条类型通知

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

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

接口说明

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

接口名描述
isSupportTemplate(templateName: string): Promise<boolean>查询模板是否存在。

开发步骤

  1. 导入模块。

    import { notificationManager } from '@kit.NotificationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    const TAG: string = '[PublishOperation]';
    const DOMAIN_NUMBER: number = 0xFF00;
    
  2. 查询系统是否支持进度条模板,查询结果为支持downloadTemplate模板类通知。

    notificationManager.isSupportTemplate('downloadTemplate').then((data:boolean) => {
      hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in supporting download template notification.');
      let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持downloadTemplate模板类通知,false表示不支持
    }).catch((err: BusinessError) => {
      hilog.error(DOMAIN_NUMBER, TAG, `Failed to support download template notification. Code is ${err.code}, message is ${err.message}`);
    });
    

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

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

    let notificationRequest: notificationManager.NotificationRequest = {
      id: 5,
      content: {
        notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
          title: 'test_title',
          text: 'test_text',
          additionalText: 'test_additionalText'
        }
      },
      // 构造进度条模板,name字段当前需要固定配置为downloadTemplate
      template: {
        name: 'downloadTemplate',
        data: { title: 'File Title', fileName: 'music.mp4', progressValue: 45 }
      }
    }
    
    // 发布通知
    notificationManager.publish(notificationRequest, (err: BusinessError) => {
      if (err) {
        hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
        return;
      }
      hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.');
    });
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Notification Kit(用户通知服务)

harmony 鸿蒙发布实况窗类型通知(仅对系统应用开放)

harmony 鸿蒙管理通知角标

harmony 鸿蒙取消通知

harmony 鸿蒙清除跨设备场景下的重复通知

harmony 鸿蒙通知消息跨设备协同管理(仅对系统应用开放)

harmony 鸿蒙跨设备协同通知概述

harmony 鸿蒙请求通知授权

harmony 鸿蒙Notification Kit简介

harmony 鸿蒙为跨设备协同通知添加快捷回复

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/24443b2b0ebb4c0fb5663e6e45ff8e89