• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Updating a Notification
2
3You can use this API to update published notifications, such as the upload/download progress and IMs.
4
5## Available APIs
6
7The table below lists the API for updating notifications. You can use the **updateOnly** field in [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1) to specify the notification to update. The value is set to **false** if no notification is specified.
8
9- Set **updateOnly** to **true**, the notification is updated if the notification with the same ID exists; the notification fails to update if the notification with the same ID does not exist and no notification is created.
10
11- Set **updateOnly** to **false**, the notification is updated if the notification with the same ID exists; a notification is created if the notification with the same ID does not exist.
12
13| **API**| **Description**|
14| -------- | -------- |
15| [publish](../reference/apis-notification-kit/js-apis-notificationManager.md#notificationmanagerpublish)(request: NotificationRequest, callback: AsyncCallback<void>): void | Publishes an updated notification.                |
16
17
18## How to Develop
19
20The following uses the progress bar notification as an example.
21
221. Import modules.
23
24   ```ts
25   import { notificationManager } from '@kit.NotificationKit';
26   import { BusinessError } from '@kit.BasicServicesKit';
27   import { hilog } from '@kit.PerformanceAnalysisKit';
28
29   const TAG: string = '[PublishOperation]';
30   const DOMAIN_NUMBER: number = 0xFF00;
31   ```
32
332. Publish the progress bar notification.
34
35   ```ts
36   let notificationRequest: notificationManager.NotificationRequest = {
37     id: 5,
38     content: {
39       notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
40       normal: {
41         title: 'test_title',
42         text: 'test_text',
43         additionalText: 'test_additionalText'
44       }
45     },
46     // Create a progress template. The name field has a fixed value of downloadTemplate.
47     template: {
48       name: 'downloadTemplate',
49       data: { title: 'File Title', fileName: 'music.mp4', progressValue: 50 }
50     }
51   }
52
53   // Publish the notification.
54   notificationManager.publish(notificationRequest, (err: BusinessError) => {
55     if (err) {
56       hilog.error(DOMAIN_NUMBER, TAG, `Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
57       return;
58     }
59     hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in publishing notification.');
60   });
61   ```
62
633. Update the progress bar notification using the **updateOnly** field in the [NotificationRequest](../reference/apis-notification-kit/js-apis-inner-notification-notificationRequest.md#notificationrequest-1) API.
64
65   ```ts
66   let notificationRequest: notificationManager.NotificationRequest = {
67     id: 5,
68     updateOnly: true,
69     content: {
70       notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
71       normal: {
72         title: 'test_title',
73         text: 'test_text',
74         additionalText: 'test_additionalText'
75       }
76     },
77     // Create a progress template. The name field has a fixed value of downloadTemplate.
78     template: {
79       name: 'downloadTemplate',
80       data: { title: 'File Title', fileName: 'music.mp4', progressValue: 99 }
81     }
82   }
83
84   // Update the published notification.
85   notificationManager.publish(notificationRequest, (err: BusinessError) => {
86     if (err) {
87       hilog.error(DOMAIN_NUMBER, TAG, `Failed to update notification. Code is ${err.code}, message is ${err.message}`);
88       return;
89     }
90     hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in updating notification.');
91   });
92   ```
93