• 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## Available APIs
9
10| Name| Description|
11| -------- | -------- |
12| 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. For details, see [isSupportTemplate()](../reference/apis/js-apis-notificationManager.md#notificationmanagerissupporttemplate).<br>Only the progress-type template is supported.|
13
14
15## How to Develop
16
171. [Enable notification](notification-enable.md). An application can use the notification feature only after being authorized by the user.
18
192. Import the module.
20
21   ```ts
22   import notificationManager from '@ohos.notificationManager';
23   import Base from '@ohos.base';
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:boolean) => {
30     console.info(`[ANS] isSupportTemplate success`);
31     console.info('Succeeded in supporting download template notification.');
32     let isSupportTpl: boolean = data; // The value true means that the template of the downloadTemplate type is supported, and false means the opposite.
33     // ...
34   }).catch((err:Base.BusinessError) => {
35     console.error(`Failed to support download template notification. Code is ${err.code}, message is ${err.message}`);
36   });
37   ```
38
39   > **NOTE**
40   >
41   > Proceed with the step below only when the specified template is supported.
42
434. Create a **NotificationRequest** object and publish a progress notification.
44
45   ```ts
46   let notificationRequest: notificationManager.NotificationRequest = {
47     id: 1,
48     content: {
49       contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
50       normal: {
51         title: 'test_title',
52         text: 'test_text',
53         additionalText: 'test_additionalText'
54       }
55     },
56     // Create a progress template. The name field has a fixed value of downloadTemplate.
57     template: {
58       name: 'downloadTemplate',
59       data: { title: 'File Title', fileName: 'music.mp4', progressValue: 45 }
60     }
61   }
62
63   // Publish the notification.
64   notificationManager.publish(notificationRequest, (err:Base.BusinessError) => {
65     if (err) {
66       console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
67       return;
68     }
69     console.info('Succeeded in publishing notification.');
70   });
71   ```
72