From 6b80a343328c97aa77d15e9659f7d13240c9ce58 Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Fri, 19 May 2023 15:20:39 +0800 Subject: [PATCH] auto delete notification Signed-off-by: fangJinliang1 Change-Id: I6c6f91cae2aa7900bd3bee15dc686b4e421b67f2 Signed-off-by: fangJinliang1 --- .../include/advanced_notification_service.h | 2 ++ .../ans/src/advanced_notification_service.cpp | 32 +++++++++++++++++++ .../advanced_notification_service_test.cpp | 31 ++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 5b3e0fde3..87a6d4943 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -839,6 +839,8 @@ private: ErrCode PublishNotificationBySa(const sptr &request); bool IsNeedPushCheck(NotificationConstant::SlotType slotType); ErrCode PushCheck(const sptr &request); + void StartAutoDelete(const std::shared_ptr &record); + void TriggerAutoDelete(std::string hashCode); private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 0272ff621..fe38d1d81 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -483,6 +483,10 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( } #endif })); + + if (record->request->IsTapDismissed()) { + StartAutoDelete(record); + } return result; } @@ -4120,5 +4124,33 @@ ErrCode AdvancedNotificationService::PushCheck(const sptr & } return ERR_OK; } + +void AdvancedNotificationService::StartAutoDelete(const std::shared_ptr &record) +{ + ANS_LOGD("enter"); + auto triggerFunc = std::bind(&AdvancedNotificationService::TriggerAutoDelete, + this, record->notification->GetKey()); + int64_t autoDeleteTime = record->request->GetAutoDeletedTime() - GetCurrentTime(); + handler_->PostTask(triggerFunc, autoDeleteTime); +} + +void AdvancedNotificationService::TriggerAutoDelete(std::string hashCode) +{ + ANS_LOGD("enter"); + for (auto record : notificationList_) { + if (!record->request) { + continue; + } + + 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); + notificationList_.remove(record); + break; + } + } +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 0c44df22a..a35bf4a82 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -3531,5 +3531,36 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_220000 std::string result = advancedNotificationService_->TimeToString(time); EXPECT_EQ(result.substr(0, result.size() - 8), ret); } + +/** + * @tc.number : AdvancedNotificationServiceTest_22100 + * @tc.name : StartAutoDelete + * @tc.desc : Test StartAutoDelete function. + * @tc.require : #I6Z5I4 + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_221000, Function | SmallTest | Level1) +{ + GTEST_LOG_(INFO) << "AdvancedNotificationServiceTest_221000 test start"; + + sptr request = new (std::nothrow) NotificationRequest(); + EXPECT_NE(request, nullptr); + request->SetCreatorUid(0); + request->SetCreatorUserId(0); + request->SetLabel("label"); + request->SetTapDismissed(true); + request->SetAutoDeletedTime(clock() + 1000); // 1000ms + sptr notification = new (std::nothrow) Notification(request); + EXPECT_NE(notification, nullptr); + + auto record = std::make_shared(); + record->request = request; + record->notification = notification; + advancedNotificationService_->notificationList_.push_back(record); + advancedNotificationService_->StartAutoDelete(record); + + std::this_thread::sleep_for(std::chrono::seconds(2)); // 2ms + EXPECT_EQ(advancedNotificationService_->notificationList_.size(), 0); + GTEST_LOG_(INFO) << "AdvancedNotificationServiceTest_221000 test end"; +} } // namespace Notification } // namespace OHOS \ No newline at end of file -- Gitee