diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 823d8a89c3f34e426611fd3d2bd37a38b7832f38..1f8fb171066cc695f99f9068cf7e750a817030b2 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -154,9 +154,9 @@ ErrCode NotificationHelper::IsAllowedNotifySelf(bool &allowed) return DelayedSingleton::GetInstance()->IsAllowedNotifySelf(allowed); } -ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId) +ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId, bool &popFlag) { - return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId); + return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId, popFlag); } ErrCode NotificationHelper::AreNotificationsSuspended(bool &suspended) diff --git a/frameworks/core/BUILD.gn b/frameworks/core/BUILD.gn index 4f9ef97a8f3302dc6de7928fa616f20f9fd67c04..35b830b1b111a73287d374ea153cf260983362b1 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 0000000000000000000000000000000000000000..6670acfc8375f870aec68dc73bd5c34dc9c92f61 --- /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 0000000000000000000000000000000000000000..c7e3c46cdf483d93d3278197f41e4c980a128e5b --- /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 472582a1ca13116902cfda59d3b28ec17475ee48..a2a91826869321f8f5329ce35dd80659dbda8774 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -343,9 +343,10 @@ public: * @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) = 0; + 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 0cc02ac0d640eae3b6cda986ae83474dc819cfe9..ae24faca53b8c480f459d98df1fe1e9fd38e44b1 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -333,7 +333,7 @@ public: * @param deviceId Indicates the device Id. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RequestEnableNotification(const std::string &deviceId) override; + 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 a20c40d2eb030bfe78b5e33a340f03c83b47c8ca..a770d555391faa3ef72ac1641030f45354169bdc 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -347,9 +347,10 @@ public: * @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) override; + 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 6c5e718d01d886b90fc706b5bab32ba84077f647..b4656044de19168a651d5200619b916107df4eff 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -283,7 +283,7 @@ public: * only be null or an empty string, indicating the current device. * @return Returns set notifications enabled for default bundle result. */ - ErrCode RequestEnableNotification(std::string &deviceId); + ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish diff --git a/frameworks/core/src/ans_callback_stub.cpp b/frameworks/core/src/ans_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85cd72cadfacb8de48f323d2262835495856667d --- /dev/null +++ b/frameworks/core/src/ans_callback_stub.cpp @@ -0,0 +1,50 @@ +/* + * 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) +{ + 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 (!reply.ReadBool(result)) { + ANS_LOGE("read notification enabled result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + 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 4a0e2b96f1eebbd5e4736e1203d02dde864e52f9..a515087404beda73d5c8e007b3765a7e0fffdeb0 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -1103,8 +1103,9 @@ ErrCode AnsManagerProxy::UpdateSlots( return result; } -ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) +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."); @@ -1116,6 +1117,10 @@ ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) return ERR_ANS_PARCELABLE_FAILED; } + if (!data.WriteBool(popFlag)) { + ANS_LOGE("[RequestEnableNotification] fail: write popFlag failed"); + return ERR_ANS_PARCELABLE_FAILED; + } MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; ErrCode result = InnerTransact(REQUEST_ENABLE_NOTIFICATION, option, data, reply); @@ -1129,6 +1134,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 63bcb0b6aa5f67437d189041a75fe39e1ae14e53..4c40bbec292b839ad3710677428fc834cc5b365f 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -936,17 +936,27 @@ 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); + if (!data.ReadBool(popFlag)) { + ANS_LOGE("[HandleRequestEnableNotification] fail: read popFlag failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + 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; } @@ -1974,7 +1984,7 @@ ErrCode AnsManagerStub::UpdateSlots( return ERR_INVALID_OPERATION; } -ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId) +ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId, bool &popFlag) { ANS_LOGE("AnsManagerStub::RequestEnableNotification called!"); return ERR_INVALID_OPERATION; diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index a78070892b79deefc123ca876a19f7923a2d283d..67371739decf88f299ace83565aefb7c94df601f 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -373,14 +373,14 @@ ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed) return ansManagerProxy_->IsAllowedNotifySelf(allowed); } -ErrCode AnsNotification::RequestEnableNotification(std::string &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); + return ansManagerProxy_->RequestEnableNotification(deviceId, popFlag); } ErrCode AnsNotification::AreNotificationsSuspended(bool &suspended) diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 2c9a2b8320493448166b10d173c069830c532fc5..affb294c3a9ba130c7c24c29cbdd609df5e38c68 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -75,6 +75,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 20f46801266bc19764fd3731f805dbbb05418eb9..8f3248850a47cf91b726799b3e28995f19a894d0 100644 --- a/frameworks/js/napi/include/enable_notification.h +++ b/frameworks/js/napi/include/enable_notification.h @@ -15,16 +15,67 @@ #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 { + NotificationBundleOption option; + bool enable = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoEnable { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + EnableParams params; + CallbackPromiseInfo info; +}; + +struct IsEnableParams { + NotificationBundleOption option; + napi_ref callback = nullptr; + bool hasBundleOption = false; + int32_t userId = SUBSCRIBE_USER_INIT; + bool hasUserId = false; + bool allowToPop = false; + //allowpop 传到第二个lambda里 +}; + +struct AsyncCallbackInfoIsEnable { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + IsEnableParams params; + CallbackPromiseInfo info; + bool allowed = false; +}; + +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 mutex; +static sptr callbackStubImpl_; 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); +bool CreateCallbackStubImpl(void *data); +void ResetCallbackStubImpl(); +void StartNotificationDialog(void *data); } // 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 de7e99a4e2b61ce7fd8d74f088f870d29e4a4e3f..2f1b5ea67f127b1357e5db6adbaf745d5e9238c6 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 { @@ -21,35 +22,6 @@ const int ENABLE_NOTIFICATION_MAX_PARA = 3; const int ENABLE_NOTIFICATION_MIN_PARA = 2; const int IS_NOTIFICATION_ENABLE_MAX_PARA = 2; -struct EnableParams { - NotificationBundleOption option; - bool enable = false; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoEnable { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - EnableParams params; - CallbackPromiseInfo info; -}; - -struct IsEnableParams { - NotificationBundleOption option; - napi_ref callback = nullptr; - bool hasBundleOption = false; - int32_t userId = SUBSCRIBE_USER_INIT; - bool hasUserId = false; -}; - -struct AsyncCallbackInfoIsEnable { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - IsEnableParams params; - CallbackPromiseInfo info; - bool allowed = false; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnableParams ¶ms) { ANS_LOGI("enter"); @@ -353,14 +325,28 @@ 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); + // 在结构体里加个flag + bool popFlag = false; + asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId, popFlag); + //从RequestEnableNotification(这里面有个ipc过程)带一个数据结构出来,把窗口的样式参数都带出来,带进StartNotificationDialog + ANS_LOGD("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + asynccallbackinfo->params.allowToPop = popFlag; + ANS_LOGD("asynccallbackinfo->params.allowToPop = %{public}d", asynccallbackinfo->params.allowToPop); + if (asynccallbackinfo->info.errorCode == ERR_OK && popFlag) { + ANS_LOGI("Begin to start notification dialog"); + //这里要把窗口样式带进去,传到js侧 + StartNotificationDialog(data); + ANS_LOGI("Start notification dialog success"); + } + }, + [](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); @@ -372,5 +358,65 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) return promise; } } + +void StartNotificationDialog(void *data) +{ + ANS_LOGD("%{public}s, Begin Calling StartNotificationDialog.", __func__); + if (CreateCallbackStubImpl(data)) { + 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); + // 要把窗口样式带进want里 + // todo + //want.SetParam("uid", uid);暂时不塞 + 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(void *data) +{ + std::lock_guard lock(mutex); + if (callbackStubImpl_ != nullptr) { + return false; + } + auto *callbackInfo = static_cast(data); + callbackStubImpl_ = new CallbackStubImpl(callbackInfo); + return true; +} + +void ResetCallbackStubImpl() +{ + std::lock_guard lock(mutex); + callbackStubImpl_ = nullptr; +} + +bool CallbackStubImpl::OnEnableNotification(bool 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/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 2c78553908df5440a8b7392946c5dc2f60eafe96..3755ac23cf02deee0f46bd80323b0aede79ac124 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -285,7 +285,7 @@ public: * only be null or an empty string, indicating the current device. * @return Returns set notifications enabled for default bundle result. */ - static ErrCode RequestEnableNotification(std::string &deviceId); + static ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 0b82dffee2cc69a40987da8c5d8b38f3505245b7..958a4331aace703deefab906745494f334a0f760 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -343,9 +343,10 @@ public: * @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) override; + 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 40eba448fe52ca92891f8e30783d6be6a96ab28a..3020455604612b82e98fd800062eea94ce041803 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1434,67 +1434,39 @@ ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( return result; } -ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId) +ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, bool &popFlag)//传个flag的出参出去判断能不能弹窗,这个玩意的声明也得改 { 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", result); + ANS_LOGI("allowedNotify = %{public}d", 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); - }); - + ANS_LOGI("popFlag1 in = %{public}d", popFlag); + popFlag = true; + ANS_LOGI("popFlag2 in = %{public}d", popFlag); return result; } @@ -1555,6 +1527,7 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( new EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); ErrCode result = ERR_OK; + std::string dialogBundlename; handler_->PostSyncTask(std::bind([&]() { if (deviceId.empty()) { // Local device @@ -1569,6 +1542,11 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( } else { // Remote revice } + // TODO + dialogBundlename = "com.example.dialogsPopped"; + if (bundle->GetBundleName() == dialogBundlename) { + SetHasPoppedDialog(bundleOption, true); + } })); SendEnableNotificationHiSysEvent(bundleOption, enabled, result);