1048147e0Sopenharmony_ci/** 2048147e0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3048147e0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4048147e0Sopenharmony_ci * you may not use this file except in compliance with the License. 5048147e0Sopenharmony_ci * You may obtain a copy of the License at 6048147e0Sopenharmony_ci * 7048147e0Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8048147e0Sopenharmony_ci * 9048147e0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10048147e0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11048147e0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12048147e0Sopenharmony_ci * See the License for the specific language governing permissions and 13048147e0Sopenharmony_ci * limitations under the License. 14048147e0Sopenharmony_ci */ 15048147e0Sopenharmony_ciimport WantAgent from '@ohos.app.ability.wantAgent'; 16048147e0Sopenharmony_ciimport Notification from '@ohos.notificationManager'; 17048147e0Sopenharmony_ci 18048147e0Sopenharmony_ciimport common from '../data/commonData'; 19048147e0Sopenharmony_ciimport HiLog from '../utils/HiLog'; 20048147e0Sopenharmony_ciimport ConversationService from './ConversationService'; 21048147e0Sopenharmony_ciimport ConversationListService from '../service/ConversationListService'; 22048147e0Sopenharmony_ci 23048147e0Sopenharmony_civar notificationManager = globalThis.requireNapi('notificationManager'); 24048147e0Sopenharmony_ciconst label = 'notification_'; 25048147e0Sopenharmony_ciconst TAG: string = 'NotificationService'; 26048147e0Sopenharmony_ci 27048147e0Sopenharmony_ciexport default class NotificationService { 28048147e0Sopenharmony_ci private static sInstance: NotificationService; 29048147e0Sopenharmony_ci 30048147e0Sopenharmony_ci static getInstance() { 31048147e0Sopenharmony_ci if (NotificationService.sInstance == null) { 32048147e0Sopenharmony_ci NotificationService.sInstance = new NotificationService(); 33048147e0Sopenharmony_ci } 34048147e0Sopenharmony_ci return NotificationService.sInstance; 35048147e0Sopenharmony_ci } 36048147e0Sopenharmony_ci 37048147e0Sopenharmony_ci constructor() { 38048147e0Sopenharmony_ci HiLog.i(TAG, 'new start'); 39048147e0Sopenharmony_ci } 40048147e0Sopenharmony_ci 41048147e0Sopenharmony_ci /** 42048147e0Sopenharmony_ci * Send Notifications 43048147e0Sopenharmony_ci * 44048147e0Sopenharmony_ci * @param actionData 45048147e0Sopenharmony_ci */ 46048147e0Sopenharmony_ci sendNotify(actionData) { 47048147e0Sopenharmony_ci // Creating Want Information 48048147e0Sopenharmony_ci let wantAgentInfo = this.buildWantAgentInfo(actionData); 49048147e0Sopenharmony_ci // Constructing a Send Request 50048147e0Sopenharmony_ci let notificationRequest = this.buildNotificationRequest(actionData); 51048147e0Sopenharmony_ci this.getWantAgent(wantAgentInfo, (data) => { 52048147e0Sopenharmony_ci notificationRequest.wantAgent = data; 53048147e0Sopenharmony_ci notificationRequest.id = actionData.msgId; 54048147e0Sopenharmony_ci notificationRequest.label = label + actionData.msgId; 55048147e0Sopenharmony_ci Notification.publish(notificationRequest); 56048147e0Sopenharmony_ci HiLog.i(TAG, 'sendNotify finished'); 57048147e0Sopenharmony_ci }); 58048147e0Sopenharmony_ci } 59048147e0Sopenharmony_ci 60048147e0Sopenharmony_ci /** 61048147e0Sopenharmony_ci * Create a wanted message to be sent. 62048147e0Sopenharmony_ci * 63048147e0Sopenharmony_ci * @param agentInfo 64048147e0Sopenharmony_ci * @callback callback 65048147e0Sopenharmony_ci */ 66048147e0Sopenharmony_ci getWantAgent(agentInfo, callback) { 67048147e0Sopenharmony_ci WantAgent.getWantAgent(agentInfo).then(data1 => { 68048147e0Sopenharmony_ci callback(data1); 69048147e0Sopenharmony_ci }); 70048147e0Sopenharmony_ci } 71048147e0Sopenharmony_ci 72048147e0Sopenharmony_ci /** 73048147e0Sopenharmony_ci * Constructing distributed startup parameters 74048147e0Sopenharmony_ci * 75048147e0Sopenharmony_ci * @param actionData 76048147e0Sopenharmony_ci */ 77048147e0Sopenharmony_ci buildWantAgentInfo(actionData): any { 78048147e0Sopenharmony_ci let parameters = { 79048147e0Sopenharmony_ci pageFlag: 'conversation', 80048147e0Sopenharmony_ci contactObjects: actionData.contactObjects 81048147e0Sopenharmony_ci }; 82048147e0Sopenharmony_ci let wantAgentInfo = { 83048147e0Sopenharmony_ci wants: [ 84048147e0Sopenharmony_ci { 85048147e0Sopenharmony_ci deviceId: '', 86048147e0Sopenharmony_ci bundleName: common.string.BUNDLE_NAME, 87048147e0Sopenharmony_ci abilityName: common.string.ABILITY_NAME, 88048147e0Sopenharmony_ci action: 'mms.event.notification', 89048147e0Sopenharmony_ci entities: [], 90048147e0Sopenharmony_ci type: 'MIMETYPE', 91048147e0Sopenharmony_ci uri: '', 92048147e0Sopenharmony_ci } 93048147e0Sopenharmony_ci ], 94048147e0Sopenharmony_ci operationType: WantAgent.OperationType.START_ABILITY, 95048147e0Sopenharmony_ci requestCode: actionData.msgId, 96048147e0Sopenharmony_ci wantAgentFlags: [WantAgent.WantAgentFlags.CONSTANT_FLAG], 97048147e0Sopenharmony_ci extraInfo: parameters 98048147e0Sopenharmony_ci }; 99048147e0Sopenharmony_ci return wantAgentInfo; 100048147e0Sopenharmony_ci } 101048147e0Sopenharmony_ci 102048147e0Sopenharmony_ci /** 103048147e0Sopenharmony_ci * Building notification parameters 104048147e0Sopenharmony_ci * 105048147e0Sopenharmony_ci * @param actionData 106048147e0Sopenharmony_ci */ 107048147e0Sopenharmony_ci buildNotificationRequest(actionData): any { 108048147e0Sopenharmony_ci let message = actionData.message; 109048147e0Sopenharmony_ci let notificationRequest = { 110048147e0Sopenharmony_ci content: { 111048147e0Sopenharmony_ci contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 112048147e0Sopenharmony_ci normal: { 113048147e0Sopenharmony_ci title: message.title, 114048147e0Sopenharmony_ci text: message.text 115048147e0Sopenharmony_ci }, 116048147e0Sopenharmony_ci }, 117048147e0Sopenharmony_ci wantAgent: undefined, 118048147e0Sopenharmony_ci slotType: Notification.SlotType.OTHER_TYPES, 119048147e0Sopenharmony_ci deliveryTime: new Date().getTime(), 120048147e0Sopenharmony_ci groupName: 'MMS' 121048147e0Sopenharmony_ci }; 122048147e0Sopenharmony_ci return notificationRequest; 123048147e0Sopenharmony_ci } 124048147e0Sopenharmony_ci 125048147e0Sopenharmony_ci cancelMessageNotify(actionData, callback?) { 126048147e0Sopenharmony_ci ConversationService.getInstance().queryMessageDetail(actionData, res => { 127048147e0Sopenharmony_ci if (res.code == common.int.FAILURE || res.response.length == 0) { 128048147e0Sopenharmony_ci if (callback != null) { 129048147e0Sopenharmony_ci callback(common.int.FAILURE); 130048147e0Sopenharmony_ci } 131048147e0Sopenharmony_ci } 132048147e0Sopenharmony_ci let count = 0; 133048147e0Sopenharmony_ci for (let item of res.response) { 134048147e0Sopenharmony_ci this.cancelNotify(Number(item.id), result => { 135048147e0Sopenharmony_ci count++; 136048147e0Sopenharmony_ci if (count == res.response.length && callback != null) { 137048147e0Sopenharmony_ci callback(common.int.SUCCESS); 138048147e0Sopenharmony_ci } 139048147e0Sopenharmony_ci }); 140048147e0Sopenharmony_ci } 141048147e0Sopenharmony_ci }, null); 142048147e0Sopenharmony_ci } 143048147e0Sopenharmony_ci 144048147e0Sopenharmony_ci cancelNotify(msgId, callback) { 145048147e0Sopenharmony_ci Notification.cancel(msgId, label + msgId, (err, data) => { 146048147e0Sopenharmony_ci if (err) { 147048147e0Sopenharmony_ci HiLog.w(TAG, 'cancelNotify, error: ' + JSON.stringify(err.message)); 148048147e0Sopenharmony_ci callback(common.int.FAILURE); 149048147e0Sopenharmony_ci } 150048147e0Sopenharmony_ci callback(common.int.SUCCESS); 151048147e0Sopenharmony_ci }); 152048147e0Sopenharmony_ci } 153048147e0Sopenharmony_ci 154048147e0Sopenharmony_ci cancelAllNotify() { 155048147e0Sopenharmony_ci let promise = Notification.cancelAll(); 156048147e0Sopenharmony_ci promise.then((ret) => { 157048147e0Sopenharmony_ci }).catch((err) => { 158048147e0Sopenharmony_ci HiLog.e(TAG, 'cancelAllNotify, error: ' + JSON.stringify(err.message)); 159048147e0Sopenharmony_ci }); 160048147e0Sopenharmony_ci } 161048147e0Sopenharmony_ci 162048147e0Sopenharmony_ci public setBadgeNumber(unreadTotal: number): void { 163048147e0Sopenharmony_ci notificationManager.setBadgeNumber(unreadTotal); 164048147e0Sopenharmony_ci } 165048147e0Sopenharmony_ci 166048147e0Sopenharmony_ci public updateBadgeNumber(context?): void { 167048147e0Sopenharmony_ci ConversationListService.getInstance().statisticalData(res => { 168048147e0Sopenharmony_ci if (res.code == common.int.SUCCESS) { 169048147e0Sopenharmony_ci this.setBadgeNumber(Number(res.response.totalListCount)); 170048147e0Sopenharmony_ci } 171048147e0Sopenharmony_ci }, context); 172048147e0Sopenharmony_ci } 173048147e0Sopenharmony_ci};