diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index dcd72551009d1133e9938937aa813640219517cd..a32be0f0675bf1ac1ffeb94efc0b72639d29cc2e 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -904,6 +904,7 @@ public: void OnResourceRemove(int32_t userId); void OnUserStopped(int32_t userId); void OnBundleDataCleared(const sptr &bundleOption); + void DeleteAllByUserStopped(int32_t userId); /** * @brief Obtains the event of bundle install. diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index a9faf05dd717c6dfe4f86fb12ac4594424c022cd..b4bcc19f50e9b620655b8cd442beb351a6baf6de 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1248,7 +1248,59 @@ void AdvancedNotificationService::OnUserRemoved(const int32_t &userId) void AdvancedNotificationService::OnUserStopped(int32_t userId) { - DeleteAllByUserInner(userId, NotificationConstant::USER_LOGOUT_REASON_DELETE, true, true); + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return; + } + + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([=]() { + DeleteAllByUserStopped(userId); + })); +} + +void AdvancedNotificationService::DeleteAllByUserStopped(int32_t userId) +{ + std::vector keys = GetNotificationKeys(nullptr); + std::vector> notifications; + std::vector timerIds; + for (auto key : keys) { +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + std::string deviceId; + std::string bundleName; + GetDistributedInfo(key, deviceId, bundleName); +#endif + sptr notification = nullptr; + for (auto record : notificationList_) { + if ((record->notification->GetKey() == key) && + (record->notification->GetRecvUserId() == userId)) { + ProcForDeleteLiveView(record); + notification = record->notification; + notificationList_.remove(record); + break; + } + } + + if (notification == nullptr) { + continue; + } + if (notification->GetRecvUserId() == userId) { + UpdateRecentNotification(notification, true, NotificationConstant::USER_LOGOUT_REASON_DELETE); + notifications.emplace_back(notification); + timerIds.emplace_back(notification->GetAutoDeletedTimer()); +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + DoDistributedDelete(deviceId, bundleName, notification); +#endif + } + if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { + SendNotificationsOnCanceled(notifications, nullptr, NotificationConstant::USER_LOGOUT_REASON_DELETE); + } + } + + if (!notifications.empty()) { + NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled( + notifications, nullptr, NotificationConstant::USER_LOGOUT_REASON_DELETE); + } + BatchCancelTimer(timerIds); } ErrCode AdvancedNotificationService::DeleteAllByUser(int32_t userId)