// Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved // Use of this source code is governed by a MIT license that can be // found in the LICENSE file. import { RNOHLogger, TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts'; import { TM } from './generated/ts' import notificationManager from '@ohos.notificationManager'; import { ConvertUtils } from './ConvertUtils'; import { BusinessError } from '@kit.BasicServicesKit'; const TAG = "PushNotificationTurboModule:" type PushNotificationPermissions = TM.PushNotificationTurboModule.PushNotificationPermissions export class PushNotificationTurboModule extends TurboModule implements TM.PushNotificationTurboModule.Spec { utils: ConvertUtils = new ConvertUtils(); private Logger: RNOHLogger = this.ctx.logger.clone("PushNotificationTurboModule"); private arr: number[] = [] constructor(ctx: TurboModuleContext) { super(ctx) } public checkPermissions(callback: (permissions: TM.PushNotificationTurboModule.PushNotificationPermissions) => void) { let isNotificationEnabledCallback = (err: BusinessError, data: boolean): void => { if (err) { this.Logger.error(`${TAG} isNotificationEnabled failed, code is ${err.code}, message is ${err.message}`); const res = { alert: 0 } as ESObject callback(res) } else { this.Logger.info(`${TAG} isNotificationEnabled success, data is ${JSON.stringify(data)}`); const bool = data ? 1 : 0 const res = { alert: bool } as ESObject callback(res) } } notificationManager.isNotificationEnabled(isNotificationEnabledCallback); } public requestPermissions(permissions: PushNotificationPermissions): Promise { if (permissions.alert) { return new Promise((resolve, reject) => { notificationManager .requestEnableNotification(this.ctx.uiAbilityContext) .then(() => { this.Logger.info(`${TAG} [ANS] requestEnableNotification success`); // 假设权限请求成功后返回传入的权限对象 const res = { alert: 1 } as ESObject resolve(res); }) .catch((err: BusinessError) => { if (err.code === 1600004) { this.Logger.error(`${TAG} [ANS] requestEnableNotification refused, code is ${err.code}, message is ${err.message}`); } else { this.Logger.error(`${TAG} [ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`); } // 返回错误信息 resolve({ alert: 0 } as ESObject); }); }); } else { return new Promise(async (resolve, reject) => { try { const res = await notificationManager.isNotificationEnabled() const alert = res ? 1 : 0 resolve({ alert } as ESObject) } catch (error) { reject({ err: error }) } }) } } presentLocalNotification(request: TM.PushNotificationTurboModule.NotificationRequest): void { this.utils.convertNotification(request).then((data) => { if (data) { notificationManager.publish(data, (err) => { if (err) { this.Logger.error(`publish failed, code is ${err.code}, message is ${err.message}`); } else { this.Logger.info("publish success"); } }); } else { this.Logger.error("publish failed"); } }) } scheduleLocalNotification(request: TM.PushNotificationTurboModule.NotificationRequest): void { this.utils.convertNotification(request).then((data) => { if (data) { if (data.deliveryTime && typeof data.deliveryTime === 'number'){ let timer = setTimeout(()=>{ notificationManager.publish(data, (err) => { if (err) { this.Logger.error(`publish failed, code is ${err.code}, message is ${err.message}`); } else { this.Logger.info("publish success"); } }); }, data.deliveryTime - Date.now()) this.arr.push(timer) }else { return {err:'fireDate is null || not type Number'} } } else { this.Logger.error("publish failed"); } }, (e) => { return { err: e } }) } cancelAllLocalNotifications(): void { this.arr.forEach((t)=>{ clearTimeout(t) }) } addNotificationRequest(request: TM.PushNotificationTurboModule.NotificationRequest) { this.utils.convertNotification(request).then((data) => { if (data) { notificationManager.publish(data, (err) => { if (err) { this.Logger.error(`publish failed, code is ${err.code}, message is ${err.message}`); } else { this.Logger.info("publish success"); } }); } else { this.Logger.error("publish failed"); } }) } removeDeliveredNotifications(identifiers: Array) { for (let i = 0; i < identifiers.length; i++) { let data = identifiers[i]; let id = this.utils.parsePositiveInteger(data); notificationManager.cancel(id, (err) => { if (err) { this.Logger.error(`cancel failed, code is ${err.code}, message is ${err.message}`); } else { this.Logger.info("cancel success"); } }) } } removeAllDeliveredNotifications() { notificationManager.cancelAll((err) => { if (err) { this.Logger.error(`cancelAll failed, code is ${err.code}, message is ${err.message}`); } else { this.Logger.info("cancelAll success"); } }); } setApplicationIconBadgeNumber(badgeNumber: number) { notificationManager.setBadgeNumber(badgeNumber, (err) => { if (err) { this.Logger.error(`setBadge fail: ${JSON.stringify(err)}`); } else { this.Logger.info("setBadge success"); } }) } getDeliveredNotifications(callback: (result?: Array) => void) { notificationManager.getActiveNotifications().then((data) => { let newArray = this.utils.transformArray(data) callback(newArray); }) } }