diff --git a/frameworks/ans/src/message_user.cpp b/frameworks/ans/src/message_user.cpp index 6f3953cafc95f7542c8efdc66485f524e0cf5b89..a3794865b221ab70e3efcd8de938187fcda6e1ba 100644 --- a/frameworks/ans/src/message_user.cpp +++ b/frameworks/ans/src/message_user.cpp @@ -132,28 +132,28 @@ MessageUser *MessageUser::FromJson(const nlohmann::json &jsonObject) } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("key") != jsonEnd) { + if (jsonObject.find("key") != jsonEnd && jsonObject.at("key").is_string()) { messageUser->key_ = jsonObject.at("key").get(); } - if (jsonObject.find("name") != jsonEnd) { + if (jsonObject.find("name") != jsonEnd && jsonObject.at("name").is_string()) { messageUser->name_ = jsonObject.at("name").get(); } - if (jsonObject.find("pixelMap") != jsonEnd) { + if (jsonObject.find("pixelMap") != jsonEnd && jsonObject.at("pixelMap").is_string()) { auto pmStr = jsonObject.at("pixelMap").get(); messageUser->pixelMap_ = AnsImageUtil::UnPackImage(pmStr); } - if (jsonObject.find("uri") != jsonEnd) { + if (jsonObject.find("uri") != jsonEnd && jsonObject.at("uri").is_string()) { messageUser->uri_ = Uri(jsonObject.at("uri").get()); } - if (jsonObject.find("isMachine") != jsonEnd) { + if (jsonObject.find("isMachine") != jsonEnd && jsonObject.at("isMachine").is_boolean()) { messageUser->isMachine_ = jsonObject.at("isMachine").get(); } - if (jsonObject.find("isUserImportant") != jsonEnd) { + if (jsonObject.find("isUserImportant") != jsonEnd && jsonObject.at("isUserImportant").is_boolean()) { messageUser->isUserImportant_ = jsonObject.at("isUserImportant").get(); } diff --git a/frameworks/ans/src/notification_action_button.cpp b/frameworks/ans/src/notification_action_button.cpp index ea87da8274ec1c68dc4ba6eef8393bb9802d2521..737676658eebd09912aeaa4c28441479ecde1bae 100644 --- a/frameworks/ans/src/notification_action_button.cpp +++ b/frameworks/ans/src/notification_action_button.cpp @@ -247,21 +247,21 @@ NotificationActionButton *NotificationActionButton::FromJson(const nlohmann::jso } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("icon") != jsonEnd) { + if (jsonObject.find("icon") != jsonEnd && jsonObject.at("icon").is_string()) { auto iconStr = jsonObject.at("icon").get(); pButton->icon_ = AnsImageUtil::UnPackImage(iconStr); } - if (jsonObject.find("title") != jsonEnd) { + if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) { pButton->title_ = jsonObject.at("title").get(); } - if (jsonObject.find("wantAgent") != jsonEnd) { + if (jsonObject.find("wantAgent") != jsonEnd && jsonObject.at("wantAgent").is_string()) { auto wantAgentValue = jsonObject.at("wantAgent").get(); pButton->wantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString(wantAgentValue); } - if (jsonObject.find("extras") != jsonEnd) { + if (jsonObject.find("extras") != jsonEnd && jsonObject.at("extras").is_string()) { auto extrasString = jsonObject.at("extras").get(); if (!extrasString.empty()) { AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extrasString); diff --git a/frameworks/ans/src/notification_basic_content.cpp b/frameworks/ans/src/notification_basic_content.cpp index 0ab51a5c32f1697c0341cd9549c3ad8356730fa1..f02b5a3e7db1b4a375248b2e796a28b75152677d 100644 --- a/frameworks/ans/src/notification_basic_content.cpp +++ b/frameworks/ans/src/notification_basic_content.cpp @@ -73,15 +73,15 @@ void NotificationBasicContent::ReadFromJson(const nlohmann::json &jsonObject) } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("text") != jsonEnd) { + if (jsonObject.find("text") != jsonEnd && jsonObject.at("text").is_string()) { text_ = jsonObject.at("text").get(); } - if (jsonObject.find("title") != jsonEnd) { + if (jsonObject.find("title") != jsonEnd && jsonObject.at("title").is_string()) { title_ = jsonObject.at("title").get(); } - if (jsonObject.find("additionalText") != jsonEnd) { + if (jsonObject.find("additionalText") != jsonEnd && jsonObject.at("additionalText").is_string()) { additionalText_ = jsonObject.at("additionalText").get(); } } diff --git a/frameworks/ans/src/notification_content.cpp b/frameworks/ans/src/notification_content.cpp index 44a866ba1a5c802e527411d57c1a509d7fb0f0a0..244982c2d5b12a41fe94764765430ab2bc8a20a4 100644 --- a/frameworks/ans/src/notification_content.cpp +++ b/frameworks/ans/src/notification_content.cpp @@ -246,7 +246,12 @@ bool NotificationContent::ConvertJsonToContent(NotificationContent *target, cons return false; } - auto contentTypeValue = jsonObject.at("contentType").get(); + auto contentType = jsonObject.at("contentType"); + if (!contentType.is_number_integer()) { + ANS_LOGE("ContentType is not integer"); + return false; + } + auto contentTypeValue = contentType.get(); target->contentType_ = static_cast(contentTypeValue); auto contentObj = jsonObject.at("content"); diff --git a/frameworks/ans/src/notification_conversational_message.cpp b/frameworks/ans/src/notification_conversational_message.cpp index 16b787d62016a9c3736b16b4562c0365b7a672e7..a46c8297fa1991200dc16e05219fecbab8104e29 100644 --- a/frameworks/ans/src/notification_conversational_message.cpp +++ b/frameworks/ans/src/notification_conversational_message.cpp @@ -103,11 +103,11 @@ NotificationConversationalMessage *NotificationConversationalMessage::FromJson(c } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("arrivedTime") != jsonEnd) { + if (jsonObject.find("arrivedTime") != jsonEnd && jsonObject.at("arrivedTime").is_number_integer()) { pMessage->arrivedTime_ = jsonObject.at("arrivedTime").get(); } - if (jsonObject.find("text") != jsonEnd) { + if (jsonObject.find("text") != jsonEnd && jsonObject.at("text").is_string()) { pMessage->text_ = jsonObject.at("text").get(); } @@ -122,14 +122,14 @@ NotificationConversationalMessage *NotificationConversationalMessage::FromJson(c } } - if (jsonObject.find("uri") != jsonEnd) { + if (jsonObject.find("uri") != jsonEnd && jsonObject.at("uri").is_string()) { auto uriStr = jsonObject.at("uri").get(); if (!uriStr.empty()) { pMessage->uri_ = std::make_shared(uriStr); } } - if (jsonObject.find("mimeType") != jsonEnd) { + if (jsonObject.find("mimeType") != jsonEnd && jsonObject.at("mimeType").is_string()) { pMessage->mimeType_ = jsonObject.at("mimeType").get(); } diff --git a/frameworks/ans/src/notification_distributed_options.cpp b/frameworks/ans/src/notification_distributed_options.cpp index 4f6349f67d05349babd396c986141bc467475f0f..571ce714c8f8d95de4dab21808484089b0a637b4 100644 --- a/frameworks/ans/src/notification_distributed_options.cpp +++ b/frameworks/ans/src/notification_distributed_options.cpp @@ -97,15 +97,15 @@ NotificationDistributedOptions *NotificationDistributedOptions::FromJson(const n } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("isDistributed") != jsonEnd) { + if (jsonObject.find("isDistributed") != jsonEnd && jsonObject.at("isDistributed").is_boolean()) { pOpt->isDistributed_ = jsonObject.at("isDistributed").get(); } - if (jsonObject.find("devicesSupportDisplay") != jsonEnd) { + if (jsonObject.find("devicesSupportDisplay") != jsonEnd && jsonObject.at("devicesSupportDisplay").is_array()) { pOpt->devicesSupportDisplay_ = jsonObject.at("devicesSupportDisplay").get>(); } - if (jsonObject.find("devicesSupportOperate") != jsonEnd) { + if (jsonObject.find("devicesSupportOperate") != jsonEnd && jsonObject.at("devicesSupportOperate").is_array()) { pOpt->devicesSupportOperate_ = jsonObject.at("devicesSupportOperate").get>(); } diff --git a/frameworks/ans/src/notification_flags.cpp b/frameworks/ans/src/notification_flags.cpp index e26f1339d26ce32cc33289a6394ada08f31a6866..33325e445b522de9a9990654a86c6d4c0a2d0c88 100644 --- a/frameworks/ans/src/notification_flags.cpp +++ b/frameworks/ans/src/notification_flags.cpp @@ -72,12 +72,12 @@ NotificationFlags *NotificationFlags::FromJson(const nlohmann::json &jsonObject) } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("soundEnabled") != jsonEnd) { + if (jsonObject.find("soundEnabled") != jsonEnd && jsonObject.at("soundEnabled").is_number_integer()) { auto soundEnabled = jsonObject.at("soundEnabled").get(); pFlags->soundEnabled_ = static_cast(soundEnabled); } - if (jsonObject.find("vibrationEnabled") != jsonEnd) { + if (jsonObject.find("vibrationEnabled") != jsonEnd && jsonObject.at("vibrationEnabled").is_number_integer()) { auto vibrationEnabled = jsonObject.at("vibrationEnabled").get(); pFlags->vibrationEnabled_ = static_cast(vibrationEnabled); } diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index d208602e06d56bc70b19273cbee88a825cad6d10..0fc8750b92458c85c5304903416072212451d28c 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_long_text_content.cpp b/frameworks/ans/src/notification_long_text_content.cpp index 752d0ae79e250be926fad348559af8a584772fe9..db1119fb83615cd224dd9d7a21c3b5e5b3c1b743 100644 --- a/frameworks/ans/src/notification_long_text_content.cpp +++ b/frameworks/ans/src/notification_long_text_content.cpp @@ -107,15 +107,15 @@ NotificationLongTextContent *NotificationLongTextContent::FromJson(const nlohman pContent->ReadFromJson(jsonObject); const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("longText") != jsonEnd) { + if (jsonObject.find("longText") != jsonEnd && jsonObject.at("longText").is_string()) { pContent->longText_ = jsonObject.at("longText").get(); } - if (jsonObject.find("expandedTitle") != jsonEnd) { + if (jsonObject.find("expandedTitle") != jsonEnd && jsonObject.at("expandedTitle").is_string()) { pContent->expandedTitle_ = jsonObject.at("expandedTitle").get(); } - if (jsonObject.find("briefText") != jsonEnd) { + if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) { pContent->briefText_ = jsonObject.at("briefText").get(); } diff --git a/frameworks/ans/src/notification_media_content.cpp b/frameworks/ans/src/notification_media_content.cpp index d65672e0ce0b02d5a6b8f61e659cd6fcea9c8672..b292ccd4979b9dab8f183cb55e1d317f77818f36 100644 --- a/frameworks/ans/src/notification_media_content.cpp +++ b/frameworks/ans/src/notification_media_content.cpp @@ -80,7 +80,7 @@ NotificationMediaContent *NotificationMediaContent::FromJson(const nlohmann::jso pContent->ReadFromJson(jsonObject); const auto& jsonEnd = jsonObject.cend(); - if (jsonObject.find("sequenceNumbers") != jsonEnd) { + if (jsonObject.find("sequenceNumbers") != jsonEnd && jsonObject.at("sequenceNumbers").is_array()) { pContent->sequenceNumbers_ = jsonObject.at("sequenceNumbers").get>(); } diff --git a/frameworks/ans/src/notification_multiline_content.cpp b/frameworks/ans/src/notification_multiline_content.cpp index ef11f4f1dfbfbf9bf6ba6f30a4bd6cf98b6d47cc..5e1c30ebb7c4952b7c90b0b4a5014df52205645a 100644 --- a/frameworks/ans/src/notification_multiline_content.cpp +++ b/frameworks/ans/src/notification_multiline_content.cpp @@ -102,15 +102,15 @@ NotificationMultiLineContent *NotificationMultiLineContent::FromJson(const nlohm pContent->ReadFromJson(jsonObject); const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("expandedTitle") != jsonEnd) { + if (jsonObject.find("expandedTitle") != jsonEnd && jsonObject.at("expandedTitle").is_string()) { pContent->expandedTitle_ = jsonObject.at("expandedTitle").get(); } - if (jsonObject.find("briefText") != jsonEnd) { + if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) { pContent->briefText_ = jsonObject.at("briefText").get(); } - if (jsonObject.find("allLines") != jsonEnd) { + if (jsonObject.find("allLines") != jsonEnd && jsonObject.at("allLines").is_array()) { pContent->allLines_ = jsonObject.at("allLines").get>(); } diff --git a/frameworks/ans/src/notification_picture_content.cpp b/frameworks/ans/src/notification_picture_content.cpp index e683c5397689f19c65d7ffe5ee6a6092a31506f7..237af45fad0d512235bc81def7ed0b2b7f0c3a3e 100644 --- a/frameworks/ans/src/notification_picture_content.cpp +++ b/frameworks/ans/src/notification_picture_content.cpp @@ -88,15 +88,15 @@ NotificationPictureContent *NotificationPictureContent::FromJson(const nlohmann: pContent->ReadFromJson(jsonObject); const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("expandedTitle") != jsonEnd) { + if (jsonObject.find("expandedTitle") != jsonEnd && jsonObject.at("expandedTitle").is_string()) { pContent->expandedTitle_ = jsonObject.at("expandedTitle").get(); } - if (jsonObject.find("briefText") != jsonEnd) { + if (jsonObject.find("briefText") != jsonEnd && jsonObject.at("briefText").is_string()) { pContent->briefText_ = jsonObject.at("briefText").get(); } - if (jsonObject.find("bigPicture") != jsonEnd) { + if (jsonObject.find("bigPicture") != jsonEnd && jsonObject.at("bigPicture").is_string()) { auto picStr = jsonObject.at("bigPicture").get(); pContent->bigPicture_ = AnsImageUtil::UnPackImage(picStr); } diff --git a/frameworks/ans/src/notification_request.cpp b/frameworks/ans/src/notification_request.cpp index b97e43e6730172325e6fae86646be8d3ca7f2d0f..bf2052ad4d2be7c5bfe3b4f318aadb7f0aebd97a 100644 --- a/frameworks/ans/src/notification_request.cpp +++ b/frameworks/ans/src/notification_request.cpp @@ -766,7 +766,7 @@ NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObj } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("version") != jsonEnd) { + if (jsonObject.find("version") != jsonEnd && jsonObject.at("version").is_number_integer()) { jsonObject.at("version").get(); } @@ -778,7 +778,7 @@ NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObj ConvertJsonToBool(pRequest, jsonObject); - if (jsonObject.find("wantAgent") != jsonEnd) { + if (jsonObject.find("wantAgent") != jsonEnd && jsonObject.at("wantAgent").is_string()) { auto wantAgentValue = jsonObject.at("wantAgent").get(); pRequest->wantAgent_ = AbilityRuntime::WantAgent::WantAgentHelper::FromString(wantAgentValue); } @@ -795,7 +795,7 @@ NotificationRequest *NotificationRequest::FromJson(const nlohmann::json &jsonObj return nullptr; } - if (jsonObject.find("extraInfo") != jsonEnd) { + if (jsonObject.find("extraInfo") != jsonEnd && jsonObject.at("extraInfo").is_string()) { auto extraInfoStr = jsonObject.at("extraInfo").get(); if (!extraInfoStr.empty()) { AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr); @@ -1612,39 +1612,39 @@ void NotificationRequest::ConvertJsonToNum(NotificationRequest *target, const nl const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("id") != jsonEnd) { + if (jsonObject.find("id") != jsonEnd && jsonObject.at("id").is_number_integer()) { target->notificationId_ = jsonObject.at("id").get(); } - if (jsonObject.find("color") != jsonEnd) { + if (jsonObject.find("color") != jsonEnd && jsonObject.at("color").is_number_integer()) { target->color_ = jsonObject.at("color").get(); } - if (jsonObject.find("deliveryTime") != jsonEnd) { + if (jsonObject.find("deliveryTime") != jsonEnd && jsonObject.at("deliveryTime").is_number_integer()) { target->deliveryTime_ = jsonObject.at("deliveryTime").get(); } - if (jsonObject.find("autoDeletedTime") != jsonEnd) { + if (jsonObject.find("autoDeletedTime") != jsonEnd && jsonObject.at("autoDeletedTime").is_number_integer()) { target->autoDeletedTime_ = jsonObject.at("autoDeletedTime").get(); } - if (jsonObject.find("creatorUid") != jsonEnd) { + if (jsonObject.find("creatorUid") != jsonEnd && jsonObject.at("creatorUid").is_number_integer()) { target->creatorUid_ = jsonObject.at("creatorUid").get(); } - if (jsonObject.find("creatorPid") != jsonEnd) { + if (jsonObject.find("creatorPid") != jsonEnd && jsonObject.at("creatorPid").is_number_integer()) { target->creatorPid_ = jsonObject.at("creatorPid").get(); } - if (jsonObject.find("creatorUserId") != jsonEnd) { + if (jsonObject.find("creatorUserId") != jsonEnd && jsonObject.at("creatorUserId").is_number_integer()) { target->creatorUserId_ = jsonObject.at("creatorUserId").get(); } - if (jsonObject.find("receiverUserId") != jsonEnd) { + if (jsonObject.find("receiverUserId") != jsonEnd && jsonObject.at("receiverUserId").is_number_integer()) { target->receiverUserId_ = jsonObject.at("receiverUserId").get(); } - if (jsonObject.find("badgeNumber") != jsonEnd) { + if (jsonObject.find("badgeNumber") != jsonEnd && jsonObject.at("badgeNumber").is_number_integer()) { target->badgeNumber_ = jsonObject.at("badgeNumber").get(); } } @@ -1658,27 +1658,27 @@ void NotificationRequest::ConvertJsonToString(NotificationRequest *target, const const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("creatorBundleName") != jsonEnd) { + if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) { target->creatorBundleName_ = jsonObject.at("creatorBundleName").get(); } - if (jsonObject.find("ownerBundleName") != jsonEnd) { + if (jsonObject.find("ownerBundleName") != jsonEnd && jsonObject.at("ownerBundleName").is_string()) { target->ownerBundleName_ = jsonObject.at("ownerBundleName").get(); } - if (jsonObject.find("groupName") != jsonEnd) { + if (jsonObject.find("groupName") != jsonEnd && jsonObject.at("groupName").is_string()) { target->groupName_ = jsonObject.at("groupName").get(); } - if (jsonObject.find("label") != jsonEnd) { + if (jsonObject.find("label") != jsonEnd && jsonObject.at("label").is_string()) { target->label_ = jsonObject.at("label").get(); } - if (jsonObject.find("classification") != jsonEnd) { + if (jsonObject.find("classification") != jsonEnd && jsonObject.at("classification").is_string()) { target->classification_ = jsonObject.at("classification").get(); } - if (jsonObject.find("creatorBundleName") != jsonEnd) { + if (jsonObject.find("creatorBundleName") != jsonEnd && jsonObject.at("creatorBundleName").is_string()) { target->creatorBundleName_ = jsonObject.at("creatorBundleName").get(); } } @@ -1692,12 +1692,12 @@ void NotificationRequest::ConvertJsonToEnum(NotificationRequest *target, const n const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("slotType") != jsonEnd) { + if (jsonObject.find("slotType") != jsonEnd && jsonObject.at("slotType").is_number_integer()) { auto slotTypeValue = jsonObject.at("slotType").get(); target->slotType_ = static_cast(slotTypeValue); } - if (jsonObject.find("badgeIconStyle") != jsonEnd) { + if (jsonObject.find("badgeIconStyle") != jsonEnd && jsonObject.at("badgeIconStyle").is_number_integer()) { auto badgeStyleValue = jsonObject.at("badgeIconStyle").get(); target->badgeStyle_ = static_cast(badgeStyleValue); } @@ -1712,39 +1712,39 @@ void NotificationRequest::ConvertJsonToBool(NotificationRequest *target, const n const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("showDeliveryTime") != jsonEnd) { + if (jsonObject.find("showDeliveryTime") != jsonEnd && jsonObject.at("showDeliveryTime").is_boolean()) { target->showDeliveryTime_ = jsonObject.at("showDeliveryTime").get(); } - if (jsonObject.find("tapDismissed") != jsonEnd) { + if (jsonObject.find("tapDismissed") != jsonEnd && jsonObject.at("tapDismissed").is_boolean()) { target->tapDismissed_ = jsonObject.at("tapDismissed").get(); } - if (jsonObject.find("colorEnabled") != jsonEnd) { + if (jsonObject.find("colorEnabled") != jsonEnd && jsonObject.at("colorEnabled").is_boolean()) { target->colorEnabled_ = jsonObject.at("colorEnabled").get(); } - if (jsonObject.find("isOngoing") != jsonEnd) { + if (jsonObject.find("isOngoing") != jsonEnd && jsonObject.at("isOngoing").is_boolean()) { target->inProgress_ = jsonObject.at("isOngoing").get(); } - if (jsonObject.find("isAlertOnce") != jsonEnd) { + if (jsonObject.find("isAlertOnce") != jsonEnd && jsonObject.at("isAlertOnce").is_boolean()) { target->alertOneTime_ = jsonObject.at("isAlertOnce").get(); } - if (jsonObject.find("isStopwatch") != jsonEnd) { + if (jsonObject.find("isStopwatch") != jsonEnd && jsonObject.at("isStopwatch").is_boolean()) { target->showStopwatch_ = jsonObject.at("isStopwatch").get(); } - if (jsonObject.find("isCountdown") != jsonEnd) { + if (jsonObject.find("isCountdown") != jsonEnd && jsonObject.at("isCountdown").is_boolean()) { target->isCountdown_ = jsonObject.at("isCountdown").get(); } - if (jsonObject.find("isUnremovable") != jsonEnd) { + if (jsonObject.find("isUnremovable") != jsonEnd && jsonObject.at("isUnremovable").is_boolean()) { target->unremovable_ = jsonObject.at("isUnremovable").get(); } - if (jsonObject.find("isFloatingIcon") != jsonEnd) { + if (jsonObject.find("isFloatingIcon") != jsonEnd && jsonObject.at("isFloatingIcon").is_boolean()) { target->floatingIcon_ = jsonObject.at("isFloatingIcon").get(); } } @@ -1758,12 +1758,12 @@ void NotificationRequest::ConvertJsonToPixelMap(NotificationRequest *target, con const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("smallIcon") != jsonEnd) { + if (jsonObject.find("smallIcon") != jsonEnd && jsonObject.at("smallIcon").is_string()) { auto littleIconStr = jsonObject.at("smallIcon").get(); target->littleIcon_ = AnsImageUtil::UnPackImage(littleIconStr); } - if (jsonObject.find("largeIcon") != jsonEnd) { + if (jsonObject.find("largeIcon") != jsonEnd && jsonObject.at("largeIcon").is_string()) { auto bigIconStr = jsonObject.at("largeIcon").get(); target->bigIcon_ = AnsImageUtil::UnPackImage(bigIconStr); } diff --git a/frameworks/ans/src/notification_subscriber.cpp b/frameworks/ans/src/notification_subscriber.cpp index 5a01a737f51b764046b4cd17faf03f7665a86d9c..49ed76a56f1f8a1c1d8e612b322fedb4ddb07fb1 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/ans/src/notification_user_input.cpp b/frameworks/ans/src/notification_user_input.cpp index 67b358e7cca8bade50ffbdd9638e300d0929053a..baab204a5212866ea08e1554fc1b02ee7b7cecb0 100644 --- a/frameworks/ans/src/notification_user_input.cpp +++ b/frameworks/ans/src/notification_user_input.cpp @@ -261,27 +261,27 @@ NotificationUserInput *NotificationUserInput::FromJson(const nlohmann::json &jso } const auto &jsonEnd = jsonObject.cend(); - if (jsonObject.find("inputKey") != jsonEnd) { + if (jsonObject.find("inputKey") != jsonEnd && jsonObject.at("inputKey").is_string()) { pUserInput->inputKey_ = jsonObject.at("inputKey").get(); } - if (jsonObject.find("tag") != jsonEnd) { + if (jsonObject.find("tag") != jsonEnd && jsonObject.at("tag").is_string()) { pUserInput->tag_ = jsonObject.at("tag").get(); } - if (jsonObject.find("options") != jsonEnd) { + if (jsonObject.find("options") != jsonEnd && jsonObject.at("options").is_array()) { pUserInput->options_ = jsonObject.at("options").get>(); } - if (jsonObject.find("permitFreeFormInput") != jsonEnd) { + if (jsonObject.find("permitFreeFormInput") != jsonEnd && jsonObject.at("permitFreeFormInput").is_boolean()) { pUserInput->permitFreeFormInput_ = jsonObject.at("permitFreeFormInput").get(); } - if (jsonObject.find("permitMimeTypes") != jsonEnd) { + if (jsonObject.find("permitMimeTypes") != jsonEnd && jsonObject.at("permitMimeTypes").is_array()) { pUserInput->permitMimeTypes_ = jsonObject.at("permitMimeTypes").get>(); } - if (jsonObject.find("additionalData") != jsonEnd) { + if (jsonObject.find("additionalData") != jsonEnd && jsonObject.at("additionalData").is_string()) { auto additionalDataString = jsonObject.at("additionalData").get(); if (!additionalDataString.empty()) { AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(additionalDataString); @@ -289,7 +289,7 @@ NotificationUserInput *NotificationUserInput::FromJson(const nlohmann::json &jso } } - if (jsonObject.find("editType") != jsonEnd) { + if (jsonObject.find("editType") != jsonEnd && jsonObject.at("editType").is_number_integer()) { auto editTypeValue = jsonObject.at("editType").get(); pUserInput->editType_ = static_cast(editTypeValue); } diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index f1654f1062b926c753f12d961e2dd823402e0639..82c4900ac90743f8660d4cfcb972e74d7cdcc9d8 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 6041aae154e4532873fbf47a2a9e6d90032f8a81..ccccdcd2fc88d613189b2ad55c99925cf285ad6c 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 73eb6b88ced1a1e0faa872150105919434daf607..a73751a9e52168c87f7beb403baf02ef1a2eda66 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 dcd90008dcc2bc30bfdccbfdaf2c9f3272ead59b..553150a59a63e0e5131d3861e3da7ac9efda4a1d 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 f7d7af22265b38e9d83861c51df2b5555ec21af9..0794a5c4cd974e24faf77d792cc805da9750202a 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 3d3fba0c000ab8bdd2ed8e1224194f69d891e290..91e868f79580e76e3f6bfe1d7abe696e5d43d2b2 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 18ff83ee6c8bf08c51dc1a4ee8b7cdb7921565ba..2c2d883243f931c729aad561abbbf91f323bf481 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 a04b2773c187f6bea2208275ffc69c47b217bf8a..5e03e1e10cd31fa73af6b9c5e86941a7706bd705 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 fbd1d6d5fc05cf028c69760232b4d369d7c95a41..959baaa8dee18a1778e8c36b3d114a71c78e6648 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 5bad741bfe83441c41975e271eac82da1fd238e8..57efd7ab3abe5d66535cbab862b8312b79f39f09 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 526531b8e7de2d43fb64cec30b21672e9a49f101..d163eee607e1b17c5b4ebbd3eec7dd29d69b383f 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 3a099b3567e0b5e2e1be9fee6130ac04c8836807..feea0bbbc85b875c735827cbc7143ba7a517d0f5 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 834dd66b6ab03752f4d4768c03e10fb49c31b1e2..96f7b9fa37e03e08f3819dc10a03102f06ec8b91 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 b8dff16b3d0a22949968bb82c0ec70c5360ebec3..066e363602b9c134d07bf463c0158704b47d558a 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 06a546dff6f0c04916f09b3b5fb090a0c95b2838..1c8f831e8ac9979852e54efebf15b1bc4431a56c 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 bcab40bd4648e44fc72691f43dc8899d2aba3d99..a6ceefe530b4f59688cf85da8a7fffd7510e52e1 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 57dcde2e275e2e05a2d872682e9ccbab805a524a..4e47a5a6dc4af80291ea90953a4c54e4b9c06215 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 95fcbb7395c63eb050d2250ef7ce6208955b1037..b2f5bede20062c37759a6e23d6e9d39a925b534b 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 94877913b7487bd2c7f03df6c608cdff74b23590..6c04130eb881cf262036aecb8b5a2edb7b0f41fa 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 0d88b7b358721963c513b78e646db4414c2b4988..6956d1b8d9a052f6e1de2a546f5f93f842dd7d11 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 8081375505cbd77c5445bfa84d5999c457fa6700..eec196e3ce36fb05c53793048f94caef3d0c7877 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 be7b9c47d977a006c9454911e194ae47fa72704a..f05a63c8b1afb72923324f0e1c090df66e8dbe15 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 83a4ea4595a641bad9bbc702338fe3824bcf97e7..a8e38c13b7fc6f80f7a1d4faa7eb6148fa4b1775 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 903d668e6bc66cd5382c3ab29c3abf63545688a4..03624e61dd5a5101299bfa0d16b93e517ff3ce86 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 1b20cb165f09d0d4956d6fd3d44895e131f74bd4..52999a9187620527a661605758d60bed8904afe3 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_) {