From 152b138e7a17988ffb4f79366eaf7045c1ed51bb Mon Sep 17 00:00:00 2001 From: z30053788 Date: Thu, 29 May 2025 20:20:56 +0800 Subject: [PATCH] update Signed-off-by: z30053788 Change-Id: I24a8429eb208f90d0139b30189b6a4fa05742afd --- frameworks/ans/src/notification_request.cpp | 19 ++++ interfaces/inner_api/notification_request.h | 7 ++ .../include/advanced_notification_service.h | 7 +- .../advanced_notification_publish_service.cpp | 98 ++++++++++++++++--- ...nced_notification_publish_service_test.cpp | 48 ++++++++- .../distributed/include/tlv_box/request_box.h | 2 + .../distributed/include/tlv_box/tlv_box.h | 5 +- .../distributed_publish_service_v2.cpp | 6 ++ .../distributed/src/tlv_box/request_box.cpp | 16 +++ 9 files changed, 189 insertions(+), 19 deletions(-) diff --git a/frameworks/ans/src/notification_request.cpp b/frameworks/ans/src/notification_request.cpp index de6a313a2..dfdea9e05 100644 --- a/frameworks/ans/src/notification_request.cpp +++ b/frameworks/ans/src/notification_request.cpp @@ -2915,6 +2915,25 @@ std::string NotificationRequest::GenerateUniqueKey() return stream.str(); } +std::string NotificationRequest::GenerateDistributedUniqueKey() +{ + const char *keySpliter = "_"; + int typeFlag = 0; + if (GetSlotType() == NotificationConstant::SlotType::LIVE_VIEW) { + typeFlag = 1; + } + + std::stringstream stream; + if (IsAgentNotification()) { + stream << ownerBundleName_ << keySpliter << ownerUserId_ << keySpliter << typeFlag << keySpliter + << appMessageId_; + } else { + stream << creatorBundleName_ << keySpliter << creatorUserId_ << keySpliter << typeFlag << keySpliter + << appMessageId_; + } + return stream.str(); +} + void NotificationRequest::SetUnifiedGroupInfo(const std::shared_ptr &unifiedGroupInfo) { unifiedGroupInfo_ = unifiedGroupInfo; diff --git a/interfaces/inner_api/notification_request.h b/interfaces/inner_api/notification_request.h index 4788f2fc6..545a70043 100644 --- a/interfaces/inner_api/notification_request.h +++ b/interfaces/inner_api/notification_request.h @@ -1392,6 +1392,13 @@ public: */ std::string GenerateUniqueKey(); + /** + * @brief Generate notification request distributed unique key. + * + * @return Return the distributed unique key of notification request. + */ + std::string GenerateDistributedUniqueKey(); + /** * @brief Sets the unifiedGroupInfo of this notification. * diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index e8003e2db..458ad52fa 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1510,7 +1510,10 @@ private: bool isSystemApp, bool isUpdateByOwner, const bool isAgentController); ErrCode DuplicateMsgControl(const sptr &request); void RemoveExpiredUniqueKey(); - bool IsDuplicateMsg(const std::string &uniqueKey); + void RemoveExpiredDistributedUniqueKey(); + void RemoveExpiredLocalUniqueKey(); + bool IsDuplicateMsg(const std::list> &msglist, + const std::string &key); void DeleteDuplicateMsgs(const sptr &bundleOption); ErrCode PublishRemoveDuplicateEvent(const std::shared_ptr &record); ErrCode UpdateSlotAuthInfo(const std::shared_ptr &record); @@ -1629,6 +1632,8 @@ private: std::shared_ptr notificationSlotFilter_ = nullptr; std::shared_ptr dialogManager_ = nullptr; std::list> uniqueKeyList_; + std::list> distributedUniqueKeyList_; + std::list> localUniqueKeyList_; std::list, uint64_t>> delayNotificationList_; std::mutex delayNotificationMutext_; static std::mutex doNotDisturbMutex_; diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 11685b714..1d58568d5 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -186,6 +186,10 @@ ErrCode AdvancedNotificationService::CollaboratePublish(const sptrsubmit_h([&]() { + if (DuplicateMsgControl(record->request) == ERR_ANS_DUPLICATE_MSG) { + (void)PublishRemoveDuplicateEvent(record); + return; + } if (AssignToNotificationList(record) != ERR_OK) { ANS_LOGE("Failed to assign notification list"); return; @@ -779,14 +783,29 @@ ErrCode AdvancedNotificationService::DuplicateMsgControl(const sptrGenerateUniqueKey(); - if (IsDuplicateMsg(uniqueKey)) { - ANS_LOGI("Duplicate msg, no need to notify, key is %{public}s, appmessageId is %{public}s", - request->GetKey().c_str(), request->GetAppMessageId().c_str()); - return ERR_ANS_DUPLICATE_MSG; + std::string distributedUniqueKey = request->GenerateDistributedUniqueKey(); + std::string localUniqueKey = distributedUniqueKey; + + if (request->GetDistributedCollaborate()) { + if (IsDuplicateMsg(distributedUniqueKeyList_, distributedUniqueKey)) { + ANS_LOGE("Distributed duplicate msg, no need to notify, key is %{public}s, appmessageId is %{public}s", + request->GetKey().c_str(), request->GetAppMessageId().c_str()); + return ERR_ANS_DUPLICATE_MSG; + } + localUniqueKeyList_.emplace_back(std::make_pair(std::chrono::system_clock::now(), localUniqueKey)); + distributedUniqueKeyList_.emplace_back(std::make_pair(std::chrono::system_clock::now(), distributedUniqueKey)); + } else { + if (IsDuplicateMsg(uniqueKeyList_, uniqueKey) || IsDuplicateMsg(localUniqueKeyList_, localUniqueKey)) { + ANS_LOGE("Duplicate msg, no need to notify, key is %{public}s, appmessageId is %{public}s", + request->GetKey().c_str(), request->GetAppMessageId().c_str()); + return ERR_ANS_DUPLICATE_MSG; + } + uniqueKeyList_.emplace_back(std::make_pair(std::chrono::system_clock::now(), uniqueKey)); + distributedUniqueKeyList_.emplace_back(std::make_pair(std::chrono::system_clock::now(), distributedUniqueKey)); } - - uniqueKeyList_.emplace_back(std::make_pair(std::chrono::system_clock::now(), uniqueKey)); return ERR_OK; } @@ -800,7 +819,6 @@ void AdvancedNotificationService::DeleteDuplicateMsgs(const sptrGetUid() << keySpliter << bundleOption->GetBundleName() << keySpliter; std::string uniqueKeyHead = stream.str(); - auto iter = uniqueKeyList_.begin(); for (auto iter = uniqueKeyList_.begin(); iter != uniqueKeyList_.end();) { if ((*iter).second.find(uniqueKeyHead) == 0) { iter = uniqueKeyList_.erase(iter); @@ -808,6 +826,30 @@ void AdvancedNotificationService::DeleteDuplicateMsgs(const sptrGetBundleName() << keySpliter; + std::string distributedUniqueKeyHead = stream.str(); + for (auto iter = distributedUniqueKeyList_.begin(); iter != distributedUniqueKeyList_.end();) { + if ((*iter).second.find(distributedUniqueKeyHead) == 0) { + iter = distributedUniqueKeyList_.erase(iter); + } else { + ++iter; + } + } + + stream.str(std::string()); + stream.clear(); + stream << bundleOption->GetBundleName() << keySpliter; + std::string localUniqueKeyHead = stream.str(); + for (auto iter = localUniqueKeyList_.begin(); iter != localUniqueKeyList_.end();) { + if ((*iter).second.find(localUniqueKeyHead) == 0) { + iter = localUniqueKeyList_.erase(iter); + } else { + ++iter; + } + } } void AdvancedNotificationService::RemoveExpiredUniqueKey() @@ -818,7 +860,7 @@ void AdvancedNotificationService::RemoveExpiredUniqueKey() uint32_t duration = std::chrono::duration_cast(abs(now - (*iter).first)).count(); ANS_LOGD("RemoveExpiredUniqueKey duration is %{public}u", duration); if (duration > SECONDS_IN_ONE_DAY) { - ANS_LOGD("RemoveExpiredUniqueKey end duration is %{public}u", duration); + ANS_LOGI("RemoveExpiredUniqueKey end duration is %{public}u", duration); iter = uniqueKeyList_.erase(iter); } else { break; @@ -826,14 +868,46 @@ void AdvancedNotificationService::RemoveExpiredUniqueKey() } } -bool AdvancedNotificationService::IsDuplicateMsg(const std::string &uniqueKey) +void AdvancedNotificationService::RemoveExpiredDistributedUniqueKey() { - for (auto record : uniqueKeyList_) { - if (strcmp(record.second.c_str(), uniqueKey.c_str()) == 0) { - return true; + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + auto iter = distributedUniqueKeyList_.begin(); + while (iter != distributedUniqueKeyList_.end()) { + uint32_t duration = std::chrono::duration_cast(abs(now - (*iter).first)).count(); + ANS_LOGD("RemoveExpired distributedUniqueKeyList_ duration is %{public}u", duration); + if (duration > SECONDS_IN_ONE_DAY) { + ANS_LOGI("RemoveExpired distributedUniqueKeyList_ end duration is %{public}u", duration); + iter = distributedUniqueKeyList_.erase(iter); + } else { + break; } } +} +void AdvancedNotificationService::RemoveExpiredLocalUniqueKey() +{ + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + auto iter = localUniqueKeyList_.begin(); + while (iter != localUniqueKeyList_.end()) { + uint32_t duration = std::chrono::duration_cast(abs(now - (*iter).first)).count(); + ANS_LOGD("RemoveExpired localUniqueKeyList_ duration is %{public}u", duration); + if (duration > SECONDS_IN_ONE_DAY) { + ANS_LOGI("RemoveExpired localUniqueKeyList_ end duration is %{public}u", duration); + iter = localUniqueKeyList_.erase(iter); + } else { + break; + } + } +} + +bool AdvancedNotificationService::IsDuplicateMsg(const std::list> &msglist, const std::string &key) +{ + for (auto record : msglist) { + if (strcmp(record.second.c_str(), key.c_str()) == 0) { + return true; + } + } return false; } diff --git a/services/ans/test/unittest/advanced_notification_publish_service_test.cpp b/services/ans/test/unittest/advanced_notification_publish_service_test.cpp index a852599e2..e1c7fdeec 100644 --- a/services/ans/test/unittest/advanced_notification_publish_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_publish_service_test.cpp @@ -1308,8 +1308,7 @@ HWTEST_F(AnsPublishServiceTest, IsDuplicateMsg_00001, Function | SmallTest | Lev request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); request->SetAppMessageId("test2"); auto uniqueKey = request->GenerateUniqueKey(); - - auto ret = advancedNotificationService_->IsDuplicateMsg(uniqueKey); + auto ret = advancedNotificationService_->IsDuplicateMsg(advancedNotificationService_->uniqueKeyList_, uniqueKey); ASSERT_EQ(ret, false); } @@ -1327,8 +1326,7 @@ HWTEST_F(AnsPublishServiceTest, IsDuplicateMsg_00002, Function | SmallTest | Lev auto uniqueKey = request->GenerateUniqueKey(); advancedNotificationService_->uniqueKeyList_.emplace_back( std::make_pair(std::chrono::system_clock::now(), uniqueKey)); - - auto ret = advancedNotificationService_->IsDuplicateMsg(uniqueKey); + auto ret = advancedNotificationService_->IsDuplicateMsg(advancedNotificationService_->uniqueKeyList_, uniqueKey); ASSERT_EQ(ret, true); } @@ -1353,6 +1351,48 @@ HWTEST_F(AnsPublishServiceTest, RemoveExpiredUniqueKey_00001, Function | SmallTe ASSERT_EQ(advancedNotificationService_->uniqueKeyList_.size(), 0); } +/** + * @tc.name: RemoveExpiredDistributedUniqueKey_00001 + * @tc.desc: Test RemoveExpiredDistributedUniqueKey + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsPublishServiceTest, RemoveExpiredDistributedUniqueKey_00001, Function | SmallTest | Level1) +{ + sptr request = new (std::nothrow) NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + request->SetAppMessageId("test3"); + auto distributedUniqueKey = request->GenerateDistributedUniqueKey(); + advancedNotificationService_->distributedUniqueKeyList_.emplace_back( + std::make_pair(std::chrono::system_clock::now() - std::chrono::hours(24), distributedUniqueKey)); + + sleep(1); + ASSERT_EQ(advancedNotificationService_->distributedUniqueKeyList_.size(), 1); + advancedNotificationService_->RemoveExpiredDistributedUniqueKey(); + ASSERT_EQ(advancedNotificationService_->distributedUniqueKeyList_.size(), 0); +} + +/** + * @tc.name: RemoveExpiredLocalUniqueKey_00001 + * @tc.desc: Test RemoveExpiredLocalUniqueKey + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AnsPublishServiceTest, RemoveExpiredLocalUniqueKey_00001, Function | SmallTest | Level1) +{ + sptr request = new (std::nothrow) NotificationRequest(); + request->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + request->SetAppMessageId("test4"); + auto localUniqueKey = request->GenerateDistributedUniqueKey(); + advancedNotificationService_->localUniqueKeyList_.emplace_back( + std::make_pair(std::chrono::system_clock::now() - std::chrono::hours(24), localUniqueKey)); + + sleep(1); + ASSERT_EQ(advancedNotificationService_->localUniqueKeyList_.size(), 1); + advancedNotificationService_->RemoveExpiredLocalUniqueKey(); + ASSERT_EQ(advancedNotificationService_->localUniqueKeyList_.size(), 0); +} + /* * @tc.name: SetSmartReminderEnabled_0100 * @tc.desc: test SetSmartReminderEnabled with parameters diff --git a/services/distributed/include/tlv_box/request_box.h b/services/distributed/include/tlv_box/request_box.h index 296548ba2..bc323e386 100644 --- a/services/distributed/include/tlv_box/request_box.h +++ b/services/distributed/include/tlv_box/request_box.h @@ -51,6 +51,7 @@ public: bool SetCommonLiveView(const std::vector& byteSequence); bool SetFinishTime(int64_t time); bool SetAutoDeleteTime(int64_t time); + bool SetAppMessageId(const std::string& appMessageId); #else bool GetNotificationHashCode(std::string& hasdCode) const; @@ -74,6 +75,7 @@ public: bool GetCommonLiveView(std::vector& byteSequence) const; bool GetFinishTime(int64_t& time) const; bool GetAutoDeleteTime(int64_t& time) const; + bool GetAppMessageId(std::string& appMessageId) const; #endif }; } // namespace Notification diff --git a/services/distributed/include/tlv_box/tlv_box.h b/services/distributed/include/tlv_box/tlv_box.h index f2cd9713d..37a39ce56 100644 --- a/services/distributed/include/tlv_box/tlv_box.h +++ b/services/distributed/include/tlv_box/tlv_box.h @@ -60,8 +60,8 @@ enum TlvType : int32_t { NOTIFICATION_BRIEF_TEXT = 20, NOTIFICATION_EXPANDED_TITLE = 21, NOTIFICATION_LONG_TITLE = 22, - NOTIFICATION_ALL_LINES_START_INDEX = 23, // 23 - 29 多行文本持有 - ALL_LINES_LENGTH = 30, + ALL_LINES_LENGTH = 23, + NOTIFICATION_APP_MESSAGE_ID = 24, RESULT_CODE = 989, OPERATION_TYPE = 990, OPERATION_EVENT_ID = 991, @@ -74,6 +74,7 @@ enum TlvType : int32_t { LOCAL_DEVICE_TYPE = 998, LOCAL_VERSION = 999, CHECK_SUM = 1000, + NOTIFICATION_ALL_LINES_START_INDEX = 2000, }; class TlvItem { diff --git a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp index a35450768..10a2db6aa 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp @@ -229,6 +229,9 @@ void DistributedPublishService::SendNotifictionRequest(const std::shared_ptrSetReminderFlag(requestPoint->GetFlags()->GetReminderFlags()); } + if (!requestPoint->GetAppMessageId().empty()) { + requestBox->SetAppMessageId(requestPoint->GetAppMessageId()); + } if (request->GetBundleName().empty()) { requestBox->SetCreatorBundleName(request->GetCreateBundle()); } else { @@ -446,6 +449,9 @@ void DistributedPublishService::MakeNotificationReminderFlag(const NotificationR if (box.GetReminderFlag(type)) { request->SetCollaboratedReminderFlag(static_cast(type)); } + if (box.GetAppMessageId(context)) { + request->SetAppMessageId(context); + } if (box.GetCreatorBundleName(context)) { request->SetOwnerBundleName(context); request->SetCreatorBundleName(context); diff --git a/services/distributed/src/tlv_box/request_box.cpp b/services/distributed/src/tlv_box/request_box.cpp index 42a5b6daf..407df781e 100644 --- a/services/distributed/src/tlv_box/request_box.cpp +++ b/services/distributed/src/tlv_box/request_box.cpp @@ -59,6 +59,14 @@ bool NotificationRequestBox::SetContentType(int32_t type) return box_->PutValue(std::make_shared(NOTIFICATION_CONTENT_TYPE, type)); } +bool NotificationRequestBox::SetAppMessageId(const std::string& appMessageId) +{ + if (box_ == nullptr) { + return false; + } + return box_->PutValue(std::make_shared(NOTIFICATION_APP_MESSAGE_ID, appMessageId)); +} + bool NotificationRequestBox::SetReminderFlag(int32_t flag) { if (box_ == nullptr) { @@ -421,6 +429,14 @@ bool NotificationRequestBox::GetAutoDeleteTime(int64_t& time) const } return box_->GetInt64Value(AUTO_DELETE_TIME, time); } + +bool NotificationRequestBox::GetAppMessageId(std::string& appMessageId) const +{ + if (box_ == nullptr) { + return false; + } + return box_->GetStringValue(NOTIFICATION_APP_MESSAGE_ID, appMessageId); +} #endif } } -- Gitee