Updating a Notification
Starting from API version 18, applications can update only published notifications. such as the upload/download progress and IMs.
Available APIs
The table below lists the API for updating notifications. You can use the updateOnly field in NotificationRequest to specify the notification to update. The value is set to false if no notification is specified.
-
When updateOnly is set to true, if a notification with the same ID exists, it will be updated. If no notification with the same ID exists, the update fails and no new notification is created.
-
When updateOnly is set to false, if a notification with the same ID exists, it will be updated. If no notification with the same ID exists, a new notification is created.
| API | Description |
|---|---|
| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | Publishes an updated notification. |
How to Develop
The following uses the progress bar notification as an example.
-
Import modules.
import { notificationManager } from '@kit.NotificationKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; const TAG: string = '[PublishOperation]'; const DOMAIN_NUMBER: number = 0xFF00; -
Publish the progress bar notification.
let notificationRequest: notificationManager.NotificationRequest = { id: 5, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: 'test_title', text: 'test_text', additionalText: 'test_additionalText' } }, // Create a progress template. The name field has a fixed value of downloadTemplate. template: { name: 'downloadTemplate', data: { title: 'File Title', fileName: 'music.mp4', progressValue: 50 } } }; // Publish the notification. 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.'); }); -
Update the progress bar notification using the updateOnly field in the NotificationRequest API.
let notificationRequest: notificationManager.NotificationRequest = { id: 5, updateOnly: true, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: 'test_title', text: 'test_text', additionalText: 'test_additionalText' } }, // Create a progress template. The name field has a fixed value of downloadTemplate. template: { name: 'downloadTemplate', data: { title: 'File Title', fileName: 'music.mp4', progressValue: 99 } } }; // Update the published notification. notificationManager.publish(notificationRequest, (err: BusinessError) => { if (err) { hilog.error(DOMAIN_NUMBER, TAG, `Failed to update notification. Code is ${err.code}, message is ${err.message}`); return; } hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in updating notification.'); });