From d54cdbd0be9fe249f40af64939c6ad960ccc5e69 Mon Sep 17 00:00:00 2001 From: xijiandong Date: Mon, 16 Jun 2025 14:31:03 +0800 Subject: [PATCH] Adjust code to support push to use LocalWantAgent Signed-off-by: xijiandong --- .../js/napi/src/common_convert_request.cpp | 13 +++++++--- .../include/advanced_notification_service.h | 2 ++ .../advanced_notification_publish.cpp | 24 ++++++++++++++++--- ...nced_notification_service_publish_test.cpp | 22 +++++++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/frameworks/js/napi/src/common_convert_request.cpp b/frameworks/js/napi/src/common_convert_request.cpp index 75905fe08..26a06dba4 100644 --- a/frameworks/js/napi/src/common_convert_request.cpp +++ b/frameworks/js/napi/src/common_convert_request.cpp @@ -1266,9 +1266,16 @@ napi_value Common::GetNotificationRemovalWantAgent( } std::shared_ptr removeWantAgent = std::make_shared(*wantAgent); - if ((uint32_t)removeWantAgent->GetPendingWant()->GetType( - removeWantAgent->GetPendingWant()->GetTarget()) >= OPERATION_MAX_TYPE) { - request.SetRemovalWantAgent(removeWantAgent); + if (removeWantAgent->IsLocal()) { + const auto localPendingWant = removeWantAgent->GetLocalPendingWant(); + if (localPendingWant != nullptr && localPendingWant->GetType() >= OPERATION_MAX_TYPE) { + request.SetRemovalWantAgent(removeWantAgent); + } + } else { + if ((uint32_t)removeWantAgent->GetPendingWant()->GetType( + removeWantAgent->GetPendingWant()->GetTarget()) >= OPERATION_MAX_TYPE) { + request.SetRemovalWantAgent(removeWantAgent); + } } } diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 1263310fc..dcd725510 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1671,6 +1671,8 @@ private: void SetChainIdToExtraInfo(const sptr &request, OHOS::HiviewDFX::HiTraceId traceId); + ErrCode CheckNotificationRequest(const sptr &request); + private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp index 6ae602d31..108211931 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp @@ -52,9 +52,9 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt OHOS::HiviewDFX::HiTraceId traceId = OHOS::HiviewDFX::HiTraceChain::GetId(); ANS_LOGD("%{public}s", __FUNCTION__); - if (!request) { - ANSR_LOGE("ReminderRequest object is nullptr"); - return ERR_ANS_INVALID_PARAM; + const auto checkResult = CheckNotificationRequest(request); + if (checkResult != ERR_OK) { + return checkResult; } SetChainIdToExtraInfo(request, traceId); @@ -374,5 +374,23 @@ ErrCode AdvancedNotificationService::UpdateNotificationTimerByUid(const int32_t return ERR_OK; } +ErrCode AdvancedNotificationService::CheckNotificationRequest(const sptr &request) +{ + if (!request) { + ANSR_LOGE("ReminderRequest object is nullptr"); + return ERR_ANS_INVALID_PARAM; + } + + const auto wantAgent = request->GetWantAgent(); + const auto removalWantAgent = request->GetRemovalWantAgent(); + const auto isLocalWantAgent = (wantAgent != nullptr && wantAgent->IsLocal()) || + (removalWantAgent != nullptr && removalWantAgent->IsLocal()); + if (isLocalWantAgent && !AccessTokenHelper::IsSystemApp()) { + ANSR_LOGE("ReminderRequest local wantAgent does not support non system app"); + return ERR_ANS_NON_SYSTEM_APP; + } + return ERR_OK; +} + } // Notification } // OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp index 80701d5e4..552dc8f10 100644 --- a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp @@ -582,6 +582,28 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01300, SleepForFC(); } +/** + * @tc.number : ANS_Publish_01400 + * @tc.name : ANSPublish01400 + * @tc.desc : When an non system app wants to publish an notification with local wantAgent, the + * notification publishing interface returns ERR_ANS_NON_SYSTEM_APP; + */ +HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01400, Function | SmallTest | Level1) +{ + TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + sptr req = new NotificationRequest(); + EXPECT_NE(req, nullptr); + std::shared_ptr want = std::make_shared(); + std::shared_ptr localPendingWant = + std::make_shared("TestBundleName", want, 0); + std::shared_ptr wantAgent = + std::make_shared(localPendingWant); + req->SetWantAgent(wantAgent); + MockIsSystemApp(false); + std::string label = "publish's label"; + ASSERT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NON_SYSTEM_APP); + SleepForFC(); +} /** * @tc.number : AdvancedNotificationServiceTest_04600 -- Gitee