Starting from API version 18, applications can update only published notifications, such as the upload/download progress and IMs.
The following table describes the APIs for publishing and updating notifications. Notification update can be specified by carrying the updateOnly field in the NotificationRequest parameter. If this field is not specified, it defaults to false.
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. |
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.');
});