diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index bd450bedff34e17e6c4c509c50a1e2e3174f6597..7a07865223977f7bb4415a92459be70240b7eaac 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -167,6 +167,11 @@ ErrCode NotificationHelper::SubscribeNotification(const NotificationSubscriber & return DelayedSingleton::GetInstance()->SubscribeNotification(subscriber); } +ErrCode NotificationHelper::SubscribeNotificationSelf(const NotificationSubscriber &subscriber) +{ + return DelayedSingleton::GetInstance()->SubscribeNotificationSelf(subscriber); +} + ErrCode NotificationHelper::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber) { return DelayedSingleton::GetInstance()->SubscribeLocalLiveViewNotification(subscriber); diff --git a/frameworks/ans/src/notification_local_live_view_content.cpp b/frameworks/ans/src/notification_local_live_view_content.cpp index 12d770e8e91f54b04a1d125e0862886ed2b9bc17..dd5775ebe24ceb0fb3e4b3a28ee2f7eca89a3adf 100644 --- a/frameworks/ans/src/notification_local_live_view_content.cpp +++ b/frameworks/ans/src/notification_local_live_view_content.cpp @@ -18,6 +18,7 @@ #include #include // for basic_string, operator+ #include // for min +#include #include "ans_log_wrapper.h" #include "nlohmann/json.hpp" // for json, basic_json<>::obje... @@ -83,6 +84,22 @@ NotificationTime NotificationLocalLiveViewContent::GetTime() return time_; } +void NotificationLocalLiveViewContent::addFlag(int32_t flag) +{ + flags_.emplace_back(flag); +} + +bool NotificationLocalLiveViewContent::isFlagExist(int32_t flag) +{ + auto it = std::find(flags_.begin(), flags_.end(), flag); + if (it != flags_.end()) { + return true; + } else { + return false; + } + +} + std::string NotificationLocalLiveViewContent::Dump() { return "NotificationLocalLiveViewContent{ " + NotificationBasicContent::Dump() + @@ -130,6 +147,7 @@ bool NotificationLocalLiveViewContent::ToJson(nlohmann::json &jsonObject) const jsonObject["button"] = buttonObj; jsonObject["progress"] = progressObj; jsonObject["time"] = timeObj; + jsonObject["flags"] = nlohmann::json(flags_); return true; } @@ -194,6 +212,10 @@ NotificationLocalLiveViewContent *NotificationLocalLiveViewContent::FromJson(con } } + if (jsonObject.find("flags") != jsonEnd && jsonObject.at("flags").is_array()) { + pContent->flags_ = jsonObject.at("flags").get>(); + } + return pContent; } @@ -229,6 +251,11 @@ bool NotificationLocalLiveViewContent::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteInt32Vector(flags_)) { + ANS_LOGE("Failed to write flags"); + return false; + } + return true; } @@ -291,6 +318,11 @@ bool NotificationLocalLiveViewContent::ReadFromParcel(Parcel &parcel) delete pTime; pTime = nullptr; + if (!parcel.ReadInt32Vector(&flags_)) { + ANS_LOGE("Failed to read flags"); + return false; + } + return true; } } // namespace Notification diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index b8301d2d671a1bb047206b34a4b39ee8ebbe56bb..8c95f8a1d72657b4252e9e44ae1dfe9d28b7f1fd 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -414,6 +414,14 @@ public: virtual ErrCode Subscribe(const sptr &subscriber, const sptr &info) = 0; + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode SubscribeSelf(const sptr &subscriber) = 0; + /** * @brief Subscribes local live view notifications. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 7bbc92b249f9caea78254d1ba96f510e93f8e7f9..dfff30d41a70b9e2c0b3720e8980c24287451b86 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -403,6 +403,15 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @param info Indicates the NotificationSubscribeInfo object. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SubscribeSelf(const sptr &subscriber) override; + /** * @brief Subscribes notifications. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index 98545d6c4263c0fe9f36ed8a1ba0333b43d059ad..90972b194bf7feb5ea324de80cb3845054b97ee2 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -408,6 +408,14 @@ public: */ virtual ErrCode Subscribe( const sptr &subscriber, const sptr &info) override; + + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @return Returns ERR_OK on success, others on failure. + */ + virtual ErrCode SubscribeSelf(const sptr &subscriber) override; virtual ErrCode SubscribeLocalLiveView( const sptr &subscriber, @@ -789,6 +797,7 @@ private: ErrCode HandleUnregisterPushCallback(MessageParcel &data, MessageParcel &reply); ErrCode HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply); ErrCode HandleTriggerLocalLiveView(MessageParcel &data, MessageParcel &reply); + ErrCode HandleSubscribeSelf(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result) diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index fcad82aee0ba2aec886987e8faffe963d8a8b4b1..20d34927d318d12e8c746715fb6d485a374bdbbd 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -302,6 +302,19 @@ public: */ ErrCode SubscribeNotification(const NotificationSubscriber &subscriber); + /** + * @brief Subscribes to notifications from the appliaction self. + * @note To subscribe to a notification, inherit the {NotificationSubscriber} class, override its + * callback methods and create a subscriber. The subscriber will be used as a parameter of this method. + * After the notification is published, subscribers that meet the filter criteria can receive the + * notification. + * + * @param subscriber Indicates the {NotificationSubscriber} to receive notifications. + * This parameter must be specified. + * @return Returns subscribe notification result. + */ + ErrCode SubscribeNotificationSelf(const NotificationSubscriber &subscriber); + /** * @brief Subscribes liveView notification. This method can be called only by applications * with required system permissions. diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 3d1d2201c00c5388f05a3eb00c88b4495b8b08fa..65229f203b5795fbc5bae70c100287210d50a494 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -123,6 +123,7 @@ namespace Notification { ON_RESPONSE, SUBSCRIBE_LOCAL_LIVE_VIEW_NOTIFICATION, TRIGGER_LOCAL_LIVE_VIEW_NOTIFICATION, + SUBSCRIBE_NOTIFICATION_SELF, }; } } diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 22c05e5c1173212ef32c043ed7d44147b068c793..9d5c85c959aeffc4153a9657dc69532bca0b6b6d 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -1473,7 +1473,6 @@ ErrCode AnsManagerProxy::Subscribe(const sptr &subscribe return ERR_ANS_PARCELABLE_FAILED; } } - MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION, option, data, reply); @@ -1490,6 +1489,41 @@ ErrCode AnsManagerProxy::Subscribe(const sptr &subscribe return result; } +ErrCode AnsManagerProxy::SubscribeSelf(const sptr &subscriber) +{ + if (subscriber == nullptr) { + ANS_LOGE("[SubscribeSelf] fail: subscriber is empty."); + return ERR_ANS_INVALID_PARAM; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[SubscribeSelf] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool ret = data.WriteRemoteObject(subscriber->AsObject()); + if (!ret) { + ANS_LOGE("[SubscribeSelf] fail: write subscriber failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::SUBSCRIBE_NOTIFICATION_SELF, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[SubscribeSelf] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[SubscribeSelf] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index b412ab0dfe3013cd1cd5027683fc317e69597dfb..0b1ec17e02e8becf0561ceca69761f296963ed7e 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -1192,6 +1192,22 @@ ErrCode AnsManagerStub::HandleSubscribe(MessageParcel &data, MessageParcel &repl return ERR_OK; } +ErrCode AnsManagerStub::HandleSubscribeSelf(MessageParcel &data, MessageParcel &reply) +{ + sptr subscriber = data.ReadRemoteObject(); + if (subscriber == nullptr) { + ANS_LOGE("[HandleSubscribeSelf] fail: read subscriber failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = SubscribeSelf(iface_cast(subscriber)); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleSubscribeSelf] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + return ERR_OK; +} + ErrCode AnsManagerStub::HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply) { sptr subscriber = data.ReadRemoteObject(); @@ -2158,6 +2174,12 @@ ErrCode AnsManagerStub::Subscribe(const sptr &subscriber return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::SubscribeSelf(const sptr &subscriber) +{ + ANS_LOGE("AnsManagerStub::SubscribeSelf called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) { diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 44b3f3f6edcb6844f57c88723d9afc01aedaf4fd..34c7670d6ad52043925a7f95d88abeec58c98aa4 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -18,6 +18,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "hitrace_meter_adapter.h" +#include "ipc_skeleton.h" #include "iservice_registry.h" #include "notification_button_option.h" #include "notification_local_live_view_subscriber.h" @@ -386,6 +387,22 @@ ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &sub return ansManagerProxy_->Subscribe(subscriberSptr, nullptr); } +ErrCode AnsNotification::SubscribeNotificationSelf(const NotificationSubscriber &subscriber) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr subscriberSptr = subscriber.GetImpl(); + if (subscriberSptr == nullptr) { + ANS_LOGE("Failed to subscribeSelf with SubscriberImpl null ptr."); + return ERR_ANS_INVALID_PARAM; + } + return ansManagerProxy_->SubscribeSelf(subscriberSptr); +} + ErrCode AnsNotification::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 7fd5e12b22064b0a72fcdb5940bf9c73468922c4..bab59c2baa65d2e1828d99d061cc9f06b694543c 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -249,6 +249,11 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode SubscribeSelf(const sptr &subscriber) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode SubscribeLocalLiveView(const sptr &subscriber, const sptr &info) override { diff --git a/frameworks/js/napi/include/subscribe/napi_subscribe.h b/frameworks/js/napi/include/subscribe/napi_subscribe.h index 5bd699e838335f5f4f39d028e5c7251bfdf0f461..04640156824e42759488d40741043294c496fd41 100644 --- a/frameworks/js/napi/include/subscribe/napi_subscribe.h +++ b/frameworks/js/napi/include/subscribe/napi_subscribe.h @@ -22,6 +22,7 @@ namespace NotificationNapi { using namespace OHOS::Notification; napi_value NapiSubscribe(napi_env env, napi_callback_info info); +napi_value NapiSubscribeSelf(napi_env env, napi_callback_info info); napi_value NapiUnsubscribe(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index f46b609b56c6828a033009eb84a0c9d5769371a7..60359e048b8d0d2190f46ff3f7581dfc58ad0166 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -1019,40 +1019,48 @@ napi_value Common::SetNotificationLocalLiveViewContent( napi_set_named_property(env, result, "typeCode", value); // capsule: NotificationCapsule - napi_value capsule = nullptr; - napi_create_object(env, &capsule); - if (!SetCapsule(env, localLiveViewContent->GetCapsule(), capsule)) { - ANS_LOGE("SetCapsule call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE)) { + napi_value capsule = nullptr; + napi_create_object(env, &capsule); + if (!SetCapsule(env, localLiveViewContent->GetCapsule(), capsule)) { + ANS_LOGE("SetCapsule call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "capsule", capsule); } - napi_set_named_property(env, result, "capsule", capsule); // button: NotificationLocalLiveViewButton - napi_value button = nullptr; - napi_create_object(env, &button); - if (!SetButton(env, localLiveViewContent->GetButton(), button)) { - ANS_LOGE("SetButton call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON)) { + napi_value button = nullptr; + napi_create_object(env, &button); + if (!SetButton(env, localLiveViewContent->GetButton(), button)) { + ANS_LOGE("SetButton call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "button", button); } - napi_set_named_property(env, result, "button", button); // progress: NotificationProgress - napi_value progress = nullptr; - napi_create_object(env, &progress); - if (!SetProgress(env, localLiveViewContent->GetProgress(), progress)) { - ANS_LOGE("SetProgress call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS)) { + napi_value progress = nullptr; + napi_create_object(env, &progress); + if (!SetProgress(env, localLiveViewContent->GetProgress(), progress)) { + ANS_LOGE("SetProgress call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "progress", progress); } - napi_set_named_property(env, result, "progress", progress); - + // time: NotificationTime - napi_value time = nullptr; - napi_create_object(env, &time); - if (!SetTime(env, localLiveViewContent->GetTime(), time)) { - ANS_LOGE("SetMessageUser call failed"); - return NapiGetBoolean(env, false); + if (localLiveViewContent->isFlagExist(NotificationLocalLiveViewContent::LiveViewContentInner::TIME)) { + napi_value time = nullptr; + napi_create_object(env, &time); + if (!SetTime(env, localLiveViewContent->GetTime(), time)) { + ANS_LOGE("SetMessageUser call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "time", time); } - napi_set_named_property(env, result, "time", time); return NapiGetBoolean(env, true); } @@ -4414,6 +4422,7 @@ napi_value Common::GetNotificationLocalLiveViewCapsule( } content->SetCapsule(capsule); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::CAPSULE); return NapiGetNull(env); } @@ -4493,6 +4502,7 @@ napi_value Common::GetNotificationLocalLiveViewButton( } ANS_LOGD("button buttonIcon = %{public}s", str); content->SetButton(button); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::BUTTON); return NapiGetNull(env); } @@ -4558,6 +4568,8 @@ napi_value Common::GetNotificationLocalLiveViewProgress(const napi_env &env, con } content->SetProgress(progress); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::PROGRESS); + return NapiGetNull(env); } @@ -4635,6 +4647,8 @@ napi_value Common::GetNotificationLocalLiveViewTime(const napi_env &env, const n } content->SetTime(time); + content->addFlag(NotificationLocalLiveViewContent::LiveViewContentInner::TIME); + return NapiGetNull(env); } diff --git a/frameworks/js/napi/src/subscribe/init_module.cpp b/frameworks/js/napi/src/subscribe/init_module.cpp index 89f17d11e9287bd27f86487d999b87ec2c5dac2a..8a0c21680188fecfa2b373521e2456297149dc2b 100644 --- a/frameworks/js/napi/src/subscribe/init_module.cpp +++ b/frameworks/js/napi/src/subscribe/init_module.cpp @@ -34,6 +34,7 @@ napi_value NotificationSubscribeInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("unsubscribe", NapiUnsubscribe), DECLARE_NAPI_FUNCTION("remove", NapiRemove), DECLARE_NAPI_FUNCTION("removeAll", NapiRemoveAll), + DECLARE_NAPI_FUNCTION("subscribeSelf", NapiSubscribeSelf), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/frameworks/js/napi/src/subscribe/napi_subscribe.cpp b/frameworks/js/napi/src/subscribe/napi_subscribe.cpp index eed5bcde2977be0db52708f1551a818f76a287b1..6773be6fa451fc2cab85f79367537947ba03aea9 100644 --- a/frameworks/js/napi/src/subscribe/napi_subscribe.cpp +++ b/frameworks/js/napi/src/subscribe/napi_subscribe.cpp @@ -121,6 +121,97 @@ napi_value NapiSubscribe(napi_env env, napi_callback_info info) } } +napi_value NapiSubscribeSelf(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + napi_ref callback = nullptr; + SubscriberInstance *objectInfo = nullptr; + NotificationSubscribeInfo subscriberInfo; + if (ParseParameters(env, info, subscriberInfo, objectInfo, callback) == nullptr) { + if (objectInfo) { + delete objectInfo; + objectInfo = nullptr; + } + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoSubscribe *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoSubscribe { + .env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .subscriberInfo = subscriberInfo + }; + if (!asynccallbackinfo) { + if (objectInfo) { + delete objectInfo; + objectInfo = nullptr; + } + return Common::JSParaError(env, callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "subscribeNotificationSelf", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("NapiSubscribeSelf work excute."); + if (!data) { + ANS_LOGE("Invalid asynccallbackinfo!"); + return; + } + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::SubscribeNotificationSelf(*(asynccallbackinfo->objectInfo)); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("NapiSubscribeSelf work complete."); + if (!data) { + ANS_LOGE("Invalid asynccallbackinfo!"); + return; + } + auto asynccallbackinfo = reinterpret_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete napiSubscribeSelf callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGD("NapiSubscribeSelf work complete end."); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + bool isCallback = asynccallbackinfo->info.isCallback; + napi_status status = napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + if (status != napi_ok) { + ANS_LOGE("Queue napiSubscribeSelf work failed return: %{public}d", status); + asynccallbackinfo->info.errorCode = ERROR_INTERNAL_ERROR; + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete napiSubscribeSelf callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + + if (isCallback) { + ANS_LOGD("NapiSubscribeSelf callback is nullptr."); + return Common::NapiGetNull(env); + } else { + return promise; + } +} + napi_value NapiUnsubscribe(napi_env env, napi_callback_info info) { ANS_LOGI("Unsubscribe start"); diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 410eb51360269ebb627fe72b5dc935fd1251456d..1958d744ee15865ef6063f083235b235dc8dfe62 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -305,6 +305,19 @@ public: */ static ErrCode SubscribeNotification(const NotificationSubscriber &subscriber); + /** + * @brief Subscribes to notifications from the appliaction self. + * @note To subscribe to a notification, inherit the {NotificationSubscriber} class, override its + * callback methods and create a subscriber. The subscriber will be used as a parameter of this method. + * After the notification is published, subscribers that meet the filter criteria can receive the + * notification. + * + * @param subscriber Indicates the {NotificationSubscriber} to receive notifications. + * This parameter must be specified. + * @return Returns unsubscribe notification result. + */ + static ErrCode SubscribeNotificationSelf(const NotificationSubscriber &subscriber); + /** * @brief Subscribes to all notifications based on the filtering criteria. This method can be called only * by applications with required system permissions. diff --git a/interfaces/inner_api/notification_local_live_view_content.h b/interfaces/inner_api/notification_local_live_view_content.h index 7807c1118b06ace876c7d7a4e9e6b5db6ea44eb5..f2f8cb2ed650358afd97ca51b67e4cfed9711188 100644 --- a/interfaces/inner_api/notification_local_live_view_content.h +++ b/interfaces/inner_api/notification_local_live_view_content.h @@ -25,11 +25,19 @@ #include "notification_json_convert.h" #include "notification_time.h" #include "parcel.h" +#include namespace OHOS { namespace Notification { class NotificationLocalLiveViewContent : public NotificationBasicContent { public: + enum LiveViewContentInner { + CAPSULE = 1, + BUTTON, + PROGRESS, + TIME, + }; + NotificationLocalLiveViewContent() = default; ~NotificationLocalLiveViewContent() = default; @@ -100,6 +108,21 @@ public: */ NotificationTime GetTime(); + /* + * @add flag function. + * + * @param flag Indicates the flag to be added. + + */ + void addFlag(int32_t flag); + + /* + * @return is the given flag exist. + * + * @param flag Indicates the flag to be added. + */ + bool isFlagExist(int32_t flag); + /** * @brief Returns a string representation of the object. * @@ -154,6 +177,7 @@ private: NotificationLocalLiveViewButton button_ {}; NotificationProgress progress_ {}; NotificationTime time_ {}; + std::vector flags_ {}; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index c79f9f0c3e2bcf3a645a50ce39302709ac44b88e..cfbd791d1155c95093b97a4f0d37db42b3161188 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -429,6 +429,14 @@ public: ErrCode Subscribe(const sptr &subscriber, const sptr &info) override; + /** + * @brief Subscribes notifications self. + * + * @param subscriber Indicates the subscriber. + * @return Returns ERR_OK on success, others on failure. + */ + ErrCode SubscribeSelf(const sptr &subscriber) override; + /** * @brief Subscribes notifications. * diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index a03e1b9847868c167d7ccf199a08068bcb2f01f8..caf0bc2b11a285637398b00c6622608f2d2a34ea 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1728,6 +1728,45 @@ ErrCode AdvancedNotificationService::Subscribe( return errCode; } +ErrCode AdvancedNotificationService::SubscribeSelf(const sptr &subscriber) +{ + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + sptr sptrInfo = new (std::nothrow) NotificationSubscribeInfo(); + ErrCode errCode = ERR_OK; + do { + if (subscriber == nullptr) { + errCode = ERR_ANS_INVALID_PARAM; + break; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("Client is not a system app or subsystem"); + errCode = ERR_ANS_NON_SYSTEM_APP; + break; + } + + int32_t uid = IPCSkeleton().GetCallingUid(); + // subscribeSelf doesn't need OHOS_PERMISSION_NOTIFICATION_CONTROLLER permission + std::string bundle; + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + bundle = bundleManager->GetBundleNameByUid(uid); + } + + sptrInfo->AddAppName(bundle); + + errCode = NotificationSubscriberManager::GetInstance()->AddSubscriber(subscriber, sptrInfo); + if (errCode != ERR_OK) { + break; + } + } while (0); + + SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), sptrInfo, errCode); + return errCode; +} + ErrCode AdvancedNotificationService::SubscribeLocalLiveView( const sptr &subscriber, const sptr &info) {