1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import notification from '@ohos.notificationManager'; 17import wantAgent from '@ohos.app.ability.wantAgent'; 18import type common from '@ohos.app.ability.common'; 19import { Action, PACKAGE_NAME } from '@ohos/common/src/main/ets/const/update_const'; 20import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils'; 21import { INotify } from '@ohos/common/src/main/ets/manager/UpgradeInterface'; 22 23/** 24 * 通知辅助者 25 * 26 * @since 2022-06-05 27 */ 28export class NotificationHelper implements INotify { 29 /** 30 * 跳转信息--跳转到搜包页面 31 */ 32 private checkWantAgentInfo = { 33 wants: [{ 34 bundleName: PACKAGE_NAME, 35 abilityName: 'ServiceExtAbility', 36 action: Action.NOTIFICATION_CHECK 37 }], 38 operationType: wantAgent.OperationType.START_ABILITY, 39 requestCode: 0, 40 wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 41 }; 42 43 /** 44 * 跳转信息--下载中拉起界面 45 */ 46 private downloadingWantAgentInfo = { 47 wants: [{ 48 bundleName: PACKAGE_NAME, 49 abilityName: 'ServiceExtAbility', 50 action: Action.NOTIFICATION_DETAIL 51 }], 52 operationType: wantAgent.OperationType.START_ABILITY, 53 requestCode: 0, 54 wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 55 }; 56 57 /** 58 * 下载进度通知 59 * 60 * @param version 版本号 61 * @param progress 进度 62 * @param context 上下文 63 */ 64 async showDownloading(version, progress, context): Promise<void> { 65 let templateName: string = 'downloadTemplate'; 66 if (!globalThis.isSupportTemplate) { 67 globalThis.isSupportTemplate = await notification.isSupportTemplate(templateName).catch(err => { 68 this.logError('showDownloading isSupportTemplate failed because ' + JSON.stringify(err)); 69 return false; 70 }); 71 } 72 if (!globalThis.isSupportTemplate) { 73 this.logError('showDownloading is not supportTemplate'); 74 return; 75 } 76 var notificationRequest = { 77 content: { 78 contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 79 normal: { 80 title: await context.resourceManager.getString($r('app.string.software_update').id), 81 text: await context.resourceManager.getString($r('app.string.software_download_progress').id) 82 }, 83 }, 84 template: { 85 name: 'downloadTemplate', 86 data: { 87 title: await context.resourceManager.getString($r('app.string.software_download_progress').id), 88 fileName: version, 89 progressValue: progress 90 } 91 }, 92 wantAgent: await wantAgent.getWantAgent(this.downloadingWantAgentInfo), 93 id: 5, 94 label: '111', 95 slotType: notification.SlotType.SERVICE_INFORMATION, 96 deliveryTime: new Date().getTime() 97 } 98 await notification.publish(notificationRequest).catch((err) => { 99 this.logError('showDownloading notification publish failed because ' + JSON.stringify(err)); 100 }); 101 } 102 103 /** 104 * 下载进度通知 105 * 106 * @param version 版本号 107 * @param progress 进度 108 * @param context 上下文 109 */ 110 async showInstalling(version, progress, context): Promise<void> { 111 let templateName: string = 'installTemplate'; 112 if (!globalThis.isSupportTemplate) { 113 globalThis.isSupportTemplate = await notification.isSupportTemplate(templateName).catch(err => { 114 this.logError('showInstalling isSupportTemplate failed because ' + JSON.stringify(err)); 115 return false; 116 }); 117 } 118 if (!globalThis.isSupportTemplate) { 119 this.logError('showInstalling is not supportTemplate'); 120 return; 121 } 122 var notificationRequest = { 123 content: { 124 contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 125 normal: { 126 title: await context.resourceManager.getString($r('app.string.software_update').id), 127 text: await context.resourceManager.getString($r('app.string.software_install_progress').id) 128 }, 129 }, 130 template: { 131 name: 'installTemplate', 132 data: { 133 title: await context.resourceManager.getString($r('app.string.software_install_progress').id), 134 fileName: version, 135 progressValue: progress 136 } 137 }, 138 wantAgent: await wantAgent.getWantAgent(this.downloadingWantAgentInfo), 139 id: 5, 140 label: '111', 141 slotType: notification.SlotType.SERVICE_INFORMATION, 142 deliveryTime: new Date().getTime() 143 } 144 await notification.publish(notificationRequest).catch((err) => { 145 this.logError('showInstalling publish failed because ' + JSON.stringify(err)); 146 }); 147 } 148 149 /** 150 * 弹安装失败通知 151 * 152 * @param context 实上下文 153 */ 154 async showUpgradeFailed(versionName: string, context: common.Context): Promise<void> { 155 let request = { 156 content: { 157 contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 158 normal: { 159 title: await context.resourceManager.getString($r('app.string.install_fail_message').id), 160 text: versionName 161 } 162 }, 163 wantAgent: await wantAgent.getWantAgent(this.checkWantAgentInfo), 164 id: 3, 165 slotType: notification.SlotType.SERVICE_INFORMATION 166 } 167 await notification.publish(request).then(() => { 168 this.logInfo('showUpgradeFailed publish promise success.'); 169 }).catch((err) => { 170 this.logError('showUpgradeFailed publish promise failed because ' + JSON.stringify(err)); 171 }); 172 } 173 174 /** 175 * 弹安装成功通知 176 * 177 * @param context 实上下文 178 */ 179 async showUpgradeSuccess(versionName: string, context: common.Context): Promise<void> { 180 let request = { 181 content: { 182 contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 183 normal: { 184 title: await context.resourceManager.getString($r('app.string.install_success_message').id), 185 text: versionName 186 } 187 }, 188 wantAgent: await wantAgent.getWantAgent(this.checkWantAgentInfo), 189 id: 4, 190 slotType: notification.SlotType.SERVICE_INFORMATION 191 } 192 await notification.publish(request).then(() => { 193 this.logInfo('showUpgradeSuccess publish promise success.'); 194 }).catch((err) => { 195 this.logError('showUpgradeSuccess publish promise failed because ' + JSON.stringify(err)); 196 }); 197 } 198 199 /** 200 * 取消所有通知 201 */ 202 async cancelAll(): Promise<void> { 203 await notification.cancelAll().then(() => { 204 this.logInfo('cancelAll notification success'); 205 }); 206 } 207 208 /** 209 * info级别日志打印 210 * 211 * @param message 日志内容 212 */ 213 logInfo(message: string): void { 214 LogUtils.info('NotificationHelper', message); 215 } 216 217 /** 218 * error级别日志打印 219 * 220 * @param message 日志内容 221 */ 222 logError(message: string): void { 223 LogUtils.error('NotificationHelper', message); 224 } 225}