智能客服
你问我答,随时在线为你解决问题

























从API version 18开始,支持应用只更新已发布的通知。主要用于上传下载进度更新、IM会话消息更新等场景。
通知发布更新接口说明详见下表,通知更新可通过入参NotificationRequest携带updateOnly字段来指定,不指定该字段默认为false。
当updateOnly为true时,若相同ID通知存在,则更新通知;若相同ID通知不存在,则更新失败,并且不创建新的通知。
当updateOnly为false时,若相同ID通知存在,则更新通知;若相同ID通知不存在,则创建通知。
| 接口名 | 描述 |
|---|---|
| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | 发布更新通知。 |
下面以进度条通知发布更新为例。
导入模块。
- import { notificationManager } from '@kit.NotificationKit';
- import { BusinessError } from '@kit.BasicServicesKit';
- import { hilog } from '@kit.PerformanceAnalysisKit';
-
- const TAG: string = '[PublishOperation]';
- const DOMAIN_NUMBER: number = 0xFF00;
发布进度条通知。
- let notificationRequest: notificationManager.NotificationRequest = {
- id: 5,
- content: {
- notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
- normal: {
- title: 'test_title',
- text: 'test_text',
- additionalText: 'test_additionalText'
- }
- },
- // 构造进度条模板,name字段当前需要固定配置为downloadTemplate
- template: {
- name: 'downloadTemplate',
- data: { title: 'File Title', fileName: 'music.mp4', progressValue: 50 }
- }
- };
-
- // 发布通知
- 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.');
- });
通过NotificationRequest接口携带updateOnly字段更新进度条通知。
- 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'
- }
- },
- // 构造进度条模板,name字段当前需要固定配置为downloadTemplate
- template: {
- name: 'downloadTemplate',
- data: { title: 'File Title', fileName: 'music.mp4', progressValue: 99 }
- }
- };
-
- // 更新发布通知
- 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.');
- });
智能客服
你问我答,随时在线为你解决问题
合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功