From 1e683f167d1680408c363645a221ad88ded78781 Mon Sep 17 00:00:00 2001 From: chenyuyan Date: Tue, 27 Sep 2022 11:48:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2UIservice=E5=BC=B9=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyuyan Change-Id: I8dc20a721d68b98fb719b618678999fb9d7ee4fe --- frameworks/ans/src/notification_helper.cpp | 5 ++ frameworks/core/BUILD.gn | 1 + .../core/include/ans_callback_interface.h | 40 ++++++++++ frameworks/core/include/ans_callback_stub.h | 35 ++++++++ .../core/include/ans_manager_interface.h | 9 +++ frameworks/core/include/ans_manager_proxy.h | 9 +++ frameworks/core/include/ans_manager_stub.h | 9 +++ frameworks/core/include/ans_notification.h | 10 +++ frameworks/core/src/ans_callback_stub.cpp | 52 ++++++++++++ frameworks/core/src/ans_manager_proxy.cpp | 11 +++ frameworks/core/src/ans_manager_stub.cpp | 16 +++- frameworks/core/src/ans_notification.cpp | 10 +++ frameworks/js/napi/BUILD.gn | 1 + .../js/napi/include/enable_notification.h | 45 ++++++++--- .../js/napi/src/enable_notification.cpp | 79 +++++++++++++++++-- frameworks/js/napi/src/manager/BUILD.gn | 1 + interfaces/inner_api/notification_helper.h | 10 +++ .../include/advanced_notification_service.h | 9 +++ .../ans/src/advanced_notification_service.cpp | 54 ++++--------- 19 files changed, 348 insertions(+), 58 deletions(-) create mode 100644 frameworks/core/include/ans_callback_interface.h create mode 100644 frameworks/core/include/ans_callback_stub.h create mode 100644 frameworks/core/src/ans_callback_stub.cpp diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 823d8a89c..ec6963f02 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -159,6 +159,11 @@ ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId) return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId); } +ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId, bool &popFlag) +{ + return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId, popFlag); +} + ErrCode NotificationHelper::AreNotificationsSuspended(bool &suspended) { return DelayedSingleton::GetInstance()->AreNotificationsSuspended(suspended); diff --git a/frameworks/core/BUILD.gn b/frameworks/core/BUILD.gn index 4f9ef97a8..35b830b1b 100644 --- a/frameworks/core/BUILD.gn +++ b/frameworks/core/BUILD.gn @@ -32,6 +32,7 @@ ohos_shared_library("ans_core") { sources = [ "${core_path}/common/src/ans_log_wrapper.cpp", "${core_path}/common/src/ans_watchdog.cpp", + "${core_path}/src/ans_callback_stub.cpp", "${core_path}/src/ans_image_util.cpp", "${core_path}/src/ans_manager_death_recipient.cpp", "${core_path}/src/ans_manager_proxy.cpp", diff --git a/frameworks/core/include/ans_callback_interface.h b/frameworks/core/include/ans_callback_interface.h new file mode 100644 index 000000000..6670acfc8 --- /dev/null +++ b/frameworks/core/include/ans_callback_interface.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_INTERFACE_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_INTERFACE_H + +#include "iremote_broker.h" + +namespace OHOS { +namespace Notification { +class AnsCallbackInterface : public IRemoteBroker { +public: + AnsCallbackInterface() = default; + virtual ~AnsCallbackInterface() = default; + DISALLOW_COPY_AND_MOVE(AnsCallbackInterface); + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Notification.AnsCallbackInterface"); + + virtual bool OnEnableNotification(bool isAllow) = 0; + +protected: + enum InterfaceCode : uint32_t { + ON_ENABLE_NOTIFICATION_CALLBACK = 0, + }; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_INTERFACE_H diff --git a/frameworks/core/include/ans_callback_stub.h b/frameworks/core/include/ans_callback_stub.h new file mode 100644 index 000000000..c7e3c46cd --- /dev/null +++ b/frameworks/core/include/ans_callback_stub.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_STUB_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_STUB_H + +#include "ans_callback_interface.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace Notification { +class AnsCallbackStub : public IRemoteStub { +public: + AnsCallbackStub() = default; + virtual ~AnsCallbackStub() override = default; + DISALLOW_COPY_AND_MOVE(AnsCallbackStub); + + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_STUB_H diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 472582a1c..3b40c55fe 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -347,6 +347,15 @@ public: */ virtual ErrCode RequestEnableNotification(const std::string &deviceId) = 0; + /** + * @brief Allow notifications to be sent based on the deviceId. + * + * @param deviceId Indicates the device Id. + * @param popFlag Indicates the flag that allows dialog to be popped. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) = 0; + /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 0cc02ac0d..89c5129ff 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -335,6 +335,15 @@ public: */ ErrCode RequestEnableNotification(const std::string &deviceId) override; + /** + * @brief Allow notifications to be sent based on the deviceId. + * + * @param deviceId Indicates the device Id. + * @param popFlag Indicates the flag that allows dialog to be popped. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) override; + /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index a20c40d2e..55207f7d3 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -351,6 +351,15 @@ public: */ virtual ErrCode RequestEnableNotification(const std::string &deviceId) override; + /** + * @brief Allow notifications to be sent based on the deviceId. + * + * @param deviceId Indicates the device Id. + * @param popFlag Indicates the flag that allows dialog to be popped. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) override; + /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. * diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 6c5e718d0..0afd8a3b4 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -285,6 +285,16 @@ public: */ ErrCode RequestEnableNotification(std::string &deviceId); + /** + * @brief Allows the current application to publish notifications on a specified device. + * + * @param deviceId Indicates the ID of the device running the application. At present, this parameter can + * only be null or an empty string, indicating the current device. + * @param popFlag Indicates the flag that allows dialog to be popped. + * @return Returns set notifications enabled for default bundle result. + */ + ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); + /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish * notifications. diff --git a/frameworks/core/src/ans_callback_stub.cpp b/frameworks/core/src/ans_callback_stub.cpp new file mode 100644 index 000000000..ecf76a676 --- /dev/null +++ b/frameworks/core/src/ans_callback_stub.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ans_callback_stub.h" + +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +int32_t AnsCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags) +{ + ANS_LOGI("enter"); + std::u16string descriptor = data.ReadInterfaceToken(); + if (descriptor != AnsCallbackStub::GetDescriptor()) { + ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!"); + return OBJECT_NULL; + } + + if (InterfaceCode::ON_ENABLE_NOTIFICATION_CALLBACK == code) { + bool result = false; + if (!data.ReadBool(result)) { + ANS_LOGE("read notification enabled result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + ANS_LOGD("result = %{public}d", result); + OnEnableNotification(result); + } else { + ANS_LOGW("[OnRemoteRequest] fail: unknown code! %{public}d", code); + return IRemoteStub::OnRemoteRequest(code, data, reply, flags); + } + return NO_ERROR; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 4a0e2b96f..42b8e66f5 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -1105,6 +1105,13 @@ ErrCode AnsManagerProxy::UpdateSlots( ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) { + ANS_LOGE("[RequestEnableNotification] fail: deprecated."); + return ERR_ANS_NOT_ALLOWED; +} + +ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId, bool &popFlag) +{ + ANS_LOGI("enter"); MessageParcel data; if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { ANS_LOGE("[RequestEnableNotification] fail: write interface token failed."); @@ -1129,6 +1136,10 @@ ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) return ERR_ANS_PARCELABLE_FAILED; } + if (!reply.ReadBool(popFlag)) { + ANS_LOGE("[RequestEnableNotification] fail: read popFlag failed."); + return ERR_ANS_PARCELABLE_FAILED; + } return result; } diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 63bcb0b6a..e72995095 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -936,17 +936,23 @@ ErrCode AnsManagerStub::HandleUpdateSlots(MessageParcel &data, MessageParcel &re ErrCode AnsManagerStub::HandleRequestEnableNotification(MessageParcel &data, MessageParcel &reply) { + ANS_LOGI("enter"); std::string deviceId; + bool popFlag = false; if (!data.ReadString(deviceId)) { ANS_LOGE("[HandleRequestEnableNotification] fail: read deviceId failed."); return ERR_ANS_PARCELABLE_FAILED; } - - ErrCode result = RequestEnableNotification(deviceId); + ErrCode result = RequestEnableNotification(deviceId, popFlag); if (!reply.WriteInt32(result)) { ANS_LOGE("[HandleRequestEnableNotification] fail: write result failed, ErrCode=%{public}d", result); return ERR_ANS_PARCELABLE_FAILED; } + if (!reply.WriteBool(popFlag)) { + ANS_LOGE("[HandleRequestEnableNotification] fail: write popFlag failed, ErrCode=%{public}d", popFlag); + return ERR_ANS_PARCELABLE_FAILED; + } + ANS_LOGD("Write popFlag into reply. popFlag = %{public}d", popFlag); return ERR_OK; } @@ -1980,6 +1986,12 @@ ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId) return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId, bool &popFlag) +{ + ANS_LOGE("AnsManagerStub::RequestEnableNotification called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::SetNotificationsEnabledForBundle(const std::string &bundle, bool enabled) { ANS_LOGE("AnsManagerStub::SetNotificationsEnabledForBundle called!"); diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index a78070892..2860aa3db 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -383,6 +383,16 @@ ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId) return ansManagerProxy_->RequestEnableNotification(deviceId); } +ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId, bool &popFlag) +{ + ANS_LOGD("enter"); + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ansManagerProxy_->RequestEnableNotification(deviceId, popFlag); +} + ErrCode AnsNotification::AreNotificationsSuspended(bool &suspended) { if (!GetAnsManagerProxy()) { diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index c3e7b5496..e1908c412 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -79,6 +79,7 @@ ohos_shared_library("notification") { external_deps = [ "ability_base:want", "ability_base:zuri", + "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", "ability_runtime:wantagent_innerkits", "c_utils:utils", diff --git a/frameworks/js/napi/include/enable_notification.h b/frameworks/js/napi/include/enable_notification.h index f8656ed15..51b0faf9f 100644 --- a/frameworks/js/napi/include/enable_notification.h +++ b/frameworks/js/napi/include/enable_notification.h @@ -15,48 +15,69 @@ #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H +#include + +#include "ans_callback_stub.h" #include "common.h" namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +using RequestEnableTask = std::function; -struct EnableParams { +struct IsEnableParams { NotificationBundleOption option; - bool enable = false; napi_ref callback = nullptr; + bool hasBundleOption = false; + int32_t userId = SUBSCRIBE_USER_INIT; + bool hasUserId = false; + bool allowToPop = false; }; -struct AsyncCallbackInfoEnable { +struct AsyncCallbackInfoIsEnable { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - EnableParams params; + IsEnableParams params; CallbackPromiseInfo info; + bool allowed = false; }; -struct IsEnableParams { +class CallbackStubImpl : public AnsCallbackStub { +public: + explicit CallbackStubImpl(AsyncCallbackInfoIsEnable *task) : task_(task) {}; + ~CallbackStubImpl() override = default; + bool OnEnableNotification(bool isAllow) override; + +private: + AsyncCallbackInfoIsEnable *task_; +}; + +static std::mutex callbackMutex_; +static sptr callbackStubImpl_; + +struct EnableParams { NotificationBundleOption option; + bool enable = false; napi_ref callback = nullptr; - bool hasBundleOption = false; - int32_t userId = SUBSCRIBE_USER_INIT; - bool hasUserId = false; }; -struct AsyncCallbackInfoIsEnable { +struct AsyncCallbackInfoEnable { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - IsEnableParams params; + EnableParams params; CallbackPromiseInfo info; - bool allowed = false; }; napi_value EnableNotification(napi_env env, napi_callback_info info); napi_value IsNotificationEnabled(napi_env env, napi_callback_info info); napi_value IsNotificationEnabledSelf(napi_env env, napi_callback_info info); napi_value RequestEnableNotification(napi_env env, napi_callback_info info); - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnableParams ¶ms); napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, IsEnableParams ¶ms); + +bool CreateCallbackStubImpl(AsyncCallbackInfoIsEnable *callbackInfo); +void ResetCallbackStubImpl(); +void StartNotificationDialog(AsyncCallbackInfoIsEnable *callbackInfo); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H \ No newline at end of file diff --git a/frameworks/js/napi/src/enable_notification.cpp b/frameworks/js/napi/src/enable_notification.cpp index 9da7ec28f..769140deb 100644 --- a/frameworks/js/napi/src/enable_notification.cpp +++ b/frameworks/js/napi/src/enable_notification.cpp @@ -14,6 +14,7 @@ */ #include "enable_notification.h" +#include "ability_manager_client.h" namespace OHOS { namespace NotificationNapi { @@ -324,14 +325,25 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) nullptr, resourceName, [](napi_env env, void *data) { - ANS_LOGI("RequestEnableNotification napi_create_async_work start"); AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); - std::string deviceId {""}; - asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId); - ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + bool popFlag = false; + asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId, popFlag); + asynccallbackinfo->params.allowToPop = popFlag; + ANS_LOGI("errorCode = %{public}d, allowToPop = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->params.allowToPop); + if (asynccallbackinfo->info.errorCode == ERR_OK && asynccallbackinfo->params.allowToPop) { + ANS_LOGI("Begin to start notification dialog"); + auto *callbackInfo = static_cast(data); + StartNotificationDialog(callbackInfo); + } + }, + [](napi_env env, napi_status status, void *data) { + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + if (!(asynccallbackinfo->info.errorCode == ERR_OK && asynccallbackinfo->params.allowToPop)) { + AsyncCompleteCallbackIsNotificationEnabled(env, status, data); + } }, - AsyncCompleteCallbackIsNotificationEnabled, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); @@ -343,5 +355,62 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) return promise; } } + +void StartNotificationDialog(AsyncCallbackInfoIsEnable *callbackInfo) +{ + ANS_LOGD("%{public}s, Begin Calling StartNotificationDialog.", __func__); + if (CreateCallbackStubImpl(callbackInfo)) { + sptr token; + auto result = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token); + if (result == ERR_OK) { + AAFwk::Want want; + want.SetElementName("com.example.dialogsPopped", "DialogPopped"); + want.SetParam("callbackStubImpl_", callbackStubImpl_); + want.SetParam("tokenId", token); + ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token, -1); + ANS_LOGD("%{public}s, End Calling StartNotificationDialog. ret=%{public}d", __func__, err); + } else { + ANS_LOGE("%{public}s, show notification dialog failed", __func__); + ResetCallbackStubImpl(); + } + } +} + +bool CreateCallbackStubImpl(AsyncCallbackInfoIsEnable *callbackInfo) +{ + std::lock_guard lock(callbackMutex_); + if (callbackStubImpl_ != nullptr) { + return false; + } + callbackStubImpl_ = new (std::nothrow) CallbackStubImpl(callbackInfo); + return true; +} + +void ResetCallbackStubImpl() +{ + std::lock_guard lock(callbackMutex_); + callbackStubImpl_ = nullptr; +} + +bool CallbackStubImpl::OnEnableNotification(bool isAllow) +{ + ANS_LOGD("isAllow: %{public}d", isAllow); + if (task_) { + task_->allowed = isAllow; + napi_value resourceName = nullptr; + napi_create_string_latin1(task_->env, "RequestEnableNotification", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(task_->env, + nullptr, + resourceName, + [](napi_env env, void *data) {}, + AsyncCompleteCallbackIsNotificationEnabled, + (void *)task_, + &task_->asyncWork); + NAPI_CALL_BASE(task_->env, napi_queue_async_work(task_->env, task_->asyncWork), false); + return true; + } + return false; +} + } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/manager/BUILD.gn b/frameworks/js/napi/src/manager/BUILD.gn index 8d04207cd..24691f07e 100644 --- a/frameworks/js/napi/src/manager/BUILD.gn +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -64,6 +64,7 @@ ohos_shared_library("notificationmanager") { external_deps = [ "ability_base:want", "ability_base:zuri", + "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", "ability_runtime:wantagent_innerkits", "c_utils:utils", diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 2c7855390..c982bac66 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -287,6 +287,16 @@ public: */ static ErrCode RequestEnableNotification(std::string &deviceId); + /** + * @brief Allow the current application to publish notifications on a specified device. + * + * @param deviceId Indicates the ID of the device running the application. At present, this parameter can + * only be null or an empty string, indicating the current device. + * @param popFlag Indicates the flag that allows dialog to be popped. + * @return Returns set notifications enabled for default bundle result. + */ + static ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); + /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish * notifications. diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 0b82dffee..27f7a8663 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -347,6 +347,15 @@ public: */ ErrCode RequestEnableNotification(const std::string &deviceId) override; + /** + * @brief Allow notifications to be sent based on the deviceId. + * + * @param deviceId Indicates the device Id. + * @param popFlag Indicates the flag that allows dialog to be popped. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) override; + /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. * diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 40eba448f..01aca386e 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1435,66 +1435,42 @@ ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( } ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId) +{ + ANS_LOGE("[RequestEnableNotification] fail: deprecated."); + return ERR_ANS_NOT_ALLOWED; +} + +ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, bool &popFlag) { ANS_LOGD("%{public}s", __FUNCTION__); ErrCode result = ERR_OK; sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { + ANS_LOGD("bundleOption == nullptr"); return ERR_ANS_INVALID_BUNDLE; } + // To get the permission bool allowedNotify = false; result = IsAllowedNotifySelf(bundleOption, allowedNotify); + ANS_LOGI("result = %{public}d, allowedNotify = %{public}d", result, allowedNotify); if (result != ERR_OK || allowedNotify) { ANS_LOGD("Already granted permission"); + popFlag = false; return result; } - + + // Check to see if it has been popover before bool hasPopped = false; result = GetHasPoppedDialog(bundleOption, hasPopped); if (result != ERR_OK || hasPopped) { ANS_LOGD("Already shown dialog"); + popFlag = false; return result; } - - int32_t positionX; - int32_t positionY; - int32_t width; - int32_t height; - bool wideScreen; - GetDisplayPosition(positionX, positionY, width, height, wideScreen); - - const std::string params = std::string("{\"requestNotification\":\"Allowed to send notification?\", ") + - std::string("\"allowButton\":\"Allow\", \"cancelButton\":\"Cancel\", \"uid\":\"") + - std::to_string(bundleOption->GetUid()) + std::string("\"}"); - Ace::UIServiceMgrClient::GetInstance()->ShowDialog( - "notification_dialog", - params, - Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, - positionX, - positionY, - width, - height, - [this](int32_t id, const std::string& event, const std::string& params) { - ANS_LOGD("Dialog callback: %{public}s, %{public}s", event.c_str(), params.c_str()); - int32_t uid = std::stoi(params, nullptr); - std::string bundle; - std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); - if (bundleManager != nullptr) { - bundle = bundleManager->GetBundleNameByUid(uid); - } - sptr bundleOption = new NotificationBundleOption(bundle, uid); - if (event == "EVENT_ALLOW") { - this->SetNotificationsEnabledForSpecialBundle("", bundleOption, true); - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); - } else { - this->SetNotificationsEnabledForSpecialBundle("", bundleOption, false); - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); - } - this->SetHasPoppedDialog(bundleOption, true); - }); - + SetHasPoppedDialog(bundleOption, true); + popFlag = true; return result; } -- Gitee