1/** 2 * Copyright (c) 2022 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 */ 15import WantAgent from '@ohos.app.ability.wantAgent'; 16import Notification from '@ohos.notificationManager'; 17 18import common from '../data/commonData'; 19import HiLog from '../utils/HiLog'; 20import ConversationService from './ConversationService'; 21import ConversationListService from '../service/ConversationListService'; 22 23var notificationManager = globalThis.requireNapi('notificationManager'); 24const label = 'notification_'; 25const TAG: string = 'NotificationService'; 26 27export default class NotificationService { 28 private static sInstance: NotificationService; 29 30 static getInstance() { 31 if (NotificationService.sInstance == null) { 32 NotificationService.sInstance = new NotificationService(); 33 } 34 return NotificationService.sInstance; 35 } 36 37 constructor() { 38 HiLog.i(TAG, 'new start'); 39 } 40 41 /** 42 * Send Notifications 43 * 44 * @param actionData 45 */ 46 sendNotify(actionData) { 47 // Creating Want Information 48 let wantAgentInfo = this.buildWantAgentInfo(actionData); 49 // Constructing a Send Request 50 let notificationRequest = this.buildNotificationRequest(actionData); 51 this.getWantAgent(wantAgentInfo, (data) => { 52 notificationRequest.wantAgent = data; 53 notificationRequest.id = actionData.msgId; 54 notificationRequest.label = label + actionData.msgId; 55 Notification.publish(notificationRequest); 56 HiLog.i(TAG, 'sendNotify finished'); 57 }); 58 } 59 60 /** 61 * Create a wanted message to be sent. 62 * 63 * @param agentInfo 64 * @callback callback 65 */ 66 getWantAgent(agentInfo, callback) { 67 WantAgent.getWantAgent(agentInfo).then(data1 => { 68 callback(data1); 69 }); 70 } 71 72 /** 73 * Constructing distributed startup parameters 74 * 75 * @param actionData 76 */ 77 buildWantAgentInfo(actionData): any { 78 let parameters = { 79 pageFlag: 'conversation', 80 contactObjects: actionData.contactObjects 81 }; 82 let wantAgentInfo = { 83 wants: [ 84 { 85 deviceId: '', 86 bundleName: common.string.BUNDLE_NAME, 87 abilityName: common.string.ABILITY_NAME, 88 action: 'mms.event.notification', 89 entities: [], 90 type: 'MIMETYPE', 91 uri: '', 92 } 93 ], 94 operationType: WantAgent.OperationType.START_ABILITY, 95 requestCode: actionData.msgId, 96 wantAgentFlags: [WantAgent.WantAgentFlags.CONSTANT_FLAG], 97 extraInfo: parameters 98 }; 99 return wantAgentInfo; 100 } 101 102 /** 103 * Building notification parameters 104 * 105 * @param actionData 106 */ 107 buildNotificationRequest(actionData): any { 108 let message = actionData.message; 109 let notificationRequest = { 110 content: { 111 contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 112 normal: { 113 title: message.title, 114 text: message.text 115 }, 116 }, 117 wantAgent: undefined, 118 slotType: Notification.SlotType.OTHER_TYPES, 119 deliveryTime: new Date().getTime(), 120 groupName: 'MMS' 121 }; 122 return notificationRequest; 123 } 124 125 cancelMessageNotify(actionData, callback?) { 126 ConversationService.getInstance().queryMessageDetail(actionData, res => { 127 if (res.code == common.int.FAILURE || res.response.length == 0) { 128 if (callback != null) { 129 callback(common.int.FAILURE); 130 } 131 return; 132 } 133 let count = 0; 134 for (let item of res.response) { 135 this.cancelNotify(Number(item.id), result => { 136 count++; 137 if (count == res.response.length && callback != null) { 138 callback(common.int.SUCCESS); 139 } 140 }); 141 } 142 }, null); 143 } 144 145 cancelNotify(msgId, callback) { 146 Notification.cancel(msgId, label + msgId, (err, data) => { 147 if (err) { 148 HiLog.w(TAG, 'cancelNotify, error: ' + JSON.stringify(err.message)); 149 callback(common.int.FAILURE); 150 } 151 callback(common.int.SUCCESS); 152 }); 153 } 154 155 cancelAllNotify() { 156 let promise = Notification.cancelAll(); 157 promise.then((ret) => { 158 }).catch((err) => { 159 HiLog.e(TAG, 'cancelAllNotify, error: ' + JSON.stringify(err.message)); 160 }); 161 } 162 163 public setBadgeNumber(unreadTotal: number): void { 164 notificationManager.setBadgeNumber(unreadTotal); 165 } 166 167 public updateBadgeNumber(context?): void { 168 ConversationListService.getInstance().statisticalData(res => { 169 if (res.code == common.int.SUCCESS) { 170 this.setBadgeNumber(Number(res.response.totalListCount)); 171 } 172 }, context); 173 } 174};