中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
实现一个发送进度条通知的方法
微信扫码分享
// NotificationUtil.ets import { notificationManager } from '@kit.NotificationKit'; // 获取应用的Want信息 export function createWantAgent(bundleName: string, abilityName: string): Promise<object> { let wantAgentInfo = { wants: [ { bundleName: bundleName, abilityName: abilityName } ], operationType: wantAgent.OperationType.START_ABILITY, requestCode: 0, wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG] } as wantAgent.WantAgentInfo; return wantAgent.getWantAgent(wantAgentInfo); } // 发布通知 export function publishNotification(progress: number, title: string, wantAgentObj: object) { // 构造通知模板对象 let template:notificationManager.NotificationTemplate = { name: 'downloadTemplate', data: { progressValue: progress, progressMaxValue: CommonConstants.PROGRESS_TOTAL, isProgressIndeterminate: false } }; // 构造NotificationRequest对象 let notificationRequest: notificationManager.NotificationRequest = { id: CommonConstants.NOTIFICATION_ID, notificationSlotType: notificationManager.SlotType.CONTENT_INFORMATION, // 模板对象 template: template, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: `${title}:${CommonConstants.DOWNLOAD_FILE}`, text: ' ', additionalText: `${progress}%` } }, // 点击拉起应用的Want信息 wantAgent: wantAgentObj }; // 发送通知 notificationManager.publish(notificationRequest).catch((err: Error) => { Logger.error(`[ANS] publish failed,message is ${err}`); }); }