From 2d7fa4fe78bce7571c6b4ea56c7927c9cf5482c3 Mon Sep 17 00:00:00 2001 From: zengsiyu Date: Fri, 8 Apr 2022 11:08:17 +0800 Subject: [PATCH] feat: show Signed-off-by: zengsiyu Change-Id: Ib10f47f17146fb2ad12aa662c3d3a986910ac6e5 Signed-off-by: zengsiyu --- interfaces/kits/napi/ans/src/publish.cpp | 198 ++++++++++++++++++++++- 1 file changed, 195 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/napi/ans/src/publish.cpp b/interfaces/kits/napi/ans/src/publish.cpp index 2cd55c318..ad6a9b101 100644 --- a/interfaces/kits/napi/ans/src/publish.cpp +++ b/interfaces/kits/napi/ans/src/publish.cpp @@ -15,9 +15,16 @@ #include "publish.h" +#include "want_agent_helper.h" + namespace OHOS { namespace NotificationNapi { -static const int32_t PUBLISH_NOTIFICATION_MAX = 3; +using namespace AbilityRuntime::WantAgent; + +namespace { +static const int8_t PUBLISH_NOTIFICATION_MAX = 3; +static const int8_t SHOW_NOTIFICATION_MAX = 1; +} struct AsyncCallbackInfoPublish { napi_env env = nullptr; @@ -160,10 +167,195 @@ napi_value Publish(napi_env env, napi_callback_info info) } } -napi_value ShowNotification(napi_env env, napi_callback_info info) +bool CheckProperty(const napi_env &env, const napi_value &content, const std::string &property) +{ + ANS_LOGI("enter"); + + bool hasProperty = false; + + NAPI_CALL(env, napi_has_named_property(env, content, property.data(), &hasProperty)); + if (!hasProperty) { + ANS_LOGW("Property %{public}s expected.", property.c_str()); + } + return hasProperty; +} + +napi_value GetStringProperty( + const napi_env &env, const napi_value &content, const std::string &property, std::string &result) +{ + ANS_LOGI("enter"); + + if (!CheckProperty(env, content, property)) { + return nullptr; + } + + napi_valuetype valuetype = napi_undefined; + napi_value value = nullptr; + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + + napi_get_named_property(env, content, property.data(), &value); + NAPI_CALL(env, napi_typeof(env, value, &valuetype)); + if (valuetype != napi_string) { + ANS_LOGW("Wrong argument type. String expected."); + return nullptr; + } + NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen)); + ANS_LOGI("normal::%{public}s = %{public}s", property.c_str(), str); + result = str; + return Common::NapiGetNull(env); +} + +napi_value GetObjectProperty( + const napi_env &env, const napi_value &content, const std::string &property, napi_value &result) +{ + ANS_LOGI("enter"); + + if (!CheckProperty(env, content, property)) { + return nullptr; + } + + napi_valuetype valuetype = napi_undefined; + napi_get_named_property(env, content, property.data(), &result); + NAPI_CALL(env, napi_typeof(env, result, &valuetype)); + if (valuetype != napi_object) { + ANS_LOGW("Wrong argument type. Object expected."); + return nullptr; + } + return Common::NapiGetNull(env); +} + +napi_value ParseShowOptions(const napi_env &env, const napi_callback_info &info, ParametersInfoPublish ¶ms) { - ANS_LOGI("ShowNotification enter"); + ANS_LOGI("enter"); + + size_t argc = SHOW_NOTIFICATION_MAX; + napi_value argv[SHOW_NOTIFICATION_MAX] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + if (argc == 0) { + ANS_LOGW("Wrong number of arguments."); + return nullptr; + } + + // argv[0] : ShowNotificationOptions + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_object) { + ANS_LOGW("Wrong argument type. Object expected."); + return nullptr; + } + + std::shared_ptr normalContent = std::make_shared(); + + // contentTitle + std::string contentTitle; + if (GetStringProperty(env, argv[PARAM0], "contentTitle", contentTitle) != nullptr) { + normalContent->SetTitle(contentTitle); + } + + // contentText + std::string contentText; + if (GetStringProperty(env, argv[PARAM0], "contentText", contentText) != nullptr) { + normalContent->SetText(contentText); + } + + std::shared_ptr content = std::make_shared(normalContent); + params.request.SetContent(content); + + // clickAction + napi_value clickAction = nullptr; + if (GetObjectProperty(env, argv[PARAM0], "clickAction", clickAction) != nullptr) { + ANS_LOGD("create wantagent"); + // bundleName & abilityName + std::shared_ptr want = std::make_shared(); + std::string bundleName; + std::string abilityName; + if (GetStringProperty(env, clickAction, "bundleName", bundleName) == nullptr) { + return nullptr; + } + if (GetStringProperty(env, clickAction, "abilityName", abilityName) == nullptr) { + return nullptr; + } + want->SetElementName(bundleName, abilityName); + // uri + std::string uri; + if (GetStringProperty(env, clickAction, "uri", uri) == nullptr) { + return nullptr; + } + want->SetUri(uri); + + std::vector> wants = {}; + std::vector wantAgentFlags = {}; + std::shared_ptr extraInfo = std::make_shared(); + wants.emplace_back(want); + WantAgentInfo wantAgentInfo(-1, WantAgentConstant::OperationType::START_ABILITY, wantAgentFlags, + wants, extraInfo); + std::shared_ptr context = AbilityRuntime::Context::GetApplicationContext(); + + std::shared_ptr wantAgent = + WantAgentHelper::GetWantAgent(context, wantAgentInfo); + params.request.SetWantAgent(wantAgent); + } + + ANS_LOGI("end"); return Common::NapiGetNull(env); } + +napi_value ShowNotification(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoPublish params; + if (ParseShowOptions(env, info, params) == nullptr) { + ANS_LOGW("parse showOptions failed"); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoPublish *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoPublish {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + ANS_LOGW("failed to create asynccallbackinfo"); + return Common::JSParaError(env, params.callback); + } + + asynccallbackinfo->request = params.request; + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "show", NAPI_AUTO_LENGTH, &resourceName); + + ANS_LOGI("before napi_create_async_work"); + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Show napi_create_async_work start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + ANS_LOGI("Show napi_create_async_work start notificationId = %{public}d, contentType = " + "%{public}d", + asynccallbackinfo->request.GetNotificationId(), + asynccallbackinfo->request.GetContent()->GetContentType()); + + asynccallbackinfo->info.errorCode = + NotificationHelper::PublishNotification(asynccallbackinfo->request); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Show napi_create_async_work complete start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGI("Show napi_create_async_work complete end"); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + return nullptr; +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file -- Gitee