From ba51ec985be534c6cc385910dacfe1207ec716f0 Mon Sep 17 00:00:00 2001 From: wangsen1994 Date: Fri, 4 Jul 2025 10:32:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8D=8F=E5=90=8C=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangsen1994 --- frameworks/ans/src/notification_request.cpp | 62 ++++++++++++++++++- interfaces/inner_api/notification_request.h | 4 ++ .../advanced_notification_publish_service.cpp | 13 +++- .../distributed/include/tlv_box/request_box.h | 2 + .../distributed/include/tlv_box/tlv_box.h | 1 + .../distributed_publish_service_v2.cpp | 20 ++++-- .../distributed/src/tlv_box/request_box.cpp | 17 +++++ 7 files changed, 110 insertions(+), 9 deletions(-) diff --git a/frameworks/ans/src/notification_request.cpp b/frameworks/ans/src/notification_request.cpp index 41124e86d..9b3ade1c9 100644 --- a/frameworks/ans/src/notification_request.cpp +++ b/frameworks/ans/src/notification_request.cpp @@ -840,6 +840,62 @@ std::string NotificationRequest::Dump() (unifiedGroupInfo_ ? unifiedGroupInfo_->Dump() : "null")+ " }"; } +std::string NotificationRequest::CollaborationToJson() const +{ + nlohmann::json jsonObject; + jsonObject["id"] = notificationId_; + jsonObject["autoDeletedTime"] = autoDeletedTime_; + + jsonObject["groupName"] = groupName_; + jsonObject["label"] = label_; + jsonObject["classification"] = classification_; + jsonObject["isRemoveAllowed"] = isRemoveAllowed_; + + jsonObject["tapDismissed"] = tapDismissed_; + jsonObject["isOngoing"] = inProgress_; + jsonObject["isAlertOnce"] = alertOneTime_; + jsonObject["isUnremovable"] = unremovable_; + + jsonObject["creatorBundleName"] = GetOwnerBundleName(); + jsonObject["creatorUid"] = GetOwnerUid(); + jsonObject["creatorPid"] = GetCreatorPid(); + jsonObject["creatorInstanceKey"] = creatorInstanceKey_; + jsonObject["appInstanceKey"] = appInstanceKey_; + jsonObject["notificationControlFlags"] = notificationControlFlags_; + + if (agentBundle_ != nullptr) { + nlohmann::json bundleOptionObj; + if (!NotificationJsonConverter::ConvertToJson(agentBundle_.get(), bundleOptionObj)) { + ANS_LOGE("Cannot convert agentBundle to JSON."); + return std::string(); + } + jsonObject["agentBundle"] = bundleOptionObj; + } + + return jsonObject.dump(); +} + +NotificationRequest *NotificationRequest::CollaborationFromJson(const std::string& basicInfo) +{ + nlohmann::json jsonObject = nlohmann::json::parse(basicInfo, nullptr, false); + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + auto pRequest = new (std::nothrow) NotificationRequest(); + if (pRequest == nullptr) { + ANS_LOGE("null pRequest"); + return nullptr; + } + + ConvertJsonToNum(pRequest, jsonObject); + ConvertJsonToString(pRequest, jsonObject); + ConvertJsonToBool(pRequest, jsonObject); + ConvertJsonToAgentBundle(pRequest, jsonObject); + return pRequest; +} + bool NotificationRequest::ToJson(nlohmann::json &jsonObject) const { jsonObject["version"] = 1; @@ -2416,6 +2472,10 @@ void NotificationRequest::ConvertJsonToBool(NotificationRequest *target, const n target->distributedCollaborate_ = jsonObject.at("distributedCollaborate").get(); } + if (jsonObject.find("isRemoveAllowed") != jsonEnd && jsonObject.at("isRemoveAllowed").is_boolean()) { + target->isRemoveAllowed_ = jsonObject.at("isRemoveAllowed").get(); + } + ConvertJsonToBoolExt(target, jsonObject); } @@ -2762,7 +2822,7 @@ std::string NotificationRequest::GetBaseKey(const std::string &deviceId) if (distributedCollaborate_) { ANS_LOGI("NotificationRequest use collaborate!"); - return label_ + distributedHashCode_; + return "ans_distributed" + distributedHashCode_; } std::stringstream stream; diff --git a/interfaces/inner_api/notification_request.h b/interfaces/inner_api/notification_request.h index 4de356162..cc7c290f2 100644 --- a/interfaces/inner_api/notification_request.h +++ b/interfaces/inner_api/notification_request.h @@ -1491,6 +1491,10 @@ public: uint32_t GetCollaboratedReminderFlag() const; + std::string CollaborationToJson() const; + + static NotificationRequest *CollaborationFromJson(const std::string& basicInfo); + private: /** * Indicates the color mask, used for calculation with the ARGB value set by setColor(int32_t). diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index ddc7c5896..876b4f5d7 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -162,7 +162,9 @@ ErrCode AdvancedNotificationService::SetCollaborateRequest(const sptrSetCreatorUid(uid); - request->SetCreatorPid(pid); + if (request->GetCreatorPid() == 0) { + request->SetCreatorPid(pid); + } if (request->GetOwnerUid() == DEFAULT_UID) { request->SetOwnerUid(uid); } @@ -238,13 +240,20 @@ ErrCode AdvancedNotificationService::CollaboratePublish(const sptr record = std::make_shared(); SetCollaborateRequest(request); record->request = request; + sptr slot = new (std::nothrow) NotificationSlot(request->GetSlotType()); + if (slot == nullptr) { + ANS_LOGE("Failed to create NotificationSlot instance"); + return ERR_NO_MEMORY; + } + slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); + record->slot = slot; record->notification = new (std::nothrow) Notification(request); if (record->notification == nullptr) { ANS_LOGE("Failed to create notification"); return ERR_ANS_NO_MEMORY; } record->bundleOption = new (std::nothrow) NotificationBundleOption(request->GetCreatorBundleName(), 0); - record->notification->SetKey(request->GetLabel() + request->GetDistributedHashCode()); + record->notification->SetKey("ans_distributed" + request->GetDistributedHashCode()); if (CollaborateFilter(request) != ERR_OK) { return ERR_ANS_NOT_ALLOWED; } diff --git a/services/distributed/include/tlv_box/request_box.h b/services/distributed/include/tlv_box/request_box.h index 83a777e77..c9c0f7486 100644 --- a/services/distributed/include/tlv_box/request_box.h +++ b/services/distributed/include/tlv_box/request_box.h @@ -59,6 +59,7 @@ public: bool SetDeviceId(const std::string& deviceId); bool SetActionButtonsLength(const int32_t length); bool SetActionButtonsTitle(const std::vector& buttonsTitle); + bool SetNotificationBasicInfo(const std::string& basicInfo); #else bool GetNotificationHashCode(std::string& hasdCode) const; bool GetSlotType(int32_t& type) const; @@ -89,6 +90,7 @@ public: bool GetDeviceId(std::string& deviceId) const; bool GetActionButtonsLength(int32_t& length) const; bool GetActionButtonsTitle(std::vector& buttonsTitle) const; + bool GetNotificationBasicInfo(std::string& basicInfo) const; #endif }; } // namespace Notification diff --git a/services/distributed/include/tlv_box/tlv_box.h b/services/distributed/include/tlv_box/tlv_box.h index 33ecc9ced..8ea25aa84 100644 --- a/services/distributed/include/tlv_box/tlv_box.h +++ b/services/distributed/include/tlv_box/tlv_box.h @@ -66,6 +66,7 @@ enum TlvType : int32_t { NOTIFICATION_APP_MESSAGE_ID = 24, NOTIFICATION_EXTENDINFO = 25, NOTIFICATION_RECEIVE_USERID = 26, + NOTIFICATION_BASIC_INFO = 985, LOCAL_DEVICE_USERID = 986, LIVEVIEW_SYNC_ENABLE = 987, NOTIFICATION_SYNC_ENABLE = 988, diff --git a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp index da6a309e1..81cd54455 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp @@ -375,17 +375,18 @@ void DistributedPublishService::SendNotifictionRequest(const std::shared_ptrGetKey().c_str(), requestPoint->GetFlags() == nullptr ? "null" : requestPoint->GetFlags()->Dump().c_str()); auto local = DistributedDeviceService::GetInstance().GetLocalDevice(); + if (peerDevice.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH) { + requestBox->SetNotificationBasicInfo(requestPoint->CollaborationToJson()); + } requestBox->SetDeviceId(local.deviceId_); requestBox->SetAutoDeleteTime(requestPoint->GetAutoDeletedTime()); requestBox->SetFinishTime(requestPoint->GetFinishDeadLine()); requestBox->SetNotificationHashCode(request->GetKey()); requestBox->SetSlotType(static_cast(requestPoint->GetSlotType())); requestBox->SetContentType(static_cast(requestPoint->GetNotificationType())); - if (isSyncNotification) { - requestBox->SetReminderFlag(0); - } else { - requestBox->SetReminderFlag(requestPoint->GetFlags()->GetReminderFlags()); - } + + int32_t reminderFlag = isSyncNotification ? 0 : requestPoint->GetFlags()->GetReminderFlags(); + requestBox->SetReminderFlag(reminderFlag); if (!requestPoint->GetAppMessageId().empty()) { requestBox->SetAppMessageId(requestPoint->GetAppMessageId()); } @@ -574,6 +575,14 @@ void DistributedPublishService::PublishNotification(const std::shared_ptrSetDistributedHashCode(context); } request->SetDistributedCollaborate(true); - request->SetLabel(DISTRIBUTED_LABEL); } void DistributedPublishService::MakeExtendInfo(const NotificationRequestBox& box, diff --git a/services/distributed/src/tlv_box/request_box.cpp b/services/distributed/src/tlv_box/request_box.cpp index ef64c6eac..3c116ad5d 100644 --- a/services/distributed/src/tlv_box/request_box.cpp +++ b/services/distributed/src/tlv_box/request_box.cpp @@ -323,6 +323,14 @@ bool NotificationRequestBox::SetActionButtonsTitle(const std::vectorPutValue(std::make_shared(NOTIFICATION_BASIC_INFO, basicInfo)); + return true; +} #else bool NotificationRequestBox::GetNotificationHashCode(std::string& hasdCode) const { @@ -586,6 +594,15 @@ bool NotificationRequestBox::GetActionButtonsTitle(std::vector& but } return true; } + +bool NotificationRequestBox::GetNotificationBasicInfo(std::string& basicInfo) const +{ + if (box_ == nullptr) { + return false; + } + box_->GetStringValue(NOTIFICATION_BASIC_INFO, basicInfo); + return true; +} #endif } } -- Gitee