diff --git a/frameworks/ans/core/common/include/ans_inner_errors.h b/frameworks/ans/core/common/include/ans_inner_errors.h index c205fea2ce02ef396896a23852fe077220311745..d8a4581b0ea10a88e188970d361257f88bdea9f4 100644 --- a/frameworks/ans/core/common/include/ans_inner_errors.h +++ b/frameworks/ans/core/common/include/ans_inner_errors.h @@ -72,6 +72,7 @@ enum ErrorCode : uint32_t { ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, ERR_ANS_DISTRIBUTED_OPERATION_FAILED, ERR_ANS_DISTRIBUTED_GET_INFO_FAILED, + ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED, }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/native/src/notification.cpp b/frameworks/ans/native/src/notification.cpp index 4b8e26125243f47f0c901a924423bf3469945dec..b94a72e22479edbd6300e2a329b7842397b5526c 100644 --- a/frameworks/ans/native/src/notification.cpp +++ b/frameworks/ans/native/src/notification.cpp @@ -161,6 +161,14 @@ pid_t Notification::GetPid() const return request_->GetCreatorPid(); } +bool Notification::IsUnremovable() const +{ + if (request_ == nullptr) { + return false; + } + return request_->IsUnremovable(); +} + std::vector Notification::GetVibrationStyle() const { return vibrationStyle_; diff --git a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp index dd1e6b12f306ffa881025055b35b048d37929102..ec094df775522b4b11f5a78ad63d2f3815e10e52 100644 --- a/frameworks/ans/test/moduletest/ans_fw_module_test.cpp +++ b/frameworks/ans/test/moduletest/ans_fw_module_test.cpp @@ -2451,7 +2451,7 @@ HWTEST_F(AnsFWModuleTest, ANS_Interface_MT_PulbishContinuousTask_07600, Function EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetLabel().c_str(), NOTIFICATION_LABEL_0); EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetId(), 0); - EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->IsRemoveAllowed(), false); + EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->IsUnremovable(), true); EXPECT_EQ(eventParser.GetOnConsumedReq()[0]->GetSourceType(), NotificationConstant::SourceType::TYPE_CONTINUOUS); EXPECT_EQ(NotificationHelper::UnSubscribeNotification(subscriber), ERR_OK); diff --git a/interfaces/innerkits/ans/native/include/notification.h b/interfaces/innerkits/ans/native/include/notification.h index a12b7f4a0c8326d0f8f1a83a04a3f8ad5b543589..a187b9534fdca6d046c4ec079a83cb49b84c097b 100644 --- a/interfaces/innerkits/ans/native/include/notification.h +++ b/interfaces/innerkits/ans/native/include/notification.h @@ -164,6 +164,12 @@ public: */ pid_t GetPid() const; + /** + * Checks whether this notification is unremovable. + * @return true if this notification is unremovable; returns false otherwise. + */ + bool IsUnremovable() const; + /** * @brief Obtains the vibration style for this notifications. if * diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index cb95e52d13f7fb8b2dfb24d56012b7cc2b27b0ca..11d9f816adfade7aea25a2059e99965f8a96ea0a 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1108,9 +1108,6 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList(const sptr ¬ification, bool isCancel) { for (auto record : notificationList_) { - if (!record->notification->IsRemoveAllowed()) { - continue; - } if ((record->bundleOption->GetBundleName() == bundleOption->GetBundleName()) && (record->bundleOption->GetUid() == bundleOption->GetUid()) && (record->notification->GetLabel() == label) && @@ -1119,8 +1116,8 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList(const sptrdeviceId.empty() #endif ) { - if (!isCancel && record->request->IsUnremovable()) { - return ERR_ANS_NOTIFICATION_IS_UNREMOVABLE; + if (!isCancel && !record->notification->IsRemoveAllowed()) { + return ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; } notification = record->notification; // delete or delete all, call the function @@ -1139,12 +1136,9 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList( const std::string &key, sptr ¬ification, bool isCancel) { for (auto record : notificationList_) { - if (!record->notification->IsRemoveAllowed() && isCancel) { - continue; - } if (record->notification->GetKey() == key) { - if (!isCancel && record->request->IsUnremovable()) { - return ERR_ANS_NOTIFICATION_IS_UNREMOVABLE; + if (!isCancel && !record->notification->IsRemoveAllowed()) { + return ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; } notification = record->notification; // delete or delete all, call the function @@ -1163,10 +1157,10 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationListForDeleteAll( const std::string &key, sptr ¬ification) { for (auto record : notificationList_) { - if (!record->notification->IsRemoveAllowed()) { - continue; - } if (record->notification->GetKey() == key) { + if (!record->notification->IsRemoveAllowed()) { + return ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; + } if (record->request->IsUnremovable()) { return ERR_ANS_NOTIFICATION_IS_UNREMOVABLE; } @@ -1509,14 +1503,13 @@ ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const spt if (result != ERR_OK) { return result; } - + request->SetUnremovable(true); std::shared_ptr record = std::make_shared(); record->request = request; record->bundleOption = bundleOption; record->notification = new Notification(request); if (record->notification != nullptr) { record->notification->SetSourceType(NotificationConstant::SourceType::TYPE_CONTINUOUS); - record->notification->SetRemoveAllowed(false); } handler_->PostSyncTask(std::bind([&]() { @@ -1969,8 +1962,8 @@ ErrCode AdvancedNotificationService::RemoveNotification( (record->deviceId.empty()) && #endif (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) { - if (record->request->IsUnremovable()) { - result = ERR_ANS_NOTIFICATION_IS_UNREMOVABLE; + if (!record->notification->IsRemoveAllowed()) { + result = ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; break; } #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED @@ -2152,6 +2145,9 @@ ErrCode AdvancedNotificationService::RemoveGroupByBundle( handler_->PostSyncTask(std::bind([&]() { std::vector> removeList; for (auto record : notificationList_) { + if (!record->notification->IsRemoveAllowed()) { + continue; + } if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && (record->bundleOption->GetUid() == bundle->GetUid()) && !record->request->IsUnremovable() && #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED