diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index 79e1e6ec78074e0fdb85ef82dfd0209ae9ddfdb6..810e1128a5a55577c726190cbe048843fb37ed3c 100644 --- a/frameworks/test/moduletest/BUILD.gn +++ b/frameworks/test/moduletest/BUILD.gn @@ -62,6 +62,7 @@ ohos_moduletest("ans_fw_module_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] @@ -125,6 +126,7 @@ ohos_moduletest("ans_innerkits_module_publish_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] @@ -190,6 +192,7 @@ ohos_moduletest("ans_innerkits_module_slot_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] @@ -254,6 +257,7 @@ ohos_moduletest("ans_innerkits_module_setting_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] defines = [] diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 49429688d5ea3804a20c0f0a7d67309e12e3498d..8a2e1dbfb5e9bbb6ff6f513cab885358877404dd 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -55,6 +55,8 @@ ohos_shared_library("libans") { deps = [ "${arkui_ace_engine_path}/interfaces/inner_api/ui_service_manager:ui_service_mgr", "${core_path}:ans_core", + "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", + "//third_party/libuv:uv", ] if (is_double_framework) { diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index bc084aa91502b4a4b084839272ea4a6268d9f7c1..354f3f55477c9b7afa35c40487d473f76e918637 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "event_handler.h" #include "event_runner.h" @@ -816,6 +817,8 @@ private: const NotificationConstant::SlotType &slotType, bool enabled, ErrCode errCode); void SendFlowControlOccurHiSysEvent(const std::shared_ptr &record); ErrCode PublishNotificationBySa(const sptr &request); + void StartAutoDelete(); + void TriggerAutoDelete(); private: static sptr instance_; @@ -833,6 +836,12 @@ private: NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; bool localScreenOn_ = true; #endif + + std::shared_ptr autoDelRunner_ = nullptr; + std::shared_ptr autoDelHandler_ = nullptr; + uv_timer_t timerReq_; + uv_loop_t *loop_ = nullptr; + bool uvTimerRunning_ = false; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index cb5e21dfb71aef2c6cab3d69d3b7c28e2bef402a..5a880dbf161050e69370922e1411fa29239808ca 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -436,6 +436,65 @@ ErrCode AdvancedNotificationService::PrepareNotificationInfo( return ERR_OK; } +void AdvancedNotificationService::TriggerAutoDelete() +{ + bool needStop = true; + for (auto iter = notificationList_.begin(); iter != notificationList_.end();) { + std::shared_ptr record = *iter; + if (!record->request->IsTapDismissed()) { + iter++; + continue; + } + + int64_t autoDeleteTime = record->request->GetAutoDeletedTime() / 1000; + if (autoDeleteTime <= time(nullptr)) { + int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; + UpdateRecentNotification(record->notification, true, reason); + sptr sortingMap = GenerateSortingMap(); + NotificationSubscriberManager::GetInstance()->NotifyCanceled(record->notification, sortingMap, reason); + record->request->SetTapDismissed(false); + iter = notificationList_.erase(iter); + } else { + iter++; + } + needStop = false; + } + + if (needStop) { + ANS_LOGD("No auto delete notification, stop timer."); + uvTimerRunning_ = false; + uv_timer_stop(&timerReq_); + } +} + +void AdvancedNotificationService::StartAutoDelete() +{ + ANS_LOGD("enter"); + if (!autoDelHandler_) { + autoDelRunner_ = OHOS::AppExecFwk::EventRunner::Create("NotificationAutoDel"); + autoDelHandler_ = std::make_shared(autoDelRunner_); + AnsWatchdog::AddHandlerThread(autoDelHandler_, autoDelRunner_); + + loop_ = uv_default_loop(); + uv_timer_init(loop_, &timerReq_); + timerReq_.data = this; + } + + autoDelHandler_->PostTask(std::bind([&]() { + if (uvTimerRunning_) { + return; + } + uv_timer_start(&timerReq_, [](uv_timer_t* timer) { + ANS_LOGI("timer start"); + auto notificationService = static_cast(timer->data); + notificationService->TriggerAutoDelete(); + }, 0, 1000); + + uvTimerRunning_ = true; + uv_run(loop_, UV_RUN_DEFAULT); + })); +} + ErrCode AdvancedNotificationService::PublishPreparedNotification( const sptr &request, const sptr &bundleOption) { @@ -470,6 +529,9 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( return; } UpdateRecentNotification(record->notification, false, 0); + if (record->request->IsTapDismissed()) { + StartAutoDelete(); + } sptr sortingMap = GenerateSortingMap(); ReportInfoToResourceSchedule(request->GetCreatorUserId(), bundleOption->GetBundleName()); NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index 1b20cb165f09d0d4956d6fd3d44895e131f74bd4..0a7c967bbcc04ee8423a68ead22308b1e3831d59 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -349,6 +349,7 @@ void NotificationSubscriberManager::NotifyCanceledInner( (record->userId == recvUserId) || IsSystemUser(record->userId) || // Delete this, When the systemui subscribe carry the user ID. IsSystemUser(sendUserId))) { + ANS_LOGD("%{public}s record->userId = <%{public}d>", __FUNCTION__, record->userId); record->subscriber->OnCanceled(notification, notificationMap, deleteReason); } } diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index b63872aabfaa7c19a0f2bf424927beb2af002cf9..5d36a84b452ed86848355e9664aebccb06b8559c 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -133,6 +133,7 @@ ohos_unittest("ans_unit_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] if (distributed_notification_supported) { @@ -361,6 +362,7 @@ ohos_unittest("notification_subscriber_manager_branch_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] if (distributed_notification_supported) { @@ -430,6 +432,7 @@ ohos_unittest("advanced_notification_service_branch_test") { "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] if (distributed_notification_supported) { diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index acdf7d314cc46ed665a5f59fee6edb570e19ca2d..18a1a8df7967ba792298ee6306967e13e89989db 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -3350,5 +3350,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 : #I61RF2 + */ +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(time(nullptr) + 5); + 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(); + + std::this_thread::sleep_for(std::chrono::seconds(5)); + EXPECT_EQ(advancedNotificationService_->notificationList_.size(), 0); + GTEST_LOG_(INFO) << "AdvancedNotificationServiceTest_221000 test end"; +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index 55ed2db7a4c87e830f228e0dfd310dd63de98bdb..f778ee4fb5ebdd04a623fd881a4e14eb7cc447a2 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -60,6 +60,7 @@ ohos_moduletest("ans_module_test") { "${core_path}:ans_core", "${frameworks_module_ans_path}:ans_innerkits", "${services_path}/ans:libans", + "//third_party/libuv:uv", ] external_deps = [ diff --git a/test/bechmarktest/notification_services_test/BUILD.gn b/test/bechmarktest/notification_services_test/BUILD.gn index 2d9f0f5ae01541c341de3d0fa1f9cf4c1b49ef84..db32fa0204dcc5c9a1b250ba1bb3b8a3d06f354f 100644 --- a/test/bechmarktest/notification_services_test/BUILD.gn +++ b/test/bechmarktest/notification_services_test/BUILD.gn @@ -36,6 +36,7 @@ ohos_benchmarktest("Benchmark_Notification_Servece_Test") { "${services_path}/ans:libans", "//third_party/benchmark:benchmark", "//third_party/googletest:gtest_main", + "//third_party/libuv:uv", ] external_deps = [