openharmony 鸿蒙 js-apis-notificationExtensionSubscription-sys

2026-08-25 浏览 (1)

@ohos.notificationExtensionSubscription (notificationExtensionSubscription) (System API)

The notificationExtensionSubscription module provides capabilities for managing notification extension, including opening the extension settings screen, subscribing to/unsubscribing from notification extension, and obtaining/setting the notification authorization status.

NOTE

The initial APIs of this module are supported since API version 22. Newly added APIs will be marked with a superscript to indicate their earliest API version.

This topic describes only the system APIs provided by the module. For details about its public APIs, see notificationExtensionSubscription.

Modules to Import

import { notificationExtensionSubscription } from '@kit.NotificationKit';
import { BusinessError } from '@kit.BasicServicesKit';

notificationExtensionSubscription.getAllSubscriptionBundles

getAllSubscriptionBundles(): Promise<BundleOption[]>

Obtains all applications that have requested the ohos.permission.SUBSCRIBE_NOTIFICATION permission and implemented NotificationSubscriberExtensionAbility. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API.

Return value

TypeDescription
Promise<BundleOption[]>Promise used to return the applications that have requested the ohos.permission.SUBSCRIBE_NOTIFICATION permission and implemented NotificationSubscriberExtensionAbility.

Error codes

For details about the error codes, see Universal Error Codes and Notification Error Codes.

IDError Message
201Permission denied.
202Not system application to call the interface.
1600001Internal error.
1600003Failed to connect to the service.

Example

notificationExtensionSubscription.getAllSubscriptionBundles().then((data: notificationExtensionSubscription.BundleOption[]) => {
  console.info(`getAllSubscriptionBundles successfully. Data: ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
  console.error(`getAllSubscriptionBundles fail: ${JSON.stringify(err)}`);
});

notificationExtensionSubscription.getUserGrantedState

getUserGrantedState(targetBundle: BundleOption): Promise<boolean>

Obtains the enabling state of the Allow access to notifications on this device switch of a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
targetBundleBundleOptionYesInformation about the target application. The application must have requested the ohos.permission.SUBSCRIBE_NOTIFICATION permission and implemented NotificationSubscriberExtensionAbility. Otherwise, error code 1600022 is returned.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. The value true indicates that the device notification access is enabled, and false indicates the opposite.

Error codes

For details about the error codes, see Universal Error Codes and Notification Error Codes.

IDError Message
201Permission denied.
202Not system application to call the interface.
1600001Internal error.
1600003Failed to connect to the service.
1600022The specified bundle is invalid.

Example

let targetBundle: notificationExtensionSubscription.BundleOption =
{
  // Use the actual target application information.
  bundle: 'com.example.testnotification',
};
notificationExtensionSubscription.getUserGrantedState(targetBundle).then((isOpen: boolean) => {
  if (isOpen) {
    console.info('GrantedState true');
  } else {
    console.info('GrantedState false');
  }
}).catch((err: BusinessError) => {
  console.error(`getUserGrantedState fail: ${JSON.stringify(err)}`);
});

notificationExtensionSubscription.setUserGrantedState

setUserGrantedState(targetBundle: BundleOption, enabled: boolean): Promise<void>

Sets the enabling state of the Allow access to notifications on this device switch for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
targetBundleBundleOptionYesInformation about the target application. The application must have requested the ohos.permission.SUBSCRIBE_NOTIFICATION permission and implemented NotificationSubscriberExtensionAbility. Otherwise, error code 1600022 is returned.
enablebooleanYesWhether to enable the device notification access. The value true indicates that this functionality is enabled, and false indicates the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Notification Error Codes.

IDError Message
201Permission denied.
202Not system application to call the interface.
1600001Internal error.
1600003Failed to connect to the service.
1600022The specified bundle is invalid.

Example

let targetBundle: notificationExtensionSubscription.BundleOption =
{
  // Use the actual target application information.
  bundle: 'com.example.testnotification',
};
notificationExtensionSubscription.setUserGrantedState(targetBundle, true).then(() => {
  console.info(`setUserGrantedState successfully.`);
}).catch((err: BusinessError) => {
  console.error(`setUserGrantedState fail: ${JSON.stringify(err)}`);
});

notificationExtensionSubscription.getUserGrantedEnabledBundles

getUserGrantedEnabledBundles(targetBundle: BundleOption): Promise<BundleOption[]>

Obtains the applications that are allowed to access device notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
targetBundleBundleOptionYesInformation about the target application. The application must have requested the ohos.permission.SUBSCRIBE_NOTIFICATION permission and implemented NotificationSubscriberExtensionAbility. Otherwise, error code 1600022 is returned.

Return value

TypeDescription
Promise<BundleOption[]>Promise used to return the applications obtained.

Error codes

For details about the error codes, see Universal Error Codes and Notification Error Codes.

IDError Message
201Permission denied.
202Not system application to call the interface.
1600001Internal error.
1600003Failed to connect to the service.
1600022The specified bundle is invalid.

Example

let targetBundle: notificationExtensionSubscription.BundleOption =
{
  // Use the actual target application information.
  bundle: 'com.example.testnotification',
};
notificationExtensionSubscription.getUserGrantedEnabledBundles(targetBundle).then((data: notificationExtensionSubscription.BundleOption[]) => {
  console.info(`getUserGrantedEnabledBundles successfully. Data: ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
  console.error(`getUserGrantedEnabledBundles fail: ${JSON.stringify(err)}`);
});

notificationExtensionSubscription.setUserGrantedBundleState

setUserGrantedBundleState(targetBundle: BundleOption, enabledBundles: BundleOption[], enabled:boolean): Promise<void>

Sets the enabling state of device notification access for the specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
targetBundleBundleOptionYesInformation about the target application. The application must have requested the ohos.permission.SUBSCRIBE_NOTIFICATION permission and implemented NotificationSubscriberExtensionAbility. Otherwise, error code 1600022 is returned.
enabledBundlesBundleOption[]YesAuthorized applications.
enabledbooleanYesWhether the device notification access for the specified application is enabled. The value true indicates that this functionality is enabled, and false indicates the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Notification Error Codes.

IDError Message
201Permission denied.
202Not system application to call the interface.
1600001Internal error.
1600003Failed to connect to the service.
1600022The specified bundle is invalid.

Example

let targetBundle: notificationExtensionSubscription.BundleOption =
  {
    // Use the actual target application information.
    bundle: 'com.example.testnotification',
  };
let enabledBundles: notificationExtensionSubscription.BundleOption[] = [
  // Use the actual source application information.
  { bundle: 'com.example.xxx', uid: 11111111 },
  { bundle: 'com.example.xxxx', uid: 11111111 },
  { bundle: 'com.example.xxxxx' },
];
notificationExtensionSubscription.setUserGrantedBundleState(targetBundle, enabledBundles, true).then(() => {
  console.info(`setUserGrantedBundleState successfully.`);
}).catch((err: BusinessError) => {
  console.error(`setUserGrantedBundleState fail: ${JSON.stringify(err)}`);
});

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 js-apis-inner-notification-notificationFlags

openharmony 鸿蒙 js-apis-notificationSubscribe-sys

openharmony 鸿蒙 js-apis-notificationSubscriberExtensionAbility

openharmony 鸿蒙 errorcode-notification

openharmony 鸿蒙 js-apis-inner-notification-notificationExtensionContent

openharmony 鸿蒙 js-apis-notification-sys

openharmony 鸿蒙 js-apis-inner-notification-notificationContent-sys

openharmony 鸿蒙 js-apis-inner-notification-notificationCommonDef

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 js-apis-inner-notification-notificationTemplate

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/Ydg0hnVo