From a7be40f03e291fd5792811ec2f3bee31591d0b50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=A5=E9=9B=B7?= <656805005@qq.com> Date: Thu, 31 Jul 2025 16:48:56 +0800 Subject: [PATCH] =?UTF-8?q?getNotificationSetting=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李春雷 <656805005@qq.com> --- frameworks/ets/ani/include/manager/ani_slot.h | 1 + frameworks/ets/ani/include/sts_slot.h | 1 + .../ets/ani/src/manager/ani_manager.cpp | 1 + frameworks/ets/ani/src/manager/ani_slot.cpp | 21 ++++++++++++++ frameworks/ets/ani/src/sts_slot.cpp | 29 +++++++++++++++++++ .../ets/ets/@ohos.notificationManager.ets | 28 +++++++++++++++++- 6 files changed, 80 insertions(+), 1 deletion(-) diff --git a/frameworks/ets/ani/include/manager/ani_slot.h b/frameworks/ets/ani/include/manager/ani_slot.h index 7fc0c6053..596786a3d 100644 --- a/frameworks/ets/ani/include/manager/ani_slot.h +++ b/frameworks/ets/ani/include/manager/ani_slot.h @@ -37,6 +37,7 @@ void AniRemoveSlot(ani_env *env, ani_enum_item enumObj); void AniRemoveAllSlots(ani_env *env); void AniSetSlotByBundle(ani_env *env, ani_object bundleOptionObj, ani_object slotObj); ani_long AniGetSlotNumByBundle(ani_env *env, ani_object bundleOption); +ani_object AniGetNotificationSetting(ani_env *env); } // namespace NotificationManagerSts } // namespace OHOS #endif diff --git a/frameworks/ets/ani/include/sts_slot.h b/frameworks/ets/ani/include/sts_slot.h index 12a8eeef0..87ee56b9f 100644 --- a/frameworks/ets/ani/include/sts_slot.h +++ b/frameworks/ets/ani/include/sts_slot.h @@ -38,6 +38,7 @@ bool ParseNotificationSlotByBasicType(ani_env *env, ani_object notificationSlotO bool UnwrapNotificationSlot(ani_env *env, ani_object notificationSlotObj, NotificationSlot &slot); bool UnwrapNotificationSlotArrayByAniObj(ani_env *env, ani_object notificationSlotArrayObj, std::vector &slots); +bool WrapGetNotificationSetting(ani_env *env, uint32_t slotFlags, ani_object &outAniObj); } // namespace NotificationSts } // OHOS #endif diff --git a/frameworks/ets/ani/src/manager/ani_manager.cpp b/frameworks/ets/ani/src/manager/ani_manager.cpp index 7b024fa20..9dba1b422 100644 --- a/frameworks/ets/ani/src/manager/ani_manager.cpp +++ b/frameworks/ets/ani/src/manager/ani_manager.cpp @@ -95,6 +95,7 @@ static std::array kitManagerFunctions = { ani_native_function {"nativeRequestEnableNotification", "Lapplication/UIAbilityContext/UIAbilityContext;:Lstd/core/Promise;", reinterpret_cast(AniRequestEnableNotification)}, + ani_native_function {"nativeGetNotificationSetting", nullptr, reinterpret_cast(AniGetNotificationSetting)}, #ifdef ANS_FEATURE_BADGE_MANAGER ani_native_function {"nativeDisplayBadge", nullptr, reinterpret_cast(AniDisplayBadge)}, diff --git a/frameworks/ets/ani/src/manager/ani_slot.cpp b/frameworks/ets/ani/src/manager/ani_slot.cpp index bcd6fc42f..2c899e899 100644 --- a/frameworks/ets/ani/src/manager/ani_slot.cpp +++ b/frameworks/ets/ani/src/manager/ani_slot.cpp @@ -382,5 +382,26 @@ ani_object AniGetSlotByBundle(ani_env *env, ani_object bundleOption, ani_enum_it } return infoObj; } + +ani_object AniGetNotificationSetting(ani_env *env) +{ + ANS_LOGD("AniGetNotificationSetting enter"); + uint32_t slotFlags = 0; + int returncode = Notification::NotificationHelper::GetNotificationSettings(slotFlags); + ANS_LOGD("AniGetNotificationSetting slotFlags: %{public}d", slotFlags); + if (returncode != ERR_OK) { + int externalCode = NotificationSts::GetExternalCode(returncode); + ANS_LOGE("GetNotificationSetting -> error, errorCode: %{public}d", externalCode); + NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode)); + return nullptr; + } + ani_object infoObj; + if (!NotificationSts::WrapGetNotificationSetting(env, slotFlags, infoObj) || infoObj == nullptr) { + NotificationSts::ThrowStsErroWithMsg(env, "WrapGetNotificationSetting Failed"); + return nullptr; + } + + return infoObj; +} } } \ No newline at end of file diff --git a/frameworks/ets/ani/src/sts_slot.cpp b/frameworks/ets/ani/src/sts_slot.cpp index 0167a798b..6f249d273 100644 --- a/frameworks/ets/ani/src/sts_slot.cpp +++ b/frameworks/ets/ani/src/sts_slot.cpp @@ -142,6 +142,35 @@ bool WrapNotificationSlotByInt(ani_env *env, sptrObject_SetPropertyByName_Boolean( + outAniObj, "vibrationEnabled", BoolToAniBoolean(vibrationEnabled)))) { + ANS_LOGE("Set vibrationEnabled fail, status %{public}d", status); + return false; + } + if (ANI_OK != (status = env->Object_SetPropertyByName_Boolean( + outAniObj, "soundEnabled", BoolToAniBoolean(soundEnabled)))) { + ANS_LOGE("Set soundEnabled fail, status %{public}d", status); + return false; + } + return true; +} + bool WrapNotificationSlot(ani_env *env, sptr slot, ani_object &outAniObj) { ANS_LOGD("WrapNotificationSlot call"); diff --git a/frameworks/ets/ets/@ohos.notificationManager.ets b/frameworks/ets/ets/@ohos.notificationManager.ets index 3bd9e4e93..33516006d 100644 --- a/frameworks/ets/ets/@ohos.notificationManager.ets +++ b/frameworks/ets/ets/@ohos.notificationManager.ets @@ -95,6 +95,16 @@ export default namespace notificationManager { end: Date; } + export interface NotificationSetting { + vibrationEnabled: boolean; + soundEnabled: boolean; + } + + class NotificationSettingInner implements NotificationSetting { + vibrationEnabled: boolean = false; + soundEnabled: boolean = false; + } + export interface DoNotDisturbProfile { id: long; name: string; @@ -222,7 +232,8 @@ export default namespace notificationManager { type: 'checkNotification', callback: CallbackForCheckInfo, checkRequest?: NotificationCheckRequest): int; export native function nativeOff( type: 'checkNotification', callback?: CallbackForCheckInfo): int; - + export native function nativeGetNotificationSetting(): NotificationSetting; + function isInvalidParameter(doNotDisturbDate: DoNotDisturbDate): BusinessError { let error: BusinessError = successCallbackError @@ -2609,4 +2620,19 @@ export default namespace notificationManager { callback(err, undefined); }) } + + export function getNotificationSetting(): Promise { + let pPromise = new Promise( + (resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): NotificationSetting => + { return nativeGetNotificationSetting(); }); + p.then((data: NullishType): void => { + let ret : NotificationSetting = data as NotificationSetting; + resolve(ret); + }, (error: Error): void => { + reject(error); + }); + }); + return pPromise; + } } -- Gitee