From eaee4c6ee08caee05b28dd1e81336b516fe99266 Mon Sep 17 00:00:00 2001 From: XKK Date: Wed, 28 Jun 2023 11:37:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=B9=E9=87=8F=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: XKK --- frameworks/ans/src/notification_helper.cpp | 5 + .../ans/src/notification_subscriber.cpp | 20 +- .../core/common/include/ans_const_define.h | 1 + .../core/include/ans_manager_interface.h | 2 + frameworks/core/include/ans_manager_proxy.h | 2 + frameworks/core/include/ans_manager_stub.h | 3 + frameworks/core/include/ans_notification.h | 2 + .../core/include/ans_subscriber_interface.h | 3 + .../core/include/ans_subscriber_proxy.h | 6 +- frameworks/core/include/ans_subscriber_stub.h | 6 + ..._notification_service_ipc_interface_code.h | 2 + frameworks/core/src/ans_manager_proxy.cpp | 44 ++++ frameworks/core/src/ans_manager_stub.cpp | 39 ++++ frameworks/core/src/ans_notification.cpp | 15 ++ frameworks/core/src/ans_subscriber_proxy.cpp | 62 ++++++ frameworks/core/src/ans_subscriber_stub.cpp | 64 ++++++ frameworks/js/napi/include/common.h | 2 + frameworks/js/napi/include/remove.h | 1 + frameworks/js/napi/src/common.cpp | 27 +++ frameworks/js/napi/src/remove.cpp | 18 +- .../js/napi/src/subscribe/napi_remove.cpp | 6 +- interfaces/inner_api/notification_helper.h | 2 + .../inner_api/notification_subscriber.h | 3 + .../include/advanced_notification_service.h | 4 + .../include/notification_subscriber_manager.h | 4 + .../ans/src/advanced_notification_service.cpp | 206 +++++++++++++++--- .../src/notification_subscriber_manager.cpp | 46 ++++ 27 files changed, 555 insertions(+), 40 deletions(-) diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index d208602e0..0fc8750b9 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -198,6 +198,11 @@ ErrCode NotificationHelper::RemoveAllNotifications(const NotificationBundleOptio return DelayedSingleton::GetInstance()->RemoveAllNotifications(bundleOption); } +ErrCode NotificationHelper::RemoveNotifications(const std::vector hashcodes, int32_t removeReason) +{ + return DelayedSingleton::GetInstance()->RemoveNotifications(hashcodes, removeReason); +} + ErrCode NotificationHelper::RemoveNotificationsByBundle(const NotificationBundleOption &bundleOption) { return DelayedSingleton::GetInstance()->RemoveNotificationsByBundle(bundleOption); diff --git a/frameworks/ans/src/notification_subscriber.cpp b/frameworks/ans/src/notification_subscriber.cpp index 5a01a737f..848b6fb1c 100644 --- a/frameworks/ans/src/notification_subscriber.cpp +++ b/frameworks/ans/src/notification_subscriber.cpp @@ -71,9 +71,23 @@ void NotificationSubscriber::SubscriberImpl::OnCanceled( const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - subscriber_.OnCanceled(std::make_shared(*notification), - std::make_shared(*notificationMap), - deleteReason); + if (notificationMap == nullptr) { + subscriber_.OnCanceled(std::make_shared(*notification), + std::make_shared(), deleteReason); + } else { + subscriber_.OnCanceled(std::make_shared(*notification), + std::make_shared(*notificationMap), deleteReason); + } +} + + +void NotificationSubscriber::SubscriberImpl::OnCanceledList(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + for (auto notification : notifications) { + OnCanceled(notification, notificationMap, deleteReason); + } } void NotificationSubscriber::SubscriberImpl::OnUpdated(const sptr ¬ificationMap) diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index f1654f106..82c4900ac 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -37,6 +37,7 @@ constexpr uint32_t MAX_CONVERSATIONAL_NUM = 10000; constexpr uint32_t MAX_PERMIT_MIME_TYPE_NUM = 10000; constexpr uint32_t MAX_ACTION_BUTTON_NUM = 3; constexpr int32_t MAX_PARCELABLE_VECTOR_NUM = 10000; +constexpr uint32_t MAX_CANCELED_PARCELABLE_VECTOR_NUM = 200; constexpr int32_t SUBSCRIBE_USER_INIT = -1; diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 6041aae15..ccccdcd2f 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -275,6 +275,8 @@ public: */ virtual ErrCode RemoveAllNotifications(const sptr &bundleOption) = 0; + virtual ErrCode RemoveNotifications(const std::vector &hashcodes, int32_t removeReason) = 0; + /** * @brief Remove notifications based on bundle. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 73eb6b88c..a73751a9e 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -255,6 +255,8 @@ public: */ ErrCode RemoveAllNotifications(const sptr &bundleOption) override; + ErrCode RemoveNotifications(const std::vector &hashcodes, int32_t removeReason) override; + /** * @brief Delete notification based on key. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index dcd90008d..553150a59 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -270,6 +270,8 @@ public: */ virtual ErrCode RemoveAllNotifications(const sptr &bundleOption) override; + virtual ErrCode RemoveNotifications(const std::vector &keys, int32_t removeReason) override; + /** * @brief Delete notification based on key. * @@ -709,6 +711,7 @@ private: ErrCode HandleIsNotificationPolicyAccessGranted(MessageParcel &data, MessageParcel &reply); ErrCode HandleRemoveNotification(MessageParcel &data, MessageParcel &reply); ErrCode HandleRemoveAllNotifications(MessageParcel &data, MessageParcel &reply); + ErrCode HandleRemoveNotifications(MessageParcel &data, MessageParcel &reply); ErrCode HandleDelete(MessageParcel &data, MessageParcel &reply); ErrCode HandleDeleteByBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleDeleteAll(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index f7d7af222..0794a5c4c 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -379,6 +379,8 @@ public: */ ErrCode RemoveAllNotifications(const NotificationBundleOption &bundleOption); + ErrCode RemoveNotifications(const std::vector hashcodes, int32_t removeReason); + /** * @brief Removes all removable notifications of a specified bundle. * @note Your application must have platform signature to use this method. diff --git a/frameworks/core/include/ans_subscriber_interface.h b/frameworks/core/include/ans_subscriber_interface.h index 3d3fba0c0..91e868f79 100644 --- a/frameworks/core/include/ans_subscriber_interface.h +++ b/frameworks/core/include/ans_subscriber_interface.h @@ -66,6 +66,9 @@ public: virtual void OnCanceled(const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) = 0; + virtual void OnCanceledList(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) = 0; + /** * @brief The callback function on the notifications updated. * diff --git a/frameworks/core/include/ans_subscriber_proxy.h b/frameworks/core/include/ans_subscriber_proxy.h index 18ff83ee6..2c2d88324 100644 --- a/frameworks/core/include/ans_subscriber_proxy.h +++ b/frameworks/core/include/ans_subscriber_proxy.h @@ -52,12 +52,14 @@ public: * @brief The callback function on a notification canceled. * * @param notification Indicates the canceled notification. - * @param notificationMap Indicates the NotificationSortingMap object. * @param deleteReason Indicates the delete reason. */ void OnCanceled(const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) override; + void OnCanceledList(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) override; + /** * @brief The callback function on the notifications updated. * @@ -89,6 +91,8 @@ public: private: ErrCode InnerTransact(NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); static inline BrokerDelegator delegator_; + template + bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &data); }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/include/ans_subscriber_stub.h b/frameworks/core/include/ans_subscriber_stub.h index a04b2773c..5e03e1e10 100644 --- a/frameworks/core/include/ans_subscriber_stub.h +++ b/frameworks/core/include/ans_subscriber_stub.h @@ -68,6 +68,9 @@ public: void OnCanceled(const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) override; + void OnCanceledList(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) override; + /** * @brief The callback function on the notifications updated. * @@ -103,10 +106,13 @@ private: ErrCode HandleOnDisconnected(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnCanceledListMap(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnUpdated(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply); ErrCode HandleOnBadgeChanged(MessageParcel &data, MessageParcel &reply); + template + bool ReadParcelableVector(std::vector> &parcelableInfos, MessageParcel &data); }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index fbd1d6d5f..959baaa8d 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -52,6 +52,7 @@ namespace Notification { IS_NOTIFICATION_POLICY_ACCESS_GRANTED, REMOVE_NOTIFICATION, REMOVE_ALL_NOTIFICATIONS, + REMOVE_NOTIFICATIONS_BY_KEYS, DELETE_NOTIFICATION, DELETE_NOTIFICATION_BY_BUNDLE, DELETE_ALL_NOTIFICATIONS, @@ -109,6 +110,7 @@ namespace Notification { ON_CONSUMED, // Obsolete ON_CONSUMED_MAP, ON_CANCELED_MAP, + ON_CANCELED_LIST_MAP, ON_UPDATED, ON_DND_DATE_CHANGED, ON_ENABLED_NOTIFICATION_CHANGED, diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 5bad741bf..57efd7ab3 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -823,6 +823,50 @@ ErrCode AnsManagerProxy::RemoveAllNotifications(const sptr &keys, int32_t removeReason) +{ + if (keys.empty()) { + ANS_LOGE("[RemoveAllNotifications] fail: keys is empty."); + return ERR_ANS_INVALID_PARAM; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[RemoveAllNotifications] fail:, write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(keys.size())) { + ANS_LOGE("write keys size failed"); + return false; + } + + if (!data.WriteStringVector(keys)) { + ANS_LOGE("[RemoveAllNotifications] fail: write keys failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(removeReason)) { + ANS_LOGE("[Delete] fail: write removeReason failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::REMOVE_NOTIFICATIONS_BY_KEYS, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[RemoveNotification] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[RemoveNotification] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::Delete(const std::string &key, int32_t removeReason) { if (key.empty()) { diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 526531b8e..d163eee60 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -100,6 +100,9 @@ const std::map keys; + if (!data.ReadStringVector(&keys)) { + ANS_LOGE("read keys failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + int32_t removeReason = 0; + if (!data.ReadInt32(removeReason)) { + ANS_LOGE("read removeReason failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = RemoveNotifications(keys, removeReason); + if (!reply.WriteInt32(result)) { + ANS_LOGE("write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + + ErrCode AnsManagerStub::HandleDelete(MessageParcel &data, MessageParcel &reply) { std::string key; @@ -1899,6 +1932,12 @@ ErrCode AnsManagerStub::RemoveAllNotifications(const sptr &keys, int32_t removeReason) +{ + ANS_LOGE("called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::Delete(const std::string &key, int32_t removeReason) { ANS_LOGE("AnsManagerStub::Delete called!"); diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 3a099b356..feea0bbbc 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -488,6 +488,21 @@ ErrCode AnsNotification::RemoveAllNotifications(const NotificationBundleOption & return ansManagerProxy_->RemoveAllNotifications(bo); } +ErrCode AnsNotification::RemoveNotifications(const std::vector hashcodes, int32_t removeReason) +{ + if (hashcodes.empty()) { + ANS_LOGE("Hashcodes is empty"); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->RemoveNotifications(hashcodes, removeReason); +} + ErrCode AnsNotification::RemoveNotificationsByBundle(const NotificationBundleOption &bundleOption) { if (bundleOption.GetBundleName().empty()) { diff --git a/frameworks/core/src/ans_subscriber_proxy.cpp b/frameworks/core/src/ans_subscriber_proxy.cpp index 834dd66b6..96f7b9fa3 100644 --- a/frameworks/core/src/ans_subscriber_proxy.cpp +++ b/frameworks/core/src/ans_subscriber_proxy.cpp @@ -172,6 +172,68 @@ void AnsSubscriberProxy::OnCanceled( } } +void AnsSubscriberProxy::OnCanceledList(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) +{ + if (notifications.empty()) { + ANS_LOGE("Notifications is empty."); + return; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) { + ANS_LOGE("Write interface token failed."); + return; + } + + if (!WriteParcelableVector(notifications, data)) { + ANS_LOGE("Write notifications failed"); + return; + } + + if (!data.WriteBool(notificationMap != nullptr)) { + ANS_LOGE("Write existMap failed"); + return; + } + + if (notificationMap != nullptr) { + if (!data.WriteParcelable(notificationMap)) { + ANS_LOGE("Write notificationMap failed"); + return; + } + } + + if (!data.WriteInt32(deleteReason)) { + ANS_LOGE("Write deleteReason failed."); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CANCELED_LIST_MAP, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("Transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} + +template +bool AnsSubscriberProxy::WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &data) +{ + if (!data.WriteInt32(parcelableVector.size())) { + ANS_LOGE("write ParcelableVector size failed"); + return false; + } + + for (auto &parcelable : parcelableVector) { + if (!data.WriteStrongParcelable(parcelable)) { + ANS_LOGE("write ParcelableVector failed"); + return false; + } + } + return true; +} + void AnsSubscriberProxy::OnUpdated(const sptr ¬ificationMap) { if (notificationMap == nullptr) { diff --git a/frameworks/core/src/ans_subscriber_stub.cpp b/frameworks/core/src/ans_subscriber_stub.cpp index b8dff16b3..066e36360 100644 --- a/frameworks/core/src/ans_subscriber_stub.cpp +++ b/frameworks/core/src/ans_subscriber_stub.cpp @@ -34,6 +34,8 @@ AnsSubscriberStub::AnsSubscriberStub() std::bind(&AnsSubscriberStub::HandleOnConsumedMap, this, std::placeholders::_1, std::placeholders::_2)); interfaces_.emplace(NotificationInterfaceCode::ON_CANCELED_MAP, std::bind(&AnsSubscriberStub::HandleOnCanceledMap, this, std::placeholders::_1, std::placeholders::_2)); + interfaces_.emplace(NotificationInterfaceCode::ON_CANCELED_LIST_MAP, + std::bind(&AnsSubscriberStub::HandleOnCanceledListMap, this, std::placeholders::_1, std::placeholders::_2)); interfaces_.emplace(NotificationInterfaceCode::ON_UPDATED, std::bind(&AnsSubscriberStub::HandleOnUpdated, this, std::placeholders::_1, std::placeholders::_2)); interfaces_.emplace(NotificationInterfaceCode::ON_DND_DATE_CHANGED, @@ -147,6 +149,64 @@ ErrCode AnsSubscriberStub::HandleOnCanceledMap(MessageParcel &data, MessageParce return ERR_OK; } + +ErrCode AnsSubscriberStub::HandleOnCanceledListMap(MessageParcel &data, MessageParcel &reply) +{ + std::vector> notifications; + if (!ReadParcelableVector(notifications, data)) { + ANS_LOGE("Read notifications failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool existMap = false; + if (!data.ReadBool(existMap)) { + ANS_LOGW("Read existMap failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + sptr notificationMap = nullptr; + if (existMap) { + notificationMap = data.ReadParcelable(); + if (notificationMap == nullptr) { + ANS_LOGW("Read NotificationSortingMap failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + } + + int32_t reason = 0; + if (!data.ReadInt32(reason)) { + ANS_LOGW("Read reason failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + OnCanceledList(notifications, notificationMap, reason); + return ERR_OK; +} + + +template +bool AnsSubscriberStub::ReadParcelableVector(std::vector> &parcelableInfos, MessageParcel &data) +{ + int32_t infoSize = 0; + if (!data.ReadInt32(infoSize)) { + ANS_LOGE("read Parcelable size failed."); + return false; + } + + parcelableInfos.clear(); + infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM; + for (int32_t index = 0; index < infoSize; index++) { + sptr info = data.ReadStrongParcelable(); + if (info == nullptr) { + ANS_LOGE("read Parcelable infos failed."); + return false; + } + parcelableInfos.emplace_back(info); + } + + return true; +} + ErrCode AnsSubscriberStub::HandleOnUpdated(MessageParcel &data, MessageParcel &reply) { sptr notificationMap = data.ReadParcelable(); @@ -206,6 +266,10 @@ void AnsSubscriberStub::OnCanceled( const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) {} +void AnsSubscriberStub::OnCanceledList(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) +{} + void AnsSubscriberStub::OnUpdated(const sptr ¬ificationMap) {} diff --git a/frameworks/js/napi/include/common.h b/frameworks/js/napi/include/common.h index 06a546dff..1c8f831e8 100644 --- a/frameworks/js/napi/include/common.h +++ b/frameworks/js/napi/include/common.h @@ -1369,6 +1369,8 @@ public: */ static napi_value GetBundleOption(const napi_env &env, const napi_value &value, NotificationBundleOption &option); + static napi_value GetHashCodes(const napi_env &env, const napi_value &value, std::vector &hashCodes); + /** * @brief Gets a NotificationKey object from specified js object * diff --git a/frameworks/js/napi/include/remove.h b/frameworks/js/napi/include/remove.h index bcab40bd4..a6ceefe53 100644 --- a/frameworks/js/napi/include/remove.h +++ b/frameworks/js/napi/include/remove.h @@ -30,6 +30,7 @@ struct BundleAndKeyInfo { struct RemoveParams { std::optional hashcode {}; std::optional bundleAndKeyInfo {}; + std::vector hashcodes; int32_t userId = SUBSCRIBE_USER_INIT; int32_t removeReason = NotificationConstant::CANCEL_REASON_DELETE; bool hasUserId = false; diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index 57dcde2e2..4e47a5a6d 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -4242,6 +4242,33 @@ napi_value Common::GetBundleOption(const napi_env &env, const napi_value &value, return NapiGetNull(env); } +napi_value Common::GetHashCodes(const napi_env &env, const napi_value &value, std::vector &hashCodes) +{ + ANS_LOGD("enter"); + uint32_t length = 0; + napi_get_array_length(env, value, &length); + if (length == 0) { + ANS_LOGE("The array is empty."); + return nullptr; + } + napi_valuetype valuetype = napi_undefined; + for (size_t i = 0; i < length; i++) { + napi_value hashCode = nullptr; + napi_get_element(env, value, i, &hashCode); + NAPI_CALL(env, napi_typeof(env, hashCode, &valuetype)); + if (valuetype != napi_string) { + ANS_LOGE("Wrong argument type. Object expected."); + return nullptr; + } + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, hashCode, str, STR_MAX_SIZE - 1, &strLen)); + hashCodes.emplace_back(str); + } + + return NapiGetNull(env); +} + napi_value Common::GetNotificationKey(const napi_env &env, const napi_value &value, NotificationKey &key) { ANS_LOGI("enter"); diff --git a/frameworks/js/napi/src/remove.cpp b/frameworks/js/napi/src/remove.cpp index 95fcbb739..b2f5bede2 100644 --- a/frameworks/js/napi/src/remove.cpp +++ b/frameworks/js/napi/src/remove.cpp @@ -64,7 +64,17 @@ bool ParseHashcodeTypeParams( const napi_env &env, napi_value* argv, size_t argc, napi_valuetype valueType, RemoveParams ¶ms) { // argv[0]: hashCode - if (valueType == napi_string) { + bool isArray = false; + napi_is_array(env, argv[PARAM0], &isArray); + if (isArray) { + std::vector hashcodes; + auto retValue = Common::GetHashCodes(env, argv[PARAM0], hashcodes); + if (retValue == nullptr) { + ANS_LOGW("GetHashCodes failed."); + return false; + } + params.hashcodes = hashcodes; + } else if (valueType == napi_string) { size_t strLen = 0; char str[STR_MAX_SIZE] = {0}; NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen), false); @@ -129,14 +139,16 @@ bool ParseParameters(const napi_env &env, const napi_callback_info &info, Remove ANS_LOGW("Wrong number of arguments."); return false; } + bool isArray = false; + napi_is_array(env, argv[PARAM0], &isArray); napi_valuetype valueType = napi_undefined; NAPI_CALL_BASE(env, napi_typeof(env, argv[PARAM0], &valueType), false); if ((valueType != napi_string) && (valueType != napi_object) && - (valueType != napi_number) && (valueType != napi_boolean)) { + (valueType != napi_number) && (valueType != napi_boolean) && !isArray) { ANS_LOGW("Wrong argument type. String or object expected."); return false; } - if ((valueType == napi_string) || (valueType == napi_number) || (valueType == napi_boolean)) { + if ((valueType == napi_string) || (valueType == napi_number) || (valueType == napi_boolean) || isArray) { return ParseHashcodeTypeParams(env, argv, argc, valueType, params); } return ParseBundleOptionTypeParams(env, argv, argc, params); diff --git a/frameworks/js/napi/src/subscribe/napi_remove.cpp b/frameworks/js/napi/src/subscribe/napi_remove.cpp index 94877913b..6c04130eb 100644 --- a/frameworks/js/napi/src/subscribe/napi_remove.cpp +++ b/frameworks/js/napi/src/subscribe/napi_remove.cpp @@ -29,7 +29,10 @@ void NapiRemoveExecuteCallback(napi_env env, void *data) } auto removeInfo = static_cast(data); if (removeInfo) { - if (removeInfo->params.hashcode.has_value()) { + if (!removeInfo->params.hashcodes.empty()) { + removeInfo->info.errorCode = NotificationHelper::RemoveNotifications(removeInfo->params.hashcodes, + removeInfo->params.removeReason); + } else if (removeInfo->params.hashcode.has_value()) { removeInfo->info.errorCode = NotificationHelper::RemoveNotification(removeInfo->params.hashcode.value(), removeInfo->params.removeReason); } else if (removeInfo->params.bundleAndKeyInfo.has_value()) { @@ -76,6 +79,7 @@ napi_value NapiRemove(napi_env env, napi_callback_info info) napi_value resourceName = nullptr; napi_create_string_latin1(env, "remove", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call napi_create_async_work(env, nullptr, resourceName, NapiRemoveExecuteCallback, NapiRemoveCompleteCallback, (void *)removeInfo, &removeInfo->asyncWork); diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 0d88b7b35..6956d1b8d 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -381,6 +381,8 @@ public: */ static ErrCode RemoveAllNotifications(const NotificationBundleOption &bundleOption); + static ErrCode RemoveNotifications(const std::vector hashcodes, int32_t removeReason); + /** * @brief Removes all removable notifications of a specified bundle. * @note Your application must have platform signature to use this method. diff --git a/interfaces/inner_api/notification_subscriber.h b/interfaces/inner_api/notification_subscriber.h index 808137550..eec196e3c 100644 --- a/interfaces/inner_api/notification_subscriber.h +++ b/interfaces/inner_api/notification_subscriber.h @@ -123,6 +123,9 @@ private: void OnCanceled(const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) override; + void OnCanceledList(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) override; + void OnUpdated(const sptr ¬ificationMap) override; void OnDoNotDisturbDateChange(const sptr &date) override; diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index be7b9c47d..f05a63c8b 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -280,6 +280,8 @@ public: */ ErrCode RemoveAllNotifications(const sptr &bundleOption) override; + ErrCode RemoveNotifications(const std::vector &keys, int32_t removeReason) override; + /** * @brief Delete notification based on key. * @@ -825,6 +827,8 @@ private: ErrCode PushCheck(const sptr &request); void StartAutoDelete(const std::shared_ptr &record); void TriggerAutoDelete(std::string hashCode); + void SendNotificationsOnCanceled(std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason); void SetAgentNotification(sptr& notificationRequest, std::string& bundleName); private: static sptr instance_; diff --git a/services/ans/include/notification_subscriber_manager.h b/services/ans/include/notification_subscriber_manager.h index 83a4ea459..a8e38c13b 100644 --- a/services/ans/include/notification_subscriber_manager.h +++ b/services/ans/include/notification_subscriber_manager.h @@ -76,6 +76,8 @@ public: void NotifyCanceled(const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason); + void BatchNotifyCanceled(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason); /** * @brief Notify all subscribers on updated. * @@ -127,6 +129,8 @@ private: const sptr ¬ification, const sptr ¬ificationMap); void NotifyCanceledInner(const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason); + void BatchNotifyCanceledInner(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason); void NotifyUpdatedInner(const sptr ¬ificationMap); void NotifyDoNotDisturbDateChangedInner(const sptr &date); void NotifyEnabledNotificationChangedInner(const sptr &callbackData); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 903d668e6..03624e61d 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -402,8 +402,7 @@ ErrCode AdvancedNotificationService::CancelPreparedNotification( if (notification != nullptr) { int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete("", "", notification); #endif @@ -678,6 +677,7 @@ ErrCode AdvancedNotificationService::CancelAll() sptr notification = nullptr; std::vector keys = GetNotificationKeys(bundleOption); + std::vector> notifications; for (auto key : keys) { #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; @@ -693,14 +693,26 @@ ErrCode AdvancedNotificationService::CancelAll() if (notification != nullptr) { int32_t reason = NotificationConstant::APP_CANCEL_ALL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + notifications.emplace_back(notification); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList; + for (auto item : notifications) { + currNotificationList.emplace_back(item); + } + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, NotificationConstant::APP_CANCEL_ALL_REASON_DELETE); + notifications.clear(); + } } + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::APP_CANCEL_ALL_REASON_DELETE); + } result = ERR_OK; })); return result; @@ -929,8 +941,7 @@ ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t remo if (notification != nullptr) { UpdateRecentNotification(notification, true, removeReason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, removeReason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif @@ -977,8 +988,7 @@ ErrCode AdvancedNotificationService::DeleteByBundle(const sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif @@ -1009,6 +1019,7 @@ ErrCode AdvancedNotificationService::DeleteAll() int32_t activeUserId = SUBSCRIBE_USER_INIT; (void)GetActiveUserId(activeUserId); std::vector keys = GetNotificationKeys(nullptr); + std::vector> notifications; for (auto key : keys) { #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; @@ -1025,12 +1036,18 @@ ErrCode AdvancedNotificationService::DeleteAll() if (notification->GetUserId() == activeUserId) { int32_t reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + notifications.emplace_back(notification); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + SendNotificationsOnCanceled(notifications, nullptr, NotificationConstant::CANCEL_ALL_REASON_DELETE); + } + } + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::CANCEL_REASON_DELETE); } result = ERR_OK; @@ -1778,8 +1795,7 @@ ErrCode AdvancedNotificationService::CancelContinuousTaskNotification(const std: if (notification != nullptr) { int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); } })); return result; @@ -2157,6 +2173,7 @@ void AdvancedNotificationService::OnBundleRemoved(const sptr keys = GetNotificationKeys(bundleOption); #endif + std::vector> notifications; for (auto key : keys) { sptr notification = nullptr; result = RemoveFromNotificationList(key, notification, true, @@ -2168,13 +2185,25 @@ void AdvancedNotificationService::OnBundleRemoved(const sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + notifications.emplace_back(notification); + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList; + for (auto item : notifications) { + currNotificationList.emplace_back(item); + } + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, reason); + notifications.clear(); + } #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete("", "", notification); #endif } } + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::PACKAGE_CHANGED_REASON_DELETE); + } NotificationPreferences::GetInstance().RemoveAnsBundleDbInfo(bundleOption); })); @@ -2306,8 +2335,7 @@ ErrCode AdvancedNotificationService::RemoveNotification(const sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, removeReason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif @@ -2342,6 +2370,7 @@ ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptrPostSyncTask(std::bind([&]() { std::vector> removeList; + int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; for (auto record : notificationList_) { if (!record->notification->IsRemoveAllowed()) { continue; @@ -2357,20 +2386,80 @@ ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptr> notifications; for (auto record : removeList) { notificationList_.remove(record); if (record->notification != nullptr) { - int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; UpdateRecentNotification(record->notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, sortingMap, reason); + notifications.emplace_back(record->notification); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(record->deviceId, record->bundleName, record->notification); #endif } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + SendNotificationsOnCanceled(notifications, nullptr, reason); + } TriggerRemoveWantAgent(record->request); } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason); + } + })); + + return ERR_OK; +} + +ErrCode AdvancedNotificationService::RemoveNotifications( + const std::vector &keys, int32_t removeReason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("enter"); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + handler_->PostSyncTask(std::bind([&]() { + std::vector> notifications; + for (auto key : keys) { + sptr notification = nullptr; +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + ErrCode result = RemoveFromNotificationList(key, notification, false, removeReason); + if (result != ERR_OK) { + continue; + } + if (notification != nullptr) { + UpdateRecentNotification(notification, true, removeReason); + notifications.emplace_back(notification); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList; + for (auto item : notifications) { + currNotificationList.emplace_back(item); + } + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, removeReason); + notifications.clear(); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, removeReason); + } })); return ERR_OK; @@ -2434,18 +2523,32 @@ ErrCode AdvancedNotificationService::CancelGroup(const std::string &groupName) } } + std::vector> notifications; for (auto record : removeList) { notificationList_.remove(record); if (record->notification != nullptr) { int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; UpdateRecentNotification(record->notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, sortingMap, reason); + notifications.emplace_back(record->notification); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(record->deviceId, record->bundleName, record->notification); #endif } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList; + for (auto item : notifications) { + currNotificationList.emplace_back(item); + } + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, NotificationConstant::APP_CANCEL_REASON_DELETE); + notifications.clear(); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::APP_CANCEL_REASON_DELETE); } })); @@ -2477,6 +2580,7 @@ ErrCode AdvancedNotificationService::RemoveGroupByBundle( handler_->PostSyncTask(std::bind([&]() { std::vector> removeList; + int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; for (auto record : notificationList_) { if (!record->notification->IsRemoveAllowed()) { continue; @@ -2491,18 +2595,24 @@ ErrCode AdvancedNotificationService::RemoveGroupByBundle( } } + std::vector> notifications; for (auto record : removeList) { notificationList_.remove(record); if (record->notification != nullptr) { - int32_t reason = NotificationConstant::CANCEL_REASON_DELETE; UpdateRecentNotification(record->notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, sortingMap, reason); + notifications.emplace_back(record->notification); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(record->deviceId, record->bundleName, record->notification); #endif } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + SendNotificationsOnCanceled(notifications, nullptr, reason); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason); } })); @@ -3126,8 +3236,7 @@ void AdvancedNotificationService::OnDistributedDelete( if (notification != nullptr) { int32_t reason = NotificationConstant::APP_CANCEL_REASON_OTHER; UpdateRecentNotification(notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); } })); } @@ -3281,6 +3390,7 @@ ErrCode AdvancedNotificationService::DeleteAllByUser(const int32_t &userId) ErrCode result = ERR_OK; handler_->PostSyncTask(std::bind([&]() { std::vector keys = GetNotificationKeys(nullptr); + std::vector> notifications; for (auto key : keys) { #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; @@ -3297,12 +3407,19 @@ ErrCode AdvancedNotificationService::DeleteAllByUser(const int32_t &userId) if (notification->GetUserId() == userId) { int32_t reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + notifications.emplace_back(notification); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + SendNotificationsOnCanceled(notifications, nullptr, NotificationConstant::CANCEL_ALL_REASON_DELETE); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::CANCEL_ALL_REASON_DELETE); } result = ERR_OK; @@ -3483,6 +3600,7 @@ void AdvancedNotificationService::OnBundleDataCleared(const sptrPostSyncTask(std::bind([&]() { std::vector keys = GetNotificationKeys(bundleOption); + std::vector> notifications; for (auto key : keys) { #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; @@ -3500,12 +3618,25 @@ void AdvancedNotificationService::OnBundleDataCleared(const sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, sortingMap, reason); + notifications.emplace_back(notification); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); #endif } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + std::vector> currNotificationList; + for (auto item : notifications) { + currNotificationList.emplace_back(item); + } + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotificationList, nullptr, NotificationConstant::PACKAGE_CHANGED_REASON_DELETE); + notifications.clear(); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::PACKAGE_CHANGED_REASON_DELETE); } })); } @@ -4147,12 +4278,23 @@ void AdvancedNotificationService::TriggerAutoDelete(std::string hashCode) if (record->notification->GetKey() == hashCode) { int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; UpdateRecentNotification(record->notification, true, reason); - sptr sortingMap = GenerateSortingMap(); - NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, sortingMap, reason); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, nullptr, reason); notificationList_.remove(record); break; } } } + +void AdvancedNotificationService::SendNotificationsOnCanceled(std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) +{ + std::vector> currNotifications; + for (auto notification : notifications) { + currNotifications.emplace_back(notification); + } + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + currNotifications, nullptr, deleteReason); + notifications.clear(); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index 1b20cb165..52999a918 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -136,6 +136,21 @@ void NotificationSubscriberManager::NotifyCanceled( handler_->PostTask(NotifyCanceledFunc); } +void NotificationSubscriberManager::BatchNotifyCanceled(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (handler_ == nullptr) { + ANS_LOGE("handler is nullptr"); + return; + } + + AppExecFwk::EventHandler::Callback NotifyCanceledFunc = std::bind( + &NotificationSubscriberManager::BatchNotifyCanceledInner, this, notifications, notificationMap, deleteReason); + + handler_->PostTask(NotifyCanceledFunc); +} + void NotificationSubscriberManager::NotifyUpdated(const sptr ¬ificationMap) { if (handler_ == nullptr) { @@ -354,6 +369,37 @@ void NotificationSubscriberManager::NotifyCanceledInner( } } +void NotificationSubscriberManager::BatchNotifyCanceledInner(const std::vector> ¬ifications, + const sptr ¬ificationMap, int32_t deleteReason) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + + ANS_LOGD("notifications size = <%{public}d>", notifications.size()); + for (auto record : subscriberRecordList_) { + ANS_LOGD("record->userId = <%{public}d>", record->userId); + std::vector> currNotifications; + for (int i = 0; i < notifications.size(); i ++) { + sptr notification = notifications[i]; + auto BundleNames = notification->GetBundleName(); + auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames); + int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId(); + int32_t sendUserId = notification->GetUserId(); + if (!record->subscribedAll == (iter != record->bundleList_.end()) && + ((record->userId == sendUserId) || + (record->userId == SUBSCRIBE_USER_ALL) || + (record->userId == recvUserId) || + IsSystemUser(record->userId) || // Delete this, When the systemui subscribe carry the user ID. + IsSystemUser(sendUserId))) { + currNotifications.emplace_back(notification); + } + } + if (!currNotifications.empty()) { + ANS_LOGD("onCanceledList currNotifications size = <%{public}d>", currNotifications.size()); + record->subscriber->OnCanceledList(currNotifications, notificationMap, deleteReason); + } + } +} + void NotificationSubscriberManager::NotifyUpdatedInner(const sptr ¬ificationMap) { for (auto record : subscriberRecordList_) { -- Gitee