• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 发布进度条类型通知
2
3
4进度条通知也是常见的通知类型,主要应用于文件下载、事务处理进度显示。OpenHarmony提供了进度条模板,发布通知应用设置好进度条模板的属性值,如模板名、模板数据,通过通知子系统发送到通知栏显示。
5
6目前系统模板仅支持进度条模板,通知模板[NotificationTemplate](../reference/apis/js-apis-inner-notification-notificationTemplate.md)中的data参数为用户自定义数据,用于显示与模块相关的数据,效果示意如下图所示。
7
8![zh-cn_image_0000001416903138](figures/zh-cn_image_0000001416903138.png)
9
10
11## 接口说明
12
13[isSupportTemplate()](../reference/apis/js-apis-notificationManager.md#notificationissupporttemplate)是查询模板是否支持接口,目前仅支持进度条模板。
14
15| | |
16| -------- | -------- |
17| **接口名** | **描述** |
18| isSupportTemplate(templateName: string, callback: AsyncCallback<boolean>): void | 查询模板是否存在。 |
19
20
21## 开发步骤
22
231. [使能通知开关](notification-enable.md),获得用户授权后,才能使用通知功能。
24
252. 导入模块。
26
27   ```ts
28   import NotificationManager from '@ohos.notificationManager';
29   ```
30
313. 查询系统是否支持进度条模板,查询结果为支持downloadTemplate模板类通知。
32
33   ```ts
34   NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {
35     console.info(`[ANS] isSupportTemplate success`);
36     let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持
37     // ...
38   }).catch((err) => {
39     console.error(`[ANS] isSupportTemplate failed, error[${err}]`);
40   });
41   ```
42
43   > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
44   > 查询系统支持进度条模板后,再进行后续的步骤操作。
45
464. 构造进度条模板对象,并发布通知。
47
48   ```ts
49   let notificationRequest = {
50     id: 1,
51     content: {
52       contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
53       normal: {
54         title: 'test_title',
55         text: 'test_text',
56         additionalText: 'test_additionalText'
57       }
58     },
59     // 构造进度条模板,name字段当前需要固定配置为downloadTemplate
60     template: {
61       name: 'downloadTemplate',
62       data: { title: 'File Title', fileName: 'music.mp4', progressValue: 45 }
63     }
64   }
65
66   // 发布通知
67   NotificationManager.publish(notificationRequest, (err) => {
68     if (err) {
69       console.error(`[ANS] failed to publish, error[${err}]`);
70       return;
71     }
72     console.info(`[ANS] publish success `);
73   });
74   ```
75