• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Publishing a Progress Notification
2
3
4The progress notification is a commonly used notification type, mainly used to display the progress of an ongoing operation, such as file downloading. When publishing a progress notification through the notification subsystem, you can use the readily available template by specifying the related attributes, such as the template name and template data.
5
6In the [NotificationTemplate](../reference/apis/js-apis-inner-notification-notificationTemplate.md), which can only be of the progress type, **data** indicates custom template data.
7
8
9## Available APIs
10
11| Name| Description|
12| -------- | -------- |
13| isSupportTemplate(templateName: string, callback: AsyncCallback&lt;boolean&gt;): void | Checks whether a specific template is supported. This API uses an asynchronous callback to return the result.<br>Only the progress-type template is supported.|
14
15
16## How to Develop
17
181. [Enable notification](notification-enable.md). An application can use the notification feature only after being authorized by the user.
19
202. Import the module.
21
22   ```ts
23   import NotificationManager from '@ohos.notificationManager';
24   ```
25
263. Check whether a specific template is supported. In this example, the template of the **downloadTemplate** type is checked.
27
28   ```ts
29   NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {
30     console.info(`[ANS] isSupportTemplate success`);
31     let isSupportTpl: boolean = data; // The value true means that the template of the downloadTemplate type is supported, and false means the opposite.
32     // ...
33   }).catch((err) => {
34     console.error(`[ANS] isSupportTemplate failed, error[${err}]`);
35   });
36   ```
37
38   > **NOTE**
39   >
40   > Proceed with the step below only when the specified template is supported.
414. Create a **NotificationRequest** object and publish a progress notification.
42
43   ```ts
44   let notificationRequest = {
45     id: 1,
46     content: {
47       contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
48       normal: {
49         title: 'test_title',
50         text: 'test_text',
51         additionalText: 'test_additionalText'
52       }
53     },
54     // Create a progress template. The name field has a fixed value of downloadTemplate.
55     template: {
56       name: 'downloadTemplate',
57       data: { title: 'File Title', fileName: 'music.mp4', progressValue: 45 }
58     }
59   }
60
61   // Publish the notification.
62   NotificationManager.publish(notificationRequest, (err) => {
63     if (err) {
64       console.error(`[ANS] failed to publish, error[${err}]`);
65       return;
66     }
67     console.info(`[ANS] publish success `);
68   });
69   ```
70