From 31ecc6d160911071d984f1405a2ae92c3d455da4 Mon Sep 17 00:00:00 2001 From: LYQ_YES Date: Thu, 15 Aug 2024 21:59:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A1=A5=E9=BD=90NotificationRequest=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LYQ_YES Change-Id: I22f7b0272eb796856f5ff32612e200a3655294e9 --- .../cj/ffi/include/notification_utils.h | 14 +++ frameworks/cj/ffi/src/notification_utils.cpp | 101 +++++++++++++++++- 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/frameworks/cj/ffi/include/notification_utils.h b/frameworks/cj/ffi/include/notification_utils.h index 9461cc2a9..8aafcba21 100644 --- a/frameworks/cj/ffi/include/notification_utils.h +++ b/frameworks/cj/ffi/include/notification_utils.h @@ -35,6 +35,8 @@ #include "notification_progress.h" #include "notification_time.h" #include "pixel_map_impl.h" +#include "want_agent_helper.h" +#include "want_params_wrapper.h" #include "ans_notification.h" #include "singleton.h" @@ -173,6 +175,11 @@ extern "C" { int32_t vibrationEnabled = 0; }; + struct CNotificationTemplate { + char* name; + char* data; + }; + struct CNotificationRequest { CNotificationContent notificationContent; int32_t id; @@ -182,6 +189,8 @@ extern "C" { int64_t deliveryTime; bool tapDismissed; int64_t autoDeletedTime; + // wantAgent + char* extraInfo; uint32_t color; bool colorEnabled; bool isAlertOnce; @@ -191,6 +200,7 @@ extern "C" { char* label; int32_t badgeIconStyle; bool showDeliveryTime; + // actionButtons int64_t smallIcon; int64_t largeIcon; char* creatorBundleName; @@ -199,8 +209,10 @@ extern "C" { int32_t creatorUserId; char* hashCode; char* groupName; + CNotificationTemplate template_; CDistributedOptions* distributedOption; CNotificationFlags notificationFlags; + // removalWantAgent uint32_t badgeNumber; char* appMessageId; }; @@ -257,6 +269,7 @@ bool GetNotificationRequestDistributedOptions( bool GetNotificationRequestByNumber(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request); bool GetNotificationRequestByString(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request); bool GetNotificationRequestByBool(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request); +bool GetNotificationTemplate(CNotificationTemplate &template_, OHOS::Notification::NotificationRequest &request); bool GetNotificationRequestByCustom(CNotificationRequest cjRequest, OHOS::Notification::NotificationRequest &request); bool GetNotificationBasicContentDetailed(CNotificationBasicContent* contentResult, std::shared_ptr basicContent); @@ -292,6 +305,7 @@ bool SlotLevelCToCJ(const OHOS::Notification::NotificationSlot::NotificationLeve bool ContentTypeCJToC(const ContentType &inType, OHOS::Notification::NotificationContent::Type &outType); bool ContentTypeCToCJ(const OHOS::Notification::NotificationContent::Type &inType, ContentType &outType); bool GetNotificationSlotType(int32_t slotType, OHOS::Notification::NotificationRequest &request); +bool GetNotificationExtraInfo(const char* extraInfo, OHOS::Notification::NotificationRequest &request); bool GetNotificationContent(CNotificationContent &content, OHOS::Notification::NotificationRequest &request); bool GetNotificationSmallIcon(int64_t smallIcon, OHOS::Notification::NotificationRequest &request); bool GetNotificationLargeIcon(int64_t largeIcon, OHOS::Notification::NotificationRequest &request); diff --git a/frameworks/cj/ffi/src/notification_utils.cpp b/frameworks/cj/ffi/src/notification_utils.cpp index 9be5c21f8..9d9e0bb0f 100644 --- a/frameworks/cj/ffi/src/notification_utils.cpp +++ b/frameworks/cj/ffi/src/notification_utils.cpp @@ -150,7 +150,7 @@ namespace Notification { } request.SetGroupName(std::string(groupName)); - // groupName?: string + // appMessageId?: string char appMessageId[STR_MAX_SIZE] = {0}; if (strcpy_s(appMessageId, STR_MAX_SIZE, cjRequest.appMessageId) != EOK) { return false; @@ -197,6 +197,41 @@ namespace Notification { return true; } + bool GetNotificationTemplate(CNotificationTemplate &template_, NotificationRequest &request) + { + if (template_.name == nullptr) { + return true; + } + std::shared_ptr templ = std::make_shared(); + if (templ == nullptr) { + LOGE("template is null"); + return false; + } + + // name: string + char name[STR_MAX_SIZE] = {0}; + if (strcpy_s(name, STR_MAX_SIZE, template_.name) != EOK) { + return false; + } + templ->SetTemplateName(std::string(name)); + + // data?: {[key: string]: object} + if (template_.data == nullptr) { + LOGE("data is required."); + return false; + } + std::string dataStr(template_.data); + if (dataStr.empty()) { + LOGE("data is empty."); + return false; + } + + AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(dataStr); + templ->SetTemplateData(std::make_shared(params)); + request.SetTemplate(templ); + return true; + } + bool GetNotificationRequestByCustom(CNotificationRequest cjRequest, NotificationRequest &request) { // content: NotificationContent @@ -207,6 +242,10 @@ namespace Notification { if (!GetNotificationSlotType(cjRequest.notificationSlotType, request)) { return false; } + // extraInfo?: {[key: string]: any} + if (!GetNotificationExtraInfo(cjRequest.extraInfo, request)) { + return false; + } // smallIcon?: image.PixelMap if (!GetNotificationSmallIcon(cjRequest.smallIcon, request)) { return false; @@ -220,6 +259,11 @@ namespace Notification { return false; } + // template?: NotificationTemplate + if (!GetNotificationTemplate(cjRequest.template_, request)) { + return false; + } + return true; } @@ -805,6 +849,21 @@ namespace Notification { return true; } + bool GetNotificationExtraInfo(const char* extraInfo, NotificationRequest &request) + { + if (extraInfo == nullptr) { + LOGE("extraInfo is null"); + return false; + } + + std::string extraInfoStr(extraInfo); + if (!extraInfoStr.empty()) { + AAFwk::WantParams params = AAFwk::WantParamWrapper::ParseWantParams(extraInfoStr); + request.SetAdditionalData(std::make_shared(params)); + } + return true; + } + bool GetNotificationSmallIcon(int64_t smallIcon, NotificationRequest &request) { if (smallIcon != -1) { @@ -1520,6 +1579,23 @@ namespace Notification { return true; } + bool SetNotificationTemplateInfo(const std::shared_ptr &templ, + CNotificationTemplate &template_) + { + if (templ == nullptr) { + LOGE("templ is null"); + return false; + } + template_.name = MallocCString(templ->GetTemplateName()); + std::shared_ptr data = templ->GetTemplateData(); + if (data) { + AAFwk::WantParamWrapper wWrapper(*data); + std::string dataStr = wWrapper.ToString(); + template_.data = MallocCString(dataStr); + } + return true; + } + bool SetNotificationFlags( const std::shared_ptr &flags, CNotificationFlags ¬ificationFlags) @@ -1548,6 +1624,23 @@ namespace Notification { return false; } + // extraInfo?: {[key:string] : any} + std::shared_ptr additionalData = request->GetAdditionalData(); + if (additionalData) { + AAFwk::WantParamWrapper wWrapper(*additionalData); + std::string extraInfoStr = wWrapper.ToString(); + notificationRequest.extraInfo = MallocCString(extraInfoStr); + } + + // template?: NotificationTemplate + std::shared_ptr templ = request->GetTemplate(); + if (templ) { + if (!SetNotificationTemplateInfo(templ, notificationRequest.template_)) { + LOGE("SetNotificationTemplate call failed"); + return false; + } + } + // readonly notificationFlags?: NotificationFlags std::shared_ptr flags = request->GetFlags(); if (flags) { @@ -1561,6 +1654,7 @@ namespace Notification { static void InitNotificationRequest(CNotificationRequest ¬ificationRequest) { + // 避免野指针 notificationRequest.notificationContent = { .notificationContentType = 0, .normal = nullptr, @@ -1568,12 +1662,17 @@ namespace Notification { .multiLine = nullptr, .picture = nullptr }; + notificationRequest.template_ = { + .name = nullptr, + .data = nullptr + }; notificationRequest.label = nullptr; notificationRequest.creatorBundleName = nullptr; notificationRequest.groupName = nullptr; notificationRequest.distributedOption = nullptr; notificationRequest.hashCode = nullptr; notificationRequest.appMessageId = nullptr; + notificationRequest.extraInfo = nullptr; } bool SetNotificationRequest( -- Gitee From 71e0136fcb7438ea9a7733fec91547cd2b3c6b11 Mon Sep 17 00:00:00 2001 From: LYQ_YES Date: Tue, 17 Dec 2024 19:23:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LYQ_YES Change-Id: Ib55cc07a9e844520701ad306102f71b47854dffc Signed-off-by: LYQ_YES --- frameworks/cj/ffi/src/notification_utils.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/frameworks/cj/ffi/src/notification_utils.cpp b/frameworks/cj/ffi/src/notification_utils.cpp index 9d9e0bb0f..1121b8c25 100644 --- a/frameworks/cj/ffi/src/notification_utils.cpp +++ b/frameworks/cj/ffi/src/notification_utils.cpp @@ -1387,7 +1387,12 @@ namespace Notification { // buttonIcons: Array int iconCount = 0; std::vector> iconsVec = button.GetAllButtonIcons(); - CArrI64 icons = { .head = nullptr, .size = iconsVec.size() }; + CArrI64 icons = { .head = nullptr, .size = 0 }; + icons.head = static_cast(malloc(sizeof(int64_t) * iconsVec.size())); + if (icons.head == nullptr) { + LOGE("NotificationButton icons malloc failed"); + return false; + } for (auto vec : iconsVec) { if (!vec) { continue; @@ -1400,6 +1405,7 @@ namespace Notification { } icons.head[iconCount++] = native->GetID(); } + icons.size = static_cast(iconsVec.size()); cButton.icons = icons; return true; } -- Gitee