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"; 17import common from "../data/commonData"; 18import HiLog from "../utils/HiLog"; 19import conversationService from "./ConversationService"; 20import LooseObject from "../data/LooseObject"; 21import messageService from "../service/ConversationListService"; 22 23var notificationManager = globalThis.requireNapi("notificationManager"); 24const label = "notification_"; 25const TAG = "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 if(Number(actionData.unreadTotal) >= 0){ 56 HiLog.i(TAG, `unreadTotal is: ${Number(actionData.unreadTotal)}`) 57 notificationManager.setBadgeNumber(Number(actionData.unreadTotal)); 58 } 59 Notification.publish(notificationRequest); 60 HiLog.i(TAG, "sendNotify finished"); 61 }); 62 } 63 64 /** 65 * Create a wanted message to be sent. 66 * 67 * @param agentInfo 68 * @callback callback 69 */ 70 getWantAgent(agentInfo, callback) { 71 WantAgent.getWantAgent(agentInfo).then(data1 => { 72 callback(data1); 73 }); 74 } 75 76 /** 77 * Constructing distributed startup parameters 78 * 79 * @param actionData 80 */ 81 buildWantAgentInfo(actionData): any { 82 let parameters = { 83 pageFlag: "conversation", 84 contactObjects: actionData.contactObjects 85 }; 86 let wantAgentInfo = { 87 wants: [ 88 { 89 deviceId: "", 90 bundleName: common.string.BUNDLE_NAME, 91 abilityName: common.string.ABILITY_NAME, 92 action: "mms.event.notification", 93 entities: [], 94 type: "MIMETYPE", 95 uri: "", 96 } 97 ], 98 operationType: WantAgent.OperationType.START_ABILITY, 99 requestCode: actionData.msgId, 100 wantAgentFlags: [WantAgent.WantAgentFlags.CONSTANT_FLAG], 101 extraInfo: parameters 102 }; 103 return wantAgentInfo; 104 } 105 106 /** 107 * Building notification parameters 108 * 109 * @param actionData 110 */ 111 buildNotificationRequest(actionData): any { 112 let message = actionData.message; 113 let notificationRequest = { 114 content: { 115 contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 116 normal: { 117 title: message.title, 118 text: message.text 119 }, 120 }, 121 wantAgent: '', 122 slotType: Notification.SlotType.OTHER_TYPES, 123 deliveryTime: new Date().getTime(), 124 groupName: "MMS" 125 }; 126 return notificationRequest; 127 } 128 129 cancelMessageNotify(actionData, callback) { 130 conversationService.queryMessageDetail(actionData, res => { 131 if (res.code == common.int.FAILURE || res.response.length == 0) { 132 callback(common.int.FAILURE); 133 } 134 let count = 0; 135 for (let item of res.response) { 136 this.cancelNotify(parseInt(item.id), result => { 137 count++; 138 if (count == res.response.length) { 139 callback(common.int.SUCCESS); 140 } 141 }); 142 } 143 }); 144 } 145 146 cancelNotify(msgId, callback) { 147 Notification.cancel(msgId, label + msgId, (err, data) => { 148 if (err) { 149 HiLog.w(TAG, "cancelNotify, error: " + JSON.stringify(err.message)); 150 callback(common.int.FAILURE); 151 } 152 callback(common.int.SUCCESS); 153 }); 154 let params: LooseObject = {}; 155 messageService.statisticalData(params, function (res) { 156 if (res.code == common.int.SUCCESS) { 157 notificationManager.setBadgeNumber(Number(res.response.totalListCount)); 158 HiLog.i(TAG, "sendNotification, callback actionData"); 159 } 160 }); 161 } 162 163 cancelAllNotify() { 164 let promise = Notification.cancelAll(); 165 promise.then((ret) => { 166 }).catch((err) => { 167 HiLog.e(TAG, "cancelAllNotify, error: " + JSON.stringify(err.message)); 168 }); 169 } 170};