1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "ani_publish.h"
16
17 #include "notification_helper.h"
18 #include "ans_log_wrapper.h"
19 #include "sts_bundle_option.h"
20 #include "sts_throw_erro.h"
21 #include "sts_common.h"
22 #include "sts_request.h"
23 #include "notification_request.h"
24
25 namespace OHOS {
26 namespace NotificationManagerSts {
27 using namespace OHOS::Notification;
28
AniPublish(ani_env * env,ani_object obj)29 void AniPublish(ani_env *env, ani_object obj)
30 {
31 ANS_LOGD("AniPublish call");
32 std::shared_ptr<NotificationRequest> notificationRequest = std::make_shared<NotificationRequest>();
33 if (NotificationSts::UnWarpNotificationRequest(env, obj, notificationRequest) != ANI_OK) {
34 ANS_LOGE("UnWarpNotificationRequest failed");
35 NotificationSts::ThrowErrorWithMsg(env, "AniPublish ERROR_INTERNAL_ERROR");
36 return;
37 }
38 int returncode = NotificationHelper::PublishNotification(*notificationRequest);
39 if (returncode != ERR_OK) {
40 int externalCode = NotificationSts::GetExternalCode(returncode);
41 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
42 }
43 ANS_LOGD("AniPublish end");
44 }
45
AniPublishWithId(ani_env * env,ani_object obj,ani_double userId)46 void AniPublishWithId(ani_env *env, ani_object obj, ani_double userId)
47 {
48 ANS_LOGD("AniPublishWithId start");
49 //NotificationRequest request;
50 std::shared_ptr<NotificationRequest> notificationRequest = std::make_shared<NotificationRequest>();
51 if (NotificationSts::UnWarpNotificationRequest(env, obj, notificationRequest) != ANI_OK) {
52 NotificationSts::ThrowErrorWithMsg(env, "AniPublishWithId ERROR_INTERNAL_ERROR");
53 return;
54 }
55 notificationRequest->SetOwnerUserId(static_cast<int32_t>(userId));
56 int returncode = NotificationHelper::PublishNotification(*notificationRequest);
57 if (returncode != ERR_OK) {
58 int externalCode = NotificationSts::GetExternalCode(returncode);
59 ANS_LOGE("AniPublishWithId error, errorCode: %{public}d", externalCode);
60 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
61 }
62 ANS_LOGD("AniPublishWithId end");
63 }
64
AniPublishAsBundle(ani_env * env,ani_object request,ani_string representativeBundle,ani_double userId)65 void AniPublishAsBundle(ani_env *env, ani_object request, ani_string representativeBundle, ani_double userId)
66 {
67 ANS_LOGD("AniPublishAsBundle enter");
68 std::string bundleStr;
69 if (ANI_OK != NotificationSts::GetStringByAniString(env, representativeBundle, bundleStr)) {
70 NotificationSts::ThrowErrorWithMsg(env, "AniPublishAsBundle ERROR_INTERNAL_ERROR");
71 return;
72 }
73
74 std::shared_ptr<NotificationRequest> notificationRequest = std::make_shared<NotificationRequest>();
75 if (NotificationSts::UnWarpNotificationRequest(env, request, notificationRequest) != ANI_OK) {
76 ANS_LOGE("AniPublishAsBundle failed");
77 NotificationSts::ThrowErrorWithMsg(env, "AniPublishAsBundle ERROR_INTERNAL_ERROR");
78 return;
79 }
80 notificationRequest->SetOwnerUserId(static_cast<int32_t>(userId));
81 notificationRequest->SetOwnerBundleName(bundleStr);
82 int returncode = NotificationHelper::PublishNotification(*notificationRequest);
83 if (returncode != ERR_OK) {
84 int externalCode = NotificationSts::GetExternalCode(returncode);
85 ANS_LOGE("AniPublishAsBundle: PublishNotificationerror, errorCode: %{public}d", externalCode);
86 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
87 }
88
89 ANS_LOGD("AniPublishAsBundle end");
90 }
91
AniPublishAsBundleWithBundleOption(ani_env * env,ani_object representativeBundle,ani_object request)92 void AniPublishAsBundleWithBundleOption(ani_env *env, ani_object representativeBundle, ani_object request)
93 {
94 ANS_LOGE("AniPublishAsBundleWithBundleOption enter");
95 std::shared_ptr<NotificationRequest> notificationRequest = std::make_shared<NotificationRequest>();
96 if (NotificationSts::UnWarpNotificationRequest(env, request, notificationRequest) != ANI_OK) {
97 NotificationSts::ThrowErrorWithMsg(env, "AniPublishAsBundleWithBundleOption ERROR_INTERNAL_ERROR");
98 return;
99 }
100
101 BundleOption option;
102 if (NotificationSts::UnwrapBundleOption(env, representativeBundle, option) != true) {
103 NotificationSts::ThrowErrorWithMsg(env, "UnwrapBundleOption ERROR_INTERNAL_ERROR");
104 return;
105 }
106
107 ANS_LOGD("AniPublishAsBundleWithBundleOption: bundle %{public}s uid: %{public}d",
108 option.GetBundleName().c_str(), option.GetUid());
109 notificationRequest->SetOwnerBundleName(option.GetBundleName());
110 notificationRequest->SetOwnerUid(option.GetUid());
111 notificationRequest->SetIsAgentNotification(true);
112
113 int returncode = NotificationHelper::PublishNotification(*notificationRequest);
114 if (returncode != ERR_OK) {
115 int externalCode = NotificationSts::GetExternalCode(returncode);
116 ANS_LOGE("AniPublishAsBundleWithBundleOption error, errorCode: %{public}d", externalCode);
117 OHOS::NotificationSts::ThrowError(env, externalCode, NotificationSts::FindAnsErrMsg(externalCode));
118 }
119 ANS_LOGD("AniPublishAsBundleWithBundleOption end");
120 }
121 } // namespace NotificationManagerSts
122 } // namespace OHOS