diff --git a/frameworks/ans/core/BUILD.gn b/frameworks/ans/core/BUILD.gn index e2ba42989e54c9aee761543abe0a9dd481289008..cf945217ad29e7dc9bda1b4c38c53229d8b5442d 100644 --- a/frameworks/ans/core/BUILD.gn +++ b/frameworks/ans/core/BUILD.gn @@ -67,6 +67,7 @@ ohos_shared_library("ans_core") { "${frameworks_path}/ans/native/src/notification_sorting_map.cpp", "${frameworks_path}/ans/native/src/notification_subscribe_info.cpp", "${frameworks_path}/ans/native/src/notification_subscriber.cpp", + "${frameworks_path}/ans/native/src/notification_template.cpp", "${frameworks_path}/ans/native/src/notification_user_input.cpp", "${frameworks_path}/ans/native/src/reminder_helper.cpp", "${frameworks_path}/ans/native/src/reminder_request.cpp", diff --git a/frameworks/ans/core/common/include/ans_const_define.h b/frameworks/ans/core/common/include/ans_const_define.h index 8568a2b3efdb9f616bd8768384b643cbf76ec3e6..e066999762eb827cbf31a7a195aa743c0ce55eb2 100644 --- a/frameworks/ans/core/common/include/ans_const_define.h +++ b/frameworks/ans/core/common/include/ans_const_define.h @@ -37,6 +37,9 @@ constexpr uint32_t SYSTEM_SERVICE_UID = 1000; const static Uri DEFAULT_NOTIFICATION_SOUND("file://system/etc/Light.ogg"); const static std::vector DEFAULT_NOTIFICATION_VIBRATION = {200}; +// Default path for template +const static std::string DEFAULT_TEMPLATE_PATH("/system/etc/external.json"); + } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/common/include/ans_inner_errors.h b/frameworks/ans/core/common/include/ans_inner_errors.h index 8e59ee723c4d6cca6045bf428220ab38428e78e3..63d35c83fc3bf07d2a8a3ba12eda12b52e13b109 100644 --- a/frameworks/ans/core/common/include/ans_inner_errors.h +++ b/frameworks/ans/core/common/include/ans_inner_errors.h @@ -69,6 +69,7 @@ enum ErrorCode : uint32_t { ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_NOT_EXIST, ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_ID_INVALID, ERR_ANS_PREFERENCES_NOTIFICATION_SLOTGROUP_EXCEED_MAX_NUM, + ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/include/ans_manager_interface.h b/frameworks/ans/core/include/ans_manager_interface.h index 9657c2b34ed010cedc43328b5648c2a3ef3a87bd..f43d0df46fe4381aaf362a2167bc75d0f4df81fd 100644 --- a/frameworks/ans/core/include/ans_manager_interface.h +++ b/frameworks/ans/core/include/ans_manager_interface.h @@ -118,6 +118,7 @@ public: virtual ErrCode CancelReminder(const int32_t reminderId) = 0; virtual ErrCode GetValidReminders(std::vector> &reminders) = 0; virtual ErrCode CancelAllReminders() = 0; + virtual ErrCode IsSupportTemplate(const std::string &templateName, bool &support) = 0; protected: enum TransactId : uint32_t { @@ -180,7 +181,8 @@ protected: PUBLISH_REMINDER, CANCEL_REMINDER, CANCEL_ALL_REMINDERS, - GET_ALL_VALID_REMINDERS + GET_ALL_VALID_REMINDERS, + IS_SUPPORT_TEMPLATE }; }; } // namespace Notification diff --git a/frameworks/ans/core/include/ans_manager_proxy.h b/frameworks/ans/core/include/ans_manager_proxy.h index d5bd993e1897726d320e7ded3c2dc70a0c1b66e6..0c58d0b8489107f8fde0e5845d76f743aafc9931 100644 --- a/frameworks/ans/core/include/ans_manager_proxy.h +++ b/frameworks/ans/core/include/ans_manager_proxy.h @@ -101,6 +101,7 @@ public: ErrCode ShellDump(const std::string &dumpOption, std::vector &dumpInfo) override; ErrCode PublishContinuousTaskNotification(const sptr &request) override; ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId) override; + ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; ErrCode PublishReminder(sptr &reminder) override; ErrCode CancelReminder(const int32_t reminderId) override; diff --git a/frameworks/ans/core/include/ans_manager_stub.h b/frameworks/ans/core/include/ans_manager_stub.h index 5366ed95044388ce7df63b8eb4d8d42b7047f116..0316ea1bd3ff31127fec68b81fe8196118d14d2c 100644 --- a/frameworks/ans/core/include/ans_manager_stub.h +++ b/frameworks/ans/core/include/ans_manager_stub.h @@ -110,6 +110,7 @@ public: virtual ErrCode CancelReminder(const int32_t reminderId) override; virtual ErrCode GetValidReminders(std::vector> &reminders) override; virtual ErrCode CancelAllReminders() override; + virtual ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; private: static const std::map> @@ -175,6 +176,7 @@ private: ErrCode HandleCancelReminder(MessageParcel &data, MessageParcel &reply); ErrCode HandleGetValidReminders(MessageParcel &data, MessageParcel &reply); ErrCode HandleCancelAllReminders(MessageParcel &data, MessageParcel &reply); + ErrCode HandleIsSupportTemplate(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result); diff --git a/frameworks/ans/core/include/ans_notification.h b/frameworks/ans/core/include/ans_notification.h index bb5480207904dc003314d39cee0162b1d58c83b3..7ea2f0d6d8efbf3d82465cd79cf45855e9beb79e 100644 --- a/frameworks/ans/core/include/ans_notification.h +++ b/frameworks/ans/core/include/ans_notification.h @@ -629,6 +629,14 @@ public: */ ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId); + /** + * Obtains whether the template is supported by the system. + * + * @param support whether is it a system supported template. + * @return Returns check result. + */ + ErrCode IsSupportTemplate(const std::string &templateName, bool &support); + /** * Reset ans manager proxy when OnRemoteDied called. */ diff --git a/frameworks/ans/core/src/ans_manager_proxy.cpp b/frameworks/ans/core/src/ans_manager_proxy.cpp index 9614eb9785174e4cdeb9d6d9504f49011a9969ec..e06b9e28f590f91c8b4bb3a1d4a5fe6316d0e29f 100644 --- a/frameworks/ans/core/src/ans_manager_proxy.cpp +++ b/frameworks/ans/core/src/ans_manager_proxy.cpp @@ -2151,5 +2151,39 @@ bool AnsManagerProxy::ReadParcelableVector(std::vector> &parcelableInfos return true; } + +ErrCode AnsManagerProxy::IsSupportTemplate(const std::string &templateName, bool &support) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[IsSupportTemplate] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(templateName)) { + ANS_LOGW("[IsSupportTemplate] fail: write template name failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(IS_SUPPORT_TEMPLATE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[IsSupportTemplate] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[IsSupportTemplate] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!reply.ReadBool(support)) { + ANS_LOGW("[IsSupportTemplate] fail: read support failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_manager_stub.cpp b/frameworks/ans/core/src/ans_manager_stub.cpp index b6352240e0088e30081a0a2f0365fc445880e86e..5344287790fb5299d8ffc333d288c254958cb0f6 100644 --- a/frameworks/ans/core/src/ans_manager_stub.cpp +++ b/frameworks/ans/core/src/ans_manager_stub.cpp @@ -210,6 +210,10 @@ const std::map> &parcelableInfos, return true; } +ErrCode AnsManagerStub::HandleIsSupportTemplate(MessageParcel &data, MessageParcel &reply) +{ + std::string templateName; + if (!data.ReadString(templateName)) { + ANS_LOGW("[HandleIsSupportTemplate] fail: read template name failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool support = false; + ErrCode result = IsSupportTemplate(templateName, support); + + if (!reply.WriteInt32(result)) { + ANS_LOGW("[HandleIsSupportTemplate] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!reply.WriteBool(support)) { + ANS_LOGW("[HandleIsSupportTemplate] fail: write support failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::Publish(const std::string &label, const sptr ¬ification) { ANS_LOGW("AnsManagerStub::Publish called!"); @@ -1733,6 +1760,7 @@ ErrCode AnsManagerStub::CancelContinuousTaskNotification(const std::string &labe ANS_LOGW("AnsManagerStub::CancelContinuousTaskNotification called!"); return ERR_INVALID_OPERATION; } + ErrCode AnsManagerStub::PublishReminder(sptr &reminder) { ANS_LOGW("AnsManagerStub::PublishReminder called!"); @@ -1756,5 +1784,11 @@ ErrCode AnsManagerStub::CancelAllReminders() ANS_LOGW("AnsManagerStub::cancelAllReminders called!"); return ERR_INVALID_OPERATION; } + +ErrCode AnsManagerStub::IsSupportTemplate(const std::string &templateName, bool &support) +{ + ANS_LOGW("AnsManagerStub::IsSupportTemplate called!"); + return ERR_INVALID_OPERATION; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/src/ans_notification.cpp b/frameworks/ans/core/src/ans_notification.cpp index 9f2214e90065f6b957171c559f25ed1ea4df8a43..8087f3b110c0ef6cc20a6ab200ce50f103015a16 100644 --- a/frameworks/ans/core/src/ans_notification.cpp +++ b/frameworks/ans/core/src/ans_notification.cpp @@ -1099,5 +1099,15 @@ ErrCode AnsNotification::CheckImageSize(const NotificationRequest &request) return ERR_OK; } + +ErrCode AnsNotification::IsSupportTemplate(const std::string &templateName, bool &support) +{ + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->IsSupportTemplate(templateName, support); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/native/BUILD.gn b/frameworks/ans/native/BUILD.gn index f83983bd09cffb689e0c29953051ccda8a992ea5..3909036248de51943e8657028e428f608457dfde 100644 --- a/frameworks/ans/native/BUILD.gn +++ b/frameworks/ans/native/BUILD.gn @@ -62,6 +62,7 @@ ohos_shared_library("ans_innerkits") { "src/notification_sorting_map.cpp", "src/notification_subscribe_info.cpp", "src/notification_subscriber.cpp", + "src/notification_template.cpp", "src/notification_user_input.cpp", ] diff --git a/frameworks/ans/native/src/notification_helper.cpp b/frameworks/ans/native/src/notification_helper.cpp index 038e5f916b8e23c7bff463c203988100cebe05ae..7f0bc3619ef4f61cfc897b6f18716b11d9ca2606 100644 --- a/frameworks/ans/native/src/notification_helper.cpp +++ b/frameworks/ans/native/src/notification_helper.cpp @@ -331,5 +331,10 @@ ErrCode NotificationHelper::CancelContinuousTaskNotification(const std::string & { return DelayedSingleton::GetInstance()->CancelContinuousTaskNotification(label, notificationId); } + +ErrCode NotificationHelper::IsSupportTemplate(const std::string &templateName, bool &support) +{ + return DelayedSingleton::GetInstance()->IsSupportTemplate(templateName, support); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/native/src/notification_long_text_content.cpp b/frameworks/ans/native/src/notification_long_text_content.cpp index fb305300d88842536a82c8f1c1b0c67c1da9324e..0e242feee4ceb9b0e14be01d4338ddb2018b0a23 100644 --- a/frameworks/ans/native/src/notification_long_text_content.cpp +++ b/frameworks/ans/native/src/notification_long_text_content.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "ans_log_wrapper.h" #include "notification_long_text_content.h" +#include "ans_log_wrapper.h" + namespace OHOS { namespace Notification { const std::size_t NotificationLongTextContent::MAX_LONGTEXT_LENGTH {1024}; diff --git a/frameworks/ans/native/src/notification_media_content.cpp b/frameworks/ans/native/src/notification_media_content.cpp index e1c303d8939bafb3bdcad8d45ce87e5ea34c277b..6c3862c5093989afd79c0a7d74b524b3e8504ccd 100644 --- a/frameworks/ans/native/src/notification_media_content.cpp +++ b/frameworks/ans/native/src/notification_media_content.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "ans_log_wrapper.h" #include "notification_media_content.h" +#include "ans_log_wrapper.h" + namespace OHOS { namespace Notification { void NotificationMediaContent::SetAVToken(const std::shared_ptr &avToken) diff --git a/frameworks/ans/native/src/notification_multiline_content.cpp b/frameworks/ans/native/src/notification_multiline_content.cpp index 320c23bd5e21a24c3505ad8e035bf13969b74f14..299ac9059e2af53ae64b75ce67effa7293cad68f 100644 --- a/frameworks/ans/native/src/notification_multiline_content.cpp +++ b/frameworks/ans/native/src/notification_multiline_content.cpp @@ -13,9 +13,11 @@ * limitations under the License. */ +#include "notification_multiline_content.h" + #include + #include "ans_log_wrapper.h" -#include "notification_multiline_content.h" namespace OHOS { namespace Notification { diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index bbf4e130301dd9f8fceb9adfb0e8443e99183986..0bce17ed3faa54971031d03d8d093bd6168fcac6 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "ans_log_wrapper.h" #include "notification_request.h" +#include "ans_log_wrapper.h" + namespace OHOS { namespace Notification { const std::string NotificationRequest::CLASSIFICATION_ALARM {"alarm"}; @@ -111,6 +112,8 @@ NotificationRequest::NotificationRequest(const NotificationRequest &other) this->actionButtons_ = other.actionButtons_; this->messageUsers_ = other.messageUsers_; this->userInputHistory_ = other.userInputHistory_; + + this->notificationTemplate_ = other.notificationTemplate_; } NotificationRequest &NotificationRequest::operator=(const NotificationRequest &other) @@ -171,6 +174,8 @@ NotificationRequest &NotificationRequest::operator=(const NotificationRequest &o this->messageUsers_ = other.messageUsers_; this->userInputHistory_ = other.userInputHistory_; + this->notificationTemplate_ = other.notificationTemplate_; + return *this; } @@ -754,6 +759,7 @@ std::string NotificationRequest::Dump() ", bigIcon = " + (bigIcon_ ? "not null" : "null") + ", notificationContent = " + (notificationContent_ ? notificationContent_->Dump() : "null") + ", publicNotification = " + (publicNotification_ ? "not null" : "null") + + ", notificationTemplate = " + (notificationTemplate_ ? "not null" : "null") + ", actionButtons = " + (!actionButtons_.empty() ? actionButtons_.at(0)->Dump() : "empty") + ", messageUsers = " + (!messageUsers_.empty() ? messageUsers_.at(0)->Dump() : "empty") + ", userInputHistory = " + (!userInputHistory_.empty() ? userInputHistory_.at(0) : "empty") + @@ -1089,6 +1095,19 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const return false; } + valid = notificationTemplate_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether publicNotification is null"); + return false; + } + + if (valid) { + if (!parcel.WriteParcelable(notificationTemplate_.get())) { + ANS_LOGE("Failed to write notificationTemplate"); + return false; + } + } + return true; } @@ -1283,6 +1302,15 @@ bool NotificationRequest::ReadFromParcel(Parcel &parcel) return false; } + valid = parcel.ReadBool(); + if (valid) { + notificationTemplate_ = std::shared_ptr(parcel.ReadParcelable()); + if (!notificationTemplate_) { + ANS_LOGE("Failed to read notificationTemplate"); + return false; + } + } + return true; } @@ -1294,5 +1322,15 @@ int64_t NotificationRequest::GetNowSysTime() int64_t duration = value.count(); return duration; } + +void NotificationRequest::SetTemplate(const std::shared_ptr &templ) +{ + notificationTemplate_ = templ; +} + +std::shared_ptr NotificationRequest::GetTemplate() const +{ + return notificationTemplate_; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/native/src/notification_template.cpp b/frameworks/ans/native/src/notification_template.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6bc3b1ed56af11c18edb8a4dab03959f0113bc1d --- /dev/null +++ b/frameworks/ans/native/src/notification_template.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notification_template.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace Notification { +void NotificationTemplate::SetTemplateName(const std::string &name) +{ + templateName_ = name; +} + +std::string NotificationTemplate::GetTemplateName() const +{ + return templateName_; +} + +void NotificationTemplate::SetTemplateData(const std::shared_ptr &data) +{ + templateData_ = data; +} + +std::shared_ptr NotificationTemplate::GetTemplateData() const +{ + return templateData_; +} + +std::string NotificationTemplate::Dump() +{ + return "templateName = " + templateName_ + + ", templateData = " + (templateData_ ? "not null" : "null"); +} + +bool NotificationTemplate::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(templateName_)) { + ANS_LOGE("Failed to write text"); + return false; + } + + bool valid = templateData_ ? true : false; + if (!parcel.WriteBool(valid)) { + ANS_LOGE("Failed to write the flag which indicate whether templateData is null"); + return false; + } + + return true; +} + +NotificationTemplate *NotificationTemplate::Unmarshalling(Parcel &parcel) +{ + auto templ = new NotificationTemplate(); + if ((templ != nullptr) && !templ->ReadFromParcel(parcel)) { + delete templ; + templ = nullptr; + } + + return templ; +} + +bool NotificationTemplate::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadString(templateName_)) { + ANS_LOGE("Failed to read template name"); + return false; + } + + bool valid = parcel.ReadBool(); + if (valid) { + templateData_ = std::shared_ptr(parcel.ReadParcelable()); + if (!templateData_) { + ANS_LOGE("Failed to read template data"); + return false; + } + } + + return true; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/ans/test/moduletest/BUILD.gn b/frameworks/ans/test/moduletest/BUILD.gn index f2edf409681f6fe4c89d46cdf1939ae51f673d4f..7c5a0ec1514fedafff539c53d406bede106cc7ab 100644 --- a/frameworks/ans/test/moduletest/BUILD.gn +++ b/frameworks/ans/test/moduletest/BUILD.gn @@ -173,6 +173,7 @@ ohos_moduletest("ans_innerkits_module_publish_test") { "${frameworks_path}/wantagent:wantagent_innerkits", "${services_path}/ans:libans", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/base:base", "//foundation/aafwk/standard/interfaces/innerkits/want:want", "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp index 2c14f23f93d52a0b3b6ddc980437f0597a3a47f6..5f3f3428fb1b9d841dac9326f1812f2f4d1a7e1c 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_publish_test.cpp @@ -28,6 +28,8 @@ #include "system_ability_definition.h" #include "want_agent_info.h" #include "want_agent_helper.h" +#include "want_params.h" +#include "ohos/aafwk/base/int_wrapper.h" using namespace testing::ext; namespace OHOS { @@ -52,6 +54,7 @@ const int32_t CASE_TEN = 10; const int32_t CASE_ELEVEN = 11; const int32_t CASE_TWELVE = 12; const int32_t CASE_THIRTEEN = 13; +const int32_t CASE_FOURTEEN = 14; const int32_t CALLING_UID = 9999; const int32_t PIXEL_MAP_TEST_WIDTH = 32; @@ -131,6 +134,8 @@ public: } else if (CASE_THIRTEEN == notificationRequest.GetNotificationId()) { EXPECT_EQ(NotificationRequest::GroupAlertType::ALL, notificationRequest.GetGroupAlertType()); EXPECT_EQ(true, notificationRequest.IsGroupOverview()); + } else if (CASE_FOURTEEN == notificationRequest.GetNotificationId()) { + CheckCaseFourteenResult(notificationRequest); } else { GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish::OnConsumed do nothing!!!!!"; } @@ -352,6 +357,18 @@ private: EXPECT_EQ(0, notificationRequest.GetProgressValue()); EXPECT_EQ(false, notificationRequest.IsProgressIndeterminate()); } + + void CheckCaseFourteenResult(const NotificationRequest& notificationRequest) const + { + std::shared_ptr notiTemplate = notificationRequest.GetTemplate(); + if (notiTemplate != nullptr) { + EXPECT_EQ("process", notiTemplate->GetTemplateName()); + std::shared_ptr param = notiTemplate->GetTemplateData(); + int value = AAFwk::Integer::Unbox(AAFwk::IInteger::Query(param->GetParam("process"))); + EXPECT_EQ(20, value); // 20 test input + } + EXPECT_EQ(NotificationConstant::OTHER, notificationRequest.GetSlotType()); + } }; class CompletedCallbackTest : public WantAgent::CompletedCallback { @@ -1159,5 +1176,49 @@ HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_CancelGroup_10100, Func EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber, info)); WaitOnUnsubscribeResult(); } + +/** + * @tc.number : ANS_Interface_MT_Publish_04000 + * @tc.name : Publish_04000 + * @tc.desc : Add notification slot(type is OTHER), make a subscriber and publish a template notification. + * @tc.expected : Add notification slot success, make a subscriber and publish a ltemplate notification success. + */ +HWTEST_F(AnsInterfaceModulePublishTest, ANS_Interface_MT_Publish_04000, Function | MediumTest | Level1) +{ + NotificationSlot slot(NotificationConstant::OTHER); + EXPECT_EQ(0, NotificationHelper::AddNotificationSlot(slot)); + auto subscriber = TestAnsSubscriber(); + NotificationSubscribeInfo info = NotificationSubscribeInfo(); + info.AddAppName("bundleName"); + g_subscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::SubscribeNotification(subscriber, info)); + WaitOnSubscribeResult(); + + std::shared_ptr notiTemplate = std::make_shared(); + EXPECT_NE(notiTemplate, nullptr); + notiTemplate->SetTemplateName("process"); + // [{'process':20}] + AAFwk::WantParams wantParams; + std::string key("process"); + int resultValue = 20; + wantParams.SetParam(key, AAFwk::Integer::Box(resultValue)); + notiTemplate->SetTemplateData(std::make_shared(wantParams)); + GTEST_LOG_(INFO) << "ANS_Interface_MT_Publish_04000::notiTemplate::" << notiTemplate->Dump(); + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + NotificationRequest req; + req.SetContent(content); + req.SetTemplate(notiTemplate); + req.SetSlotType(NotificationConstant::OTHER); + req.SetNotificationId(CASE_FOURTEEN); + g_consumed_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::PublishNotification(req)); + WaitOnConsumed(); + g_unsubscribe_mtx.lock(); + EXPECT_EQ(0, NotificationHelper::UnSubscribeNotification(subscriber, info)); + WaitOnUnsubscribeResult(); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp b/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp index 4a16ba62f171039707bf3e5ebda10f34d91b3530..a34cdb20e850acab50001e0717857aba44b584dd 100644 --- a/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp +++ b/frameworks/ans/test/moduletest/ans_innerkits_module_setting_test.cpp @@ -134,5 +134,48 @@ HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_004 EXPECT_EQ("bundlename", bundleOption.GetBundleName()); EXPECT_EQ(CALLING_UID, bundleOption.GetUid()); } + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00500 + * @tc.name : NotificationSetting_00500 + * @tc.desc : If the template configuration file does not exist, query whether the template exists. + * @tc.expected : Query return failed. + */ +HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_00500, Function | MediumTest | Level1) +{ + std::string templateName("process"); + bool support = false; + EXPECT_EQ((int)ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, + NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(false, support); +} + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00600 + * @tc.name : NotificationSetting_00600 + * @tc.desc : The template exists in the system, query whether the template exists. + * @tc.expected : Query return success. + */ +HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_00600, Function | MediumTest | Level1) +{ + std::string templateName("process"); + bool support = false; + EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(true, support); +} + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00700 + * @tc.name : NotificationSetting_00700 + * @tc.desc : The template does not exist in the system, query whether the template exists. + * @tc.expected : Query return failed. + */ +HWTEST_F(AnsInterfaceModuleSettingTest, ANS_Interface_MT_NotificationSetting_00700, Function | MediumTest | Level1) +{ + std::string templateName("template123"); + bool support = false; + EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(false, support); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/wantagent/src/want_agent_helper.cpp b/frameworks/wantagent/src/want_agent_helper.cpp index 33135ef63418f8b91bbbbcc2cdb4065e9e272b37..ba702512b1a7cae381256934525f19cb97f0d792 100644 --- a/frameworks/wantagent/src/want_agent_helper.cpp +++ b/frameworks/wantagent/src/want_agent_helper.cpp @@ -14,12 +14,13 @@ */ #include "want_agent_helper.h" + #include "ability_manager_client.h" #include "hilog_wrapper.h" #include "pending_want.h" #include "want_agent_log_wrapper.h" -#include "want_sender_interface.h" #include "want_sender_info.h" +#include "want_sender_interface.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index a5d94f43ebdd8ac54541ecc8626ff898d129521b..fcf245989ab8c6eecea90106322d5863464f746b 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -524,7 +524,7 @@ public: * only be null or an empty string, indicating the current device. * @param enabled Specifies whether to allow the current application to publish notifications. The value * true indicates that notifications are allowed, and the value false indicates that - * notifications are not allowed. + * notifications are not allowed. * @return Returns set notifications enabled for default bundle result. */ static ErrCode SetNotificationsEnabledForDefaultBundle(const std::string &deviceId, bool enabled); @@ -629,6 +629,14 @@ public: * @return Returns cancel continuous task notification result. */ static ErrCode CancelContinuousTaskNotification(const std::string &label, int32_t notificationId); + + /** + * Obtains whether the template is supported by the system. + * + * @param support whether is it a system supported template. + * @return Returns check result. + */ + static ErrCode IsSupportTemplate(const std::string &templateName, bool &support); }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/notification_request.h b/interfaces/innerkits/ans/native/include/notification_request.h index 1e8a22b5201d2156225eb8df31a618cc8e2d1ebe..74b26955014ebfd9e511b0f43daed2a682eed584 100644 --- a/interfaces/innerkits/ans/native/include/notification_request.h +++ b/interfaces/innerkits/ans/native/include/notification_request.h @@ -20,6 +20,7 @@ #include "message_user.h" #include "notification_action_button.h" #include "notification_content.h" +#include "notification_template.h" #include "ohos/aafwk/content/want_params.h" #include "parcel.h" #include "pixel_map.h" @@ -860,6 +861,18 @@ public: */ static NotificationRequest *Unmarshalling(Parcel &parcel); + /** + * Sets the template of this notification. + * @param template the template of this notification. + */ + void SetTemplate(const std::shared_ptr &templ); + + /** + * Obtains the Template of the notification. + * @return the Template of the notification. + */ + std::shared_ptr GetTemplate() const; + private: /** * Indicates the color mask, used for calculation with the ARGB value set by setColor(int32_t). @@ -945,6 +958,7 @@ private: std::vector> actionButtons_ {}; std::vector> messageUsers_ {}; std::vector userInputHistory_ {}; + std::shared_ptr notificationTemplate_ {}; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/innerkits/ans/native/include/notification_template.h b/interfaces/innerkits/ans/native/include/notification_template.h new file mode 100644 index 0000000000000000000000000000000000000000..0b56be3373617c70a946805b4e0d02ad3a035833 --- /dev/null +++ b/interfaces/innerkits/ans/native/include/notification_template.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_TEMPLATE_H +#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_TEMPLATE_H + +#include +#include +#include "parcel.h" +#include "ohos/aafwk/content/want_params.h" + +namespace OHOS { +namespace Notification { +class NotificationTemplate : public Parcelable { +public: + /** + * Default constructor used to create an empty NotificationTemplate instance. + */ + NotificationTemplate() = default; + + /** + * Default deconstructor used to deconstruct. + */ + ~NotificationTemplate() = default; + + /** + * Sets the template name to be included in a template notification. + * @param name template name. + */ + void SetTemplateName(const std::string &name); + + /** + * Obtains the template name to be included in a template notification. + * @return template name. + */ + std::string GetTemplateName() const; + + /** + * Sets the template data to be included in a template notification. + * @param data template data. + */ + void SetTemplateData(const std::shared_ptr &data); + + /** + * Obtains the template data to be included in a template notification. + * @return template data. + */ + std::shared_ptr GetTemplateData() const; + + /** + * Returns a string representation of the object. + * @return a string representation of the object. + */ + std::string Dump(); + + /** + * Marshal a object into a Parcel. + * @param parcel the object into the parcel + */ + virtual bool Marshalling(Parcel &parcel) const override; + + /** + * Unmarshal object from a Parcel. + * @return the NotificationTemplate + */ + static NotificationTemplate *Unmarshalling(Parcel &parcel); + +private: + /** + * Read a NotificationTemplate object from a Parcel. + * @param parcel the parcel + */ + bool ReadFromParcel(Parcel &parcel); + +private: + std::string templateName_; + std::shared_ptr templateData_ {}; +}; +} // namespace Notification +} // namespace OHOS + +#endif // #define BASE_NOTIFICATION_ANS_STANDARD_KITS_NATIVE_INCLUDE_NOTIFICATION_TEMPLATE_H + diff --git a/interfaces/kits/js/@ohos.notification.d.ts b/interfaces/kits/js/@ohos.notification.d.ts index aed59524ffafaa4c54e61bcaad1827cd9737ea7d..79467c48a00df5005914bb2385e1b951118b6e48 100644 --- a/interfaces/kits/js/@ohos.notification.d.ts +++ b/interfaces/kits/js/@ohos.notification.d.ts @@ -497,6 +497,8 @@ declare namespace notification { /** * Cancel the notification of a specified group for this application. + * + * @since 8 */ function cancelGroup(groupName: string, callback: AsyncCallback): void; function cancelGroup(groupName: string): Promise; @@ -504,6 +506,7 @@ declare namespace notification { /** * Delete the notification of a specified group for this application. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ @@ -513,6 +516,7 @@ declare namespace notification { /** * Set the Do Not Disturb date. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ @@ -522,6 +526,7 @@ declare namespace notification { /** * Obtains the Do Not Disturb date. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ @@ -531,12 +536,22 @@ declare namespace notification { /** * Obtains whether to support the Do Not Disturb mode. * + * @since 8 * @systemapi Hide this for inner system use. * @permission ohos.permission.NOTIFICATION_CONTROLLER */ function supportDoNotDisturbMode(callback: AsyncCallback): void; function supportDoNotDisturbMode(): Promise; + /** + * Obtains whether the template is supported by the system. + * + * @since 8 + * @param plateName Name of template to be Obtained + */ + function isSupportTemplate(plateName: string, callback: AsyncCallback): void; + function isSupportTemplate(plateName: string): Promise; + /** * Describes a BundleOption. */ @@ -587,6 +602,7 @@ declare namespace notification { /** * The type of the Do Not Disturb. * + * @since 8 * @systemapi Hide this for inner system use. */ export enum DoNotDisturbType { @@ -619,16 +635,22 @@ declare namespace notification { export interface DoNotDisturbDate { /** * the type of the Do Not Disturb. + * + * @since 8 */ type: DoNotDisturbType; /** * the start time of the Do Not Disturb. + * + * @since 8 */ begin: Date; /** * the end time of the Do Not Disturb. + * + * @since 8 */ end: Date; } @@ -636,6 +658,7 @@ declare namespace notification { /** * Notification source type * + * @since 8 * @systemapi Hide this for inner system use. */ export enum SourceType { diff --git a/interfaces/kits/js/notification/notificationActionButton.d.ts b/interfaces/kits/js/notification/notificationActionButton.d.ts index b2967c4626c391d915525657be1582e2e19ce0b6..9428cc4f0d34c8bed47bc6c6b6e034e981f6fb1d 100644 --- a/interfaces/kits/js/notification/notificationActionButton.d.ts +++ b/interfaces/kits/js/notification/notificationActionButton.d.ts @@ -42,6 +42,8 @@ export interface NotificationActionButton { /** * User input + * + * @since 8 */ userInput?: NotificationUserInput; } diff --git a/interfaces/kits/js/notification/notificationRequest.d.ts b/interfaces/kits/js/notification/notificationRequest.d.ts index 6cefef3867a765a6abd5cd0e324b2d904af1b258..6525bf19ea3640ac643504b838db1111e35a4218 100644 --- a/interfaces/kits/js/notification/notificationRequest.d.ts +++ b/interfaces/kits/js/notification/notificationRequest.d.ts @@ -18,6 +18,7 @@ import image from '../@ohos.multimedia.image'; import { WantAgent } from '../@ohos.wantAgent'; import { NotificationContent } from './notificationContent'; import { NotificationActionButton } from './notificationActionButton'; +import { NotificationTemplate } from './notificationTemplate'; /** * Defines a NotificationRequest instance. @@ -141,6 +142,8 @@ export interface NotificationRequest { /** * The group information for this notification. + * + * @since 8 */ groupName?: string; @@ -174,6 +177,8 @@ export interface NotificationRequest { /** * Whether the notification can be remove. * + * @default true + * @since 8 * @systemapi Hide this for inner system use. */ readonly isRemoveAllowed?: boolean; @@ -181,7 +186,15 @@ export interface NotificationRequest { /** * Notification source. enum SourceType * + * @since 8 * @systemapi Hide this for inner system use. */ readonly source?: number; + + /** + * Obtains the template of this notification. + * + * @since 8 + */ + template?: NotificationTemplate; } diff --git a/interfaces/kits/js/notification/notificationSubscriber.d.ts b/interfaces/kits/js/notification/notificationSubscriber.d.ts index 2b3f57ee9ea164122fb194ac4a45a4fecdb334f7..d8e2277bc72c82461cf7efc540cdf00be1cf9e19 100644 --- a/interfaces/kits/js/notification/notificationSubscriber.d.ts +++ b/interfaces/kits/js/notification/notificationSubscriber.d.ts @@ -36,6 +36,12 @@ export interface NotificationSubscriber { onDisconnect?:() => void; onDestroy?:() => void; onDisturbModeChange?:(mode: notification.DoNotDisturbMode) => void; + + /** + * Callback when the Do Not Disturb setting changed. + * + * @since 8 + */ onDoNotDisturbDateChange?:(mode: notification.DoNotDisturbDate) => void; } diff --git a/interfaces/kits/js/notification/notificationTemplate.d.ts b/interfaces/kits/js/notification/notificationTemplate.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..d131ed5cb2afa784543246f7ed70d20651717b60 --- /dev/null +++ b/interfaces/kits/js/notification/notificationTemplate.d.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"), + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Describes a NotificationTemplate instance. + * + * @name NotificationTemplate + * @since 8 + * @devices phone, tablet, tv, wearable, car + * @permission N/A + * @sysCap SystemCapability.Notification.ANS + */ +export interface NotificationTemplate { + /** + * Obtains the type of a notification template. + */ + name: string; + + /** + * Obtains the data of a notification template. + */ + data: {[key: string]: Object}; +} diff --git a/interfaces/kits/napi/ans/BUILD.gn b/interfaces/kits/napi/ans/BUILD.gn index 2d7176e35e9c49e5eacec5217b237e444765318d..01c5cfad561950da29df030923d2291b7c1a5408 100644 --- a/interfaces/kits/napi/ans/BUILD.gn +++ b/interfaces/kits/napi/ans/BUILD.gn @@ -46,6 +46,7 @@ ohos_shared_library("notification") { configs = [ ":native_module_config" ] sources = [ + "src/ans_template.cpp", "src/cancel.cpp", "src/common.cpp", "src/constant.cpp", diff --git a/interfaces/kits/napi/ans/include/ans_template.h b/interfaces/kits/napi/ans/include/ans_template.h new file mode 100644 index 0000000000000000000000000000000000000000..2919b62332f327cbfe90a89c80924f7c0ed1b348 --- /dev/null +++ b/interfaces/kits/napi/ans/include/ans_template.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_TEMPLATE_H +#define BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_TEMPLATE_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value IsSupportTemplate(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_TEMPLATE_H \ No newline at end of file diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index ca2751f8f7c8c473e95e29adb06997955333841e..4f202eb0b9dca4f7197d267afee50ceff382fa4e 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -404,6 +404,15 @@ public: static napi_value CreateWantAgentByJS(const napi_env &env, const std::shared_ptr &agent); + static napi_value GetNotificationTemplate( + const napi_env &env, const napi_value &value, NotificationRequest &request); + + static napi_value GetNotificationTemplateInfo(const napi_env &env, const napi_value &value, + std::shared_ptr &templ); + + static napi_value SetNotificationTemplateInfo( + const napi_env &env, const std::shared_ptr &templ, napi_value &result); + private: static const int ARGS_ONE = 1; static const int ARGS_TWO = 2; diff --git a/interfaces/kits/napi/ans/src/ans_template.cpp b/interfaces/kits/napi/ans/src/ans_template.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1efa212505c3f0296f50e3258adfedf40848dcfe --- /dev/null +++ b/interfaces/kits/napi/ans/src/ans_template.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ans_template.h" +#include "cancel.h" + +namespace OHOS { +namespace NotificationNapi { +const int IS_TEMPLATE_MAX_PARA = 2; + +struct TemplateName { + std::string templateName = ""; + bool support = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoTemplate { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + TemplateName params; + CallbackPromiseInfo info; +}; + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, TemplateName& params) +{ + ANS_LOGI("enter"); + + size_t argc = IS_TEMPLATE_MAX_PARA; + napi_value argv[IS_TEMPLATE_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc < IS_TEMPLATE_MAX_PARA, "Wrong number of arguments"); + + napi_valuetype valuetype = napi_undefined; + // argv[0]: name: string + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], str, STR_MAX_SIZE - 1, &strLen)); + params.templateName = str; + + // argv[1]: callback + if (argc >= IS_TEMPLATE_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + napi_create_reference(env, argv[1], 1, ¶ms.callback); + } + + return Common::NapiGetNull(env); +} + +napi_value IsSupportTemplate(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + TemplateName params; + if (ParseParameters(env, info, params) == nullptr) { + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoTemplate *asyncCallbackinfo = new (std::nothrow) + AsyncCallbackInfoTemplate {.env = env, .asyncWork = nullptr, .params = params}; + if (!asyncCallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asyncCallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "IsSupportTemplate", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work start"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = (AsyncCallbackInfoTemplate *)data; + + asyncCallbackinfo->info.errorCode = NotificationHelper::CancelNotification( + asyncCallbackinfo->params.templateName, asyncCallbackinfo->params.support); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("IsSupportTemplate napi_create_async_work end"); + AsyncCallbackInfoTemplate *asyncCallbackinfo = (AsyncCallbackInfoTemplate *)data; + + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackinfo->params.support, &result); + Common::ReturnCallbackPromise(env, asyncCallbackinfo->info, result); + + if (asyncCallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asyncCallbackinfo->info.callback); + } + + napi_delete_async_work(env, asyncCallbackinfo->asyncWork); + if (asyncCallbackinfo) { + delete asyncCallbackinfo; + asyncCallbackinfo = nullptr; + } + }, + (void *)asyncCallbackinfo, + &asyncCallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackinfo->asyncWork)); + + if (asyncCallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index c995c0e621c395c21fba57f99ea0893ad88faf90..2864281d6a025a9f0b488091ac35f251e96bfb0e 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -400,7 +400,7 @@ napi_value Common::SetNotificationRequestByPixelMap( return NapiGetBoolean(env, false); } - // smallIcon?: image.PixelMap + // smallIcon?: image.PixelMap std::shared_ptr littleIcon = request->GetLittleIcon(); if (littleIcon) { napi_value smallIconResult = nullptr; @@ -482,6 +482,18 @@ napi_value Common::SetNotificationRequestByCustom( } napi_set_named_property(env, result, "actionButtons", arr); + // template?: NotificationTemplate; + std::shared_ptr templ = request->GetTemplate(); + if (templ) { + napi_value templateResult = nullptr; + napi_create_object(env, &templateResult); + if (!SetNotificationTemplateInfo(env, templ, templateResult)) { + ANS_LOGE("SetNotificationTemplate call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "template", templateResult); + } + return NapiGetBoolean(env, true); } @@ -1408,6 +1420,10 @@ napi_value Common::GetNotificationRequestByCustom( if (GetNotificationLargeIcon(env, value, request) == nullptr) { return nullptr; } + // template?: NotificationTemplate + if (GetNotificationTemplate(env, value, request) == nullptr) { + return nullptr; + } return NapiGetNull(env); } @@ -2823,7 +2839,7 @@ napi_value Common::GetNotificationConversationalContentMessages( napi_value conversationalContentResult = nullptr; bool hasProperty = false; - // messages: Array + // messages: Array NAPI_CALL(env, napi_has_named_property(env, contentResult, "messages", &hasProperty)); NAPI_ASSERT(env, hasProperty, "Property messages expected."); napi_get_named_property(env, contentResult, "messages", &conversationalContentResult); @@ -3751,5 +3767,99 @@ napi_value Common::CreateWantAgentByJS(const napi_env &env, const std::shared_pt return wantAgent; } + +napi_value Common::GetNotificationTemplate(const napi_env &env, const napi_value &value, NotificationRequest &request) +{ + ANS_LOGI("enter"); + + napi_valuetype valuetype = napi_undefined; + napi_value result = nullptr; + bool hasProperty = false; + + NAPI_CALL(env, napi_has_named_property(env, value, "template", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, value, "template", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type. Object expected."); + + std::shared_ptr templ = std::make_shared(); + if (templ == nullptr) { + ANS_LOGE("template is null"); + return nullptr; + } + if (GetNotificationTemplateInfo(env, value, templ) == nullptr) { + return nullptr; + } + + request.SetTemplate(templ); + } + + return NapiGetNull(env); +} + +napi_value Common::GetNotificationTemplateInfo(const napi_env &env, const napi_value &value, + std::shared_ptr &templ) +{ + ANS_LOGI("enter"); + + napi_valuetype valuetype = napi_undefined; + napi_value result = nullptr; + bool hasProperty = false; + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + + // name: string + NAPI_CALL(env, napi_has_named_property(env, value, "name", &hasProperty)); + NAPI_ASSERT(env, hasProperty, "Property text expected."); + napi_get_named_property(env, value, "name", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); + NAPI_CALL(env, napi_get_value_string_utf8(env, result, str, STR_MAX_SIZE - 1, &strLen)); + std::string strInput = str; + templ->SetTemplateName(strInput); + + // data?: {[key: string]: object} + NAPI_CALL(env, napi_has_named_property(env, value, "data", &hasProperty)); + if (hasProperty) { + napi_get_named_property(env, value, "data", &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type. Object expected."); + AAFwk::WantParams wantParams; + if (!OHOS::AppExecFwk::UnwrapWantParams(env, result, wantParams)) { + return nullptr; + } + + std::shared_ptr data = std::make_shared(wantParams); + templ->SetTemplateData(data); + } + + return NapiGetNull(env); +} + +napi_value Common::SetNotificationTemplateInfo( + const napi_env &env, const std::shared_ptr &templ, napi_value &result) +{ + ANS_LOGI("enter"); + + if (templ == nullptr) { + ANS_LOGE("templ is null"); + return NapiGetBoolean(env, false); + } + + napi_value value = nullptr; + + // name: string; + napi_create_string_utf8(env, templ->GetTemplateName().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "name", value); + + // data?: {[key: string]: object}; + std::shared_ptr data = templ->GetTemplateData(); + if (data) { + value = OHOS::AppExecFwk::WrapWantParams(env, *data); + napi_set_named_property(env, result, "data", value); + } + + return NapiGetBoolean(env, true); +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/init.cpp b/interfaces/kits/napi/ans/src/init.cpp index 2a27497cb543604e9c78bb575343b2f0192799fa..c3cfdbb1dde94e05f1778a11ba77a023fb062dfd 100644 --- a/interfaces/kits/napi/ans/src/init.cpp +++ b/interfaces/kits/napi/ans/src/init.cpp @@ -14,6 +14,7 @@ */ #include "init.h" +#include "ans_template.h" #include "cancel.h" #include "constant.h" #include "display_badge.h" @@ -66,6 +67,7 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("setDoNotDisturbDate", SetDoNotDisturbDate), DECLARE_NAPI_FUNCTION("getDoNotDisturbDate", GetDoNotDisturbDate), DECLARE_NAPI_FUNCTION("supportDoNotDisturbMode", SupportDoNotDisturbMode), + DECLARE_NAPI_FUNCTION("isSupportTemplate", IsSupportTemplate), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index 8573dcb04ae1548a9108b17ba9f8d2c08e1bede8..e0083a426920c4bbec1cab740f5e22671345b378 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ +#include "subscribe.h" + #include #include -#include "subscribe.h" namespace OHOS { namespace NotificationNapi { diff --git a/interfaces/kits/napi/wantagent/napi_want_agent.h b/interfaces/kits/napi/wantagent/napi_want_agent.h index e79f2e93d5edde266d6865783350ec782ddee7ca..6a33753c74fbd69aca90b09173abbe6de6bbc7e5 100644 --- a/interfaces/kits/napi/wantagent/napi_want_agent.h +++ b/interfaces/kits/napi/wantagent/napi_want_agent.h @@ -16,22 +16,21 @@ #ifndef NAPI_WANT_AGENT_H #define NAPI_WANT_AGENT_H -#include -#include #include +#include +#include #include -#include "napi/native_common.h" -#include "napi/native_node_api.h" - -#include "want_agent_constant.h" +#include "ability.h" #include "completed_callback.h" #include "context.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" +#include "trigger_info.h" #include "want.h" -#include "want_params.h" -#include "ability.h" #include "want_agent.h" -#include "trigger_info.h" +#include "want_agent_constant.h" +#include "want_params.h" namespace OHOS { using namespace OHOS::AppExecFwk; diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 8479421503369e5abe2c9a49dd4803add27af2b2..b53211b491fe8cca63ddc1ff6d5301fb40dece19 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -112,6 +112,7 @@ public: ErrCode CancelReminder(const int32_t reminderId) override; ErrCode GetValidReminders(std::vector> &reminders) override; ErrCode CancelAllReminders() override; + ErrCode IsSupportTemplate(const std::string &templateName, bool &support) override; // SystemEvent void OnBundleRemoved(const sptr &bundleOption); @@ -175,4 +176,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // ADVANCED_NOTIFICATION_SERVICE_H \ No newline at end of file +#endif // ADVANCED_NOTIFICATION_SERVICE_H diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 3104edf4538eb7b33f7f38402f312008e53123c2..ef0af0a2f036b516c76777a2b0c4cb2c568fb7eb 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -68,6 +68,7 @@ public: ErrCode SetNotificationsEnabled(const bool &enabled); ErrCode GetDoNotDisturbDate(sptr &date); ErrCode SetDoNotDisturbDate(const sptr date); + ErrCode GetTemplateSupported(const std::string& templateName, bool &support); ErrCode ClearNotificationInRestoreFactorySettings(); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 1a1652fd409a4245635438a9cec799b4bc3915fa..b29831f2c75a8d5c900aa25ddb859419f83b695e 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -2088,5 +2088,16 @@ ErrCode AdvancedNotificationService::PrepereContinuousTaskNotificationRequest( ErrCode result = CheckPictureSize(request); return result; } + +ErrCode AdvancedNotificationService::IsSupportTemplate(const std::string& templateName, bool &support) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + ErrCode result = ERR_OK; + handler_->PostSyncTask(std::bind([&]() { + support = false; + result = NotificationPreferences::GetInstance().GetTemplateSupported(templateName, support); + })); + return result; +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 04c2ce8d6124d51a02aff3aa09b937d2ed8ab5c2..a8fcbaf9b93474d2f53cf696e72b3e2cd68607e2 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -15,9 +15,12 @@ #include "notification_preferences.h" +#include + #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "nlohmann/json.hpp" namespace OHOS { namespace Notification { @@ -773,5 +776,36 @@ void NotificationPreferences::OnDistributedKvStoreDeathRecipient() } } } + +ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templateName, bool &support) +{ + if (templateName.length() == 0) { + ANS_LOGE("template name is null."); + return ERR_ANS_INVALID_PARAM; + } + + std::ifstream inFile; + inFile.open(DEFAULT_TEMPLATE_PATH.c_str(), std::ios::in); + if (!inFile.is_open()) { + ANS_LOGE("read template config error."); + return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED; + } + + nlohmann::json jsonObj; + inFile >> jsonObj; + if (jsonObj.is_discarded()) { + ANS_LOGE("template json discarded error."); + inFile.close(); + return ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED; + } + + if (jsonObj.contains(templateName)) { + support = true; + } + + jsonObj.clear(); + inFile.close(); + return ERR_OK; +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 80690341eed4583efd8e91eaa6a55c492c615890..fbb6437500e0fdfc4bd8d51dfc41ebfa2fa3ce22 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -92,6 +92,7 @@ ohos_unittest("ans_unit_test") { "appexecfwk_standard:appexecfwk_core", "appexecfwk_standard:libeventhandler", "ces_standard:cesfwk_innerkits", + "hitrace_native:libhitrace", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index f33bd200295362c5b3045d8667cf21b01fb361e5..93dbc3c8226cb71c245c74483d8fb10f0b715053 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -90,6 +90,7 @@ ohos_moduletest("ans_module_test") { "appexecfwk_standard:appexecfwk_core", "appexecfwk_standard:libeventhandler", "ces_standard:cesfwk_innerkits", + "hitrace_native:libhitrace", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/test/BUILD.gn b/test/BUILD.gn index 1cd6c473bda10397aea56b5ccd733c6c48d551b1..9692d01f949e3dd202cdc6088c9e06b80ddb286a 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -25,8 +25,8 @@ group("systemtest") { testonly = true deps = [ - "common\acts:actsnotificationtest", - "resource\ansSTSlotGroupTest:ansSTSlotGroupTest", - "systemtest\acts\ansDump:ansDumpTest", + #"common\acts:actsnotificationtest", + #"resource\ansSTSlotGroupTest:ansSTSlotGroupTest", + #"systemtest\acts\ansDump:ansDumpTest", ] } diff --git a/tools/dump/src/main.cpp b/tools/dump/src/main.cpp index b5ea1058ddebca6ae075b861b6576edbc9cd87cc..668d3916160bcd663c605d23b28cc071d087d9d4 100644 --- a/tools/dump/src/main.cpp +++ b/tools/dump/src/main.cpp @@ -20,4 +20,4 @@ int main(int argc, char *argv[]) OHOS::Notification::NotificationShellCommand cmd(argc, argv); std::cout << cmd.ExecCommand(); return 0; -} \ No newline at end of file +}