# @ohos.notificationManager (NotificationManager模块) 本模块提供通知管理的能力,包括发布、取消发布通知,创建、获取、移除通知渠道,获取通知的使能状态、角标使能状态,获取通知的相关信息等。 > **说明:** > > 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 ## 导入模块 ```ts import { notificationManager } from '@kit.NotificationKit'; ``` ## notificationManager.publish publish(request: NotificationRequest, callback: AsyncCallback\): void 发布通知。使用callback异步回调。 如果新发布通知与已发布通知的ID相同,且label相同,则新通知将取代原有通知。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------------- | ---- | ------------------------------------------- | | request | [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) | 是 | 用于设置要发布通知的内容和相关配置信息。 | | callback | AsyncCallback\ | 是 | 发布通知的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600004 | Notification disabled. | | 1600005 | Notification slot disabled. | | 1600007 | The notification does not exist. | | 1600009 | The notification sending frequency reaches the upper limit. | | 1600012 | No memory space. | | 1600014 | No permission. | | 1600015 | The current notification status does not support duplicate configurations. | | 1600016 | The notification version for this update is too low. | | 2300007 | Network unreachable. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // publish回调 let publishCallback = (err: BusinessError): void => { if (err) { console.error(`publish failed, code is ${err.code}, message is ${err.message}`); } else { console.info("publish success"); } } // 通知Request对象 let notificationRequest: notificationManager.NotificationRequest = { id: 1, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: "test_title", text: "test_text", additionalText: "test_additionalText" } } }; notificationManager.publish(notificationRequest, publishCallback); ``` ## notificationManager.publish publish(request: NotificationRequest): Promise\ 发布通知。使用Promise异步回调。 如果新发布通知与已发布通知的ID相同,且label相同,则新通知将取代原有通知。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------------- | ---- | ------------------------------------------- | | request | [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) | 是 | 用于设置要发布通知的内容和相关配置信息。 | **返回值:** | 类型 | 说明 | | ------- |--| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600004 | Notification disabled. | | 1600005 | Notification slot disabled. | | 1600007 | The notification does not exist. | | 1600009 | The notification sending frequency reaches the upper limit. | | 1600012 | No memory space. | | 1600014 | No permission. | | 1600015 | The current notification status does not support duplicate configurations. | | 1600016 | The notification version for this update is too low. | | 2300007 | Network unreachable. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 通知Request对象 let notificationRequest: notificationManager.NotificationRequest = { id: 1, content: { notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: "test_title", text: "test_text", additionalText: "test_additionalText" } } }; notificationManager.publish(notificationRequest).then(() => { console.info("publish success"); }).catch((err: BusinessError) => { console.error(`publish fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.cancel cancel(id: number, label: string, callback: AsyncCallback\): void 通过通知ID和通知标签取消已发布的通知。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | -------------------- | | id | number | 是 | 通知ID。 | | label | string | 是 | 通知标签。 | | callback | AsyncCallback\ | 是 | 表示被指定通知的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600007 | The notification does not exist. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // cancel回调 let cancelCallback = (err: BusinessError): void => { if (err) { console.error(`cancel failed, code is ${err.code}, message is ${err.message}`); } else { console.info("cancel success"); } } notificationManager.cancel(0, "label", cancelCallback); ``` ## notificationManager.cancel cancel(id: number, label?: string): Promise\ 通过通知ID和通知标签取消已发布的通知,若label为空表示取消与指定通知ID相匹配的已发布通知。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----- | ------ | ---- | -------- | | id | number | 是 | 通知ID。 | | label | string | 否 | 通知标签,默认为空。 | **返回值:** | 类型 | 说明 | | ------- |-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600007 | The notification does not exist. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.cancel(0).then(() => { console.info("cancel success"); }).catch((err: BusinessError) => { console.error(`cancel fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.cancel cancel(id: number, callback: AsyncCallback\): void 取消与指定通知ID相匹配的已发布通知。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | -------------------- | | id | number | 是 | 通知ID。 | | callback | AsyncCallback\ | 是 | 表示被指定通知的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600007 | The notification does not exist. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // cancel回调 let cancelCallback = (err: BusinessError): void => { if (err) { console.error(`cancel failed, code is ${err.code}, message is ${err.message}`); } else { console.info("cancel success"); } } notificationManager.cancel(0, cancelCallback); ``` ## notificationManager.cancelAll cancelAll(callback: AsyncCallback\): void 取消当前应用所有已发布的通知。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | -------------------- | | callback | AsyncCallback\ | 是 | 表示被指定通知的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // cancel回调 let cancelAllCallback = (err: BusinessError): void => { if (err) { console.error(`cancelAll failed, code is ${err.code}, message is ${err.message}`); } else { console.info("cancelAll success"); } } notificationManager.cancelAll(cancelAllCallback); ``` ## notificationManager.cancelAll cancelAll(): Promise\ 取消当前应用所有已发布的通知。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | | ------- |-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.cancelAll().then(() => { console.info("cancelAll success"); }).catch((err: BusinessError) => { console.error(`cancelAll fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.addSlot addSlot(type: SlotType, callback: AsyncCallback\): void 创建指定类型的通知渠道。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | ---------------------- | | type | [SlotType](#slottype) | 是 | 要创建的通知渠道的类型。 | | callback | AsyncCallback\ | 是 | 表示被指定通道的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600012 | No memory space. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // addslot回调 let addSlotCallBack = (err: BusinessError): void => { if (err) { console.error(`addSlot failed, code is ${err.code}, message is ${err.message}`); } else { console.info("addSlot success"); } } notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION, addSlotCallBack); ``` ## notificationManager.addSlot addSlot(type: SlotType): Promise\ 创建指定类型的通知渠道。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | -------- | ---- | ---------------------- | | type | [SlotType](#slottype) | 是 | 要创建的通知渠道的类型。 | **返回值:** | 类型 | 说明 | | ------- |-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600012 | No memory space. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION).then(() => { console.info("addSlot success"); }).catch((err: BusinessError) => { console.error(`addSlot fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.getSlot getSlot(slotType: SlotType, callback: AsyncCallback\): void 获取一个指定类型的通知渠道。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------- | ---- | ----------------------------------------------------------- | | slotType | [SlotType](#slottype) | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。 | | callback | AsyncCallback\<[NotificationSlot](js-apis-inner-notification-notificationSlot.md)\> | 是 | 表示被指定通道的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // getSlot回调 let getSlotCallback = (err: BusinessError, data: notificationManager.NotificationSlot): void => { if (err) { console.error(`getSlot failed, code is ${err.code}, message is ${err.message}`); } else { console.info(`getSlot success, data is ${JSON.stringify(data)}`); } } let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; notificationManager.getSlot(slotType, getSlotCallback); ``` ## notificationManager.getSlot getSlot(slotType: SlotType): Promise\ 获取一个指定类型的通知渠道。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | ---- | ----------------------------------------------------------- | | slotType | [SlotType](#slottype) | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。 | **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- | ------------------------------------------------------------ | | Promise\<[NotificationSlot](js-apis-inner-notification-notificationSlot.md)\> | 以Promise形式返回获取一个通知渠道。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; notificationManager.getSlot(slotType).then((data: notificationManager.NotificationSlot) => { console.info("getSlot success, data: " + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error(`getSlot fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.getSlots getSlots(callback: AsyncCallback\>): void 获取此应用程序的所有通知渠道。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------- | ---- | -------------------- | | callback | AsyncCallback\\> | 是 | 以callback形式返回获取此应用程序的所有通知渠道的结果。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // getSlots回调 let getSlotsCallback = (err: BusinessError, data: Array): void => { if (err) { console.error(`getSlots failed, code is ${err.code}, message is ${err.message}`); } else { console.info(`getSlots success, data is ${JSON.stringify(data)}`); } } notificationManager.getSlots(getSlotsCallback); ``` ## notificationManager.getSlots getSlots(): Promise\> 获取此应用程序的所有通知渠道。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- | ------------------------------------------------------------ | | Promise\\> | 以Promise形式返回获取此应用程序的所有通知渠道的结果。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.getSlots().then((data: Array) => { console.info("getSlots success, data: " + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error(`getSlots fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.removeSlot removeSlot(slotType: SlotType, callback: AsyncCallback\): void 删除此应用程序指定类型的通知渠道。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | ----------------------------------------------------------- | | slotType | [SlotType](#slottype) | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。 | | callback | AsyncCallback\ | 是 | 表示被指定通道的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // removeSlot回调 let removeSlotCallback = (err: BusinessError): void => { if (err) { console.error(`removeSlot failed, code is ${err.code}, message is ${err.message}`); } else { console.info("removeSlot success"); } } let slotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; notificationManager.removeSlot(slotType, removeSlotCallback); ``` ## notificationManager.removeSlot removeSlot(slotType: SlotType): Promise\ 删除此应用程序指定类型的通知渠道。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | ---- | ----------------------------------------------------------- | | slotType | [SlotType](#slottype) | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。 | **返回值:** | 类型 | 说明 | |---------|-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION; notificationManager.removeSlot(slotType).then(() => { console.info("removeSlot success"); }).catch((err: BusinessError) => { console.error(`removeSlot fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.removeAllSlots removeAllSlots(callback: AsyncCallback\): void 删除此应用程序所有通知渠道。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | -------------------- | | callback | AsyncCallback\ | 是 | 表示被指定通道的回调方法。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let removeAllSlotsCallback = (err: BusinessError): void => { if (err) { console.error(`removeAllSlots failed, code is ${err.code}, message is ${err.message}`); } else { console.info("removeAllSlots success"); } } notificationManager.removeAllSlots(removeAllSlotsCallback); ``` ## notificationManager.removeAllSlots removeAllSlots(): Promise\ 删除此应用程序所有通知渠道。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | |---------|-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.removeAllSlots().then(() => { console.info("removeAllSlots success"); }).catch((err: BusinessError) => { console.error(`removeAllSlots fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.isNotificationEnabled11+ isNotificationEnabled(callback: AsyncCallback\): void 获取通知使能状态。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | ------------------------ | | callback | AsyncCallback\ | 是 | 获取通知使能状态回调函数(true:使能,false:禁止)。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600008 | The user does not exist. | | 17700001 | The specified bundle name was not found. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let isNotificationEnabledCallback = (err: BusinessError, data: boolean): void => { if (err) { console.error(`isNotificationEnabled failed, code is ${err.code}, message is ${err.message}`); } else { console.info(`isNotificationEnabled success, data is ${JSON.stringify(data)}`); } } notificationManager.isNotificationEnabled(isNotificationEnabledCallback); ``` ## notificationManager.isNotificationEnabled11+ isNotificationEnabled(): Promise\ 获取通知使能状态。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- | ------------------------------------------------------------ | | Promise\ | 以Promise形式返回获取通知使能状态的结果(true:使能,false:禁止)。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600008 | The user does not exist. | | 17700001 | The specified bundle name was not found. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.isNotificationEnabled().then((data: boolean) => { console.info("isNotificationEnabled success, data: " + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error(`isNotificationEnabled fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.isNotificationEnabledSync12+ isNotificationEnabledSync(): boolean 同步获取通知使能状态。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- |--------------------------------------------------------- | | boolean | 返回获取通知使能状态的结果。返回true,表示通知使能状态为开;返回false,表示通知使能状态为关。 | **错误码:** 以下错误码的详细介绍请参见[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------------------- | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts let enabled = notificationManager.isNotificationEnabledSync(); console.info(`isNotificationEnabledSync success, data is : ${JSON.stringify(enabled)}`); ``` ## notificationManager.setBadgeNumber10+ setBadgeNumber(badgeNumber: number): Promise\ 设定角标个数,在应用的桌面图标上呈现。使用Promise异步回调。 当角标设定个数取值0时,表示清除角标。取值大于99时,通知角标将显示99+。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | ---------- | | badgeNumber | number | 是 | 角标个数。 | **返回值:** | 类型 | 说明 | |---------|-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600012 | No memory space. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let badgeNumber: number = 10; notificationManager.setBadgeNumber(badgeNumber).then(() => { console.info("setBadgeNumber success"); }).catch((err: BusinessError) => { console.error(`setBadgeNumber fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.setBadgeNumber10+ setBadgeNumber(badgeNumber: number, callback: AsyncCallback\): void 设定角标个数,在应用的桌面图标上呈现。使用callback异步回调。 当角标设定个数取值0时,表示清除角标。取值大于99时,通知角标将显示99+。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | --------------------- | ---- | ------------------ | | badgeNumber | number | 是 | 角标个数。 | | callback | AsyncCallback\ | 是 | 设定角标回调函数。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600012 | No memory space. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let setBadgeNumberCallback = (err: BusinessError): void => { if (err) { console.error(`setBadgeNumber failed code is ${err.code}, message is ${err.message}`); } else { console.info("setBadgeNumber success"); } } let badgeNumber: number = 10; notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback); ``` ## notificationManager.getActiveNotificationCount getActiveNotificationCount(callback: AsyncCallback\): void 获取当前应用未删除的通知数。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---------------------- | | callback | AsyncCallback\ | 是 | 获取未删除通知数回调函数。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let getActiveNotificationCountCallback = (err: BusinessError, data: number): void => { if (err) { console.error(`getActiveNotificationCount failed, code is ${err.code}, message is ${err.message}`); } else { console.info(`getActiveNotificationCount success, data is ${JSON.stringify(data)}`); } } notificationManager.getActiveNotificationCount(getActiveNotificationCountCallback); ``` ## notificationManager.getActiveNotificationCount getActiveNotificationCount(): Promise\ 获取当前应用未删除的通知数。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | | ----------------- | ------------------------------------------- | | Promise\ | 以Promise形式返回获取当前应用未删除通知数。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.getActiveNotificationCount().then((data: number) => { console.info("getActiveNotificationCount success, data: " + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error(`getActiveNotificationCount fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.getActiveNotifications getActiveNotifications(callback: AsyncCallback\>): void 获取当前应用未删除的通知列表。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------------------------------------------ | ---- | ------------------------------ | | callback | AsyncCallback\> | 是 | 获取当前应用通知列表回调函数。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let getActiveNotificationsCallback = (err: BusinessError, data: Array): void => { if (err) { console.error(`getActiveNotifications failed, code is ${err.code}, message is ${err.message}`); } else { console.info("getActiveNotifications success" + JSON.stringify(data)); } } notificationManager.getActiveNotifications(getActiveNotificationsCallback); ``` ## notificationManager.getActiveNotifications getActiveNotifications(): Promise\\> 获取当前应用未删除的通知列表。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | --------------------------------------- | | Promise\\> | 以Promise形式返回获取当前应用通知列表。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.getActiveNotifications().then((data: Array) => { console.info("getActiveNotifications success, data: " + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error(`getActiveNotifications fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.cancelGroup cancelGroup(groupName: string, callback: AsyncCallback\): void 取消本应用指定组下的通知。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | --------- | --------------------- | ---- | ---------------------------- | | groupName | string | 是 | 通知组名称,此名称需要在发布通知时通过[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象指定。 | | callback | AsyncCallback\ | 是 | 取消本应用指定组下通知的回调函数。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let cancelGroupCallback = (err: BusinessError): void => { if (err) { console.error(`cancelGroup failed, code is ${err.code}, message is ${err.message}`); } else { console.info("cancelGroup success"); } } let groupName: string = "GroupName"; notificationManager.cancelGroup(groupName, cancelGroupCallback); ``` ## notificationManager.cancelGroup cancelGroup(groupName: string): Promise\ 取消本应用指定组下的通知。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | --------- | ------ | ---- | -------------- | | groupName | string | 是 | 通知组名称,此名称需要在发布通知时通过[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象指定。 | **返回值:** | 类型 | 说明 | |---------|-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let groupName: string = "GroupName"; notificationManager.cancelGroup(groupName).then(() => { console.info("cancelGroup success"); }).catch((err: BusinessError) => { console.error(`cancelGroup fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.isSupportTemplate isSupportTemplate(templateName: string, callback: AsyncCallback\): void 查询模板是否存在。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------------ | ------------------------ | ---- | -------------------------- | | templateName | string | 是 | 模板名称。当前仅支持'downloadTemplate'。 | | callback | AsyncCallback\ | 是 | 查询模板是否存在的回调函数(true:存在,false:不存在)。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let templateName: string = 'downloadTemplate'; let isSupportTemplateCallback = (err: BusinessError, data: boolean): void => { if (err) { console.error(`isSupportTemplate failed, code is ${err.code}, message is ${err.message}`); } else { console.info("isSupportTemplate success, data: " + JSON.stringify(data)); } } notificationManager.isSupportTemplate(templateName, isSupportTemplateCallback); ``` ## notificationManager.isSupportTemplate isSupportTemplate(templateName: string): Promise\ 查询模板是否存在。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------------ | ------ | ---- | -------- | | templateName | string | 是 | 模板名称。当前仅支持'downloadTemplate'。 | **返回值:** | 类型 | 说明 | | ------------------ | --------------- | | Promise\ | Promise方式返回模板是否存在的结果(true:存在,false:不存在)。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let templateName: string = 'downloadTemplate'; notificationManager.isSupportTemplate(templateName).then((data: boolean) => { console.info("isSupportTemplate success, data: " + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error(`isSupportTemplate fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.requestEnableNotification(deprecated) requestEnableNotification(callback: AsyncCallback\): void 应用请求通知使能。使用callback异步回调。 > **说明:** > > 从API version 12开始不再维护,建议使用有context入参的[requestEnableNotification](#notificationmanagerrequestenablenotification10)代替。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------ | ---- | -------------------------- | | callback | AsyncCallback\ | 是 | 应用请求通知使能的回调函数。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600004 | Notification disabled. | | 1600013 | A notification dialog box is already displayed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let requestEnableNotificationCallback = (err: BusinessError): void => { if (err) { console.error(`requestEnableNotification failed, code is ${err.code}, message is ${err.message}`); } else { console.info("requestEnableNotification success"); } }; notificationManager.requestEnableNotification(requestEnableNotificationCallback); ``` ## notificationManager.requestEnableNotification(deprecated) requestEnableNotification(): Promise\ 应用请求通知使能。使用Promise异步回调。 > **说明:** > > 从API version 12开始不再维护,建议使用有context入参的[requestEnableNotification](#notificationmanagerrequestenablenotification10-1)代替。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | |---------|-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600004 | Notification disabled. | | 1600013 | A notification dialog box is already displayed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.requestEnableNotification().then(() => { console.info("requestEnableNotification success"); }).catch((err: BusinessError) => { console.error(`requestEnableNotification fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.requestEnableNotification10+ requestEnableNotification(context: UIAbilityContext, callback: AsyncCallback\): void 应用请求通知使能模态弹窗。使用callback异步回调。 仅当应用界面加载完成后(即调用[loadContent](../apis-ability-kit/js-apis-app-ability-uiExtensionContentSession.md#uiextensioncontentsessionloadcontent)成功),方可使用该接口。 **模型约束**:此接口仅可在Stage模型下使用。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------ | ---- |--------------------| | context | [UIAbilityContext](../../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) | 是 | 通知弹窗绑定Ability的上下文。 | | callback | AsyncCallback\ | 是 | 应用请求通知使能的回调函数。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600004 | Notification disabled. | | 1600013 | A notification dialog box is already displayed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; class MyAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); let requestEnableNotificationCallback = (err: BusinessError): void => { if (err) { hilog.error(0x0000, 'testTag', `[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`); } else { hilog.info(0x0000, 'testTag', `[ANS] requestEnableNotification success`); } }; notificationManager.requestEnableNotification(this.context, requestEnableNotificationCallback); }); } } ``` ## notificationManager.requestEnableNotification10+ requestEnableNotification(context: UIAbilityContext): Promise\ 应用请求通知使能模态弹窗。使用Promise异步回调。 仅当应用界面加载完成后(即调用[loadContent](../apis-ability-kit/js-apis-app-ability-uiExtensionContentSession.md#uiextensioncontentsessionloadcontent)成功),方可使用该接口。 **模型约束**:此接口仅可在Stage模型下使用。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------ | ---- |--------------------| | context | [UIAbilityContext](../../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) | 是 | 通知弹窗绑定Ability的上下文。 | **返回值:** | 类型 | 说明 | |---------|-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600004 | Notification disabled. | | 1600013 | A notification dialog box is already displayed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; class MyAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); notificationManager.requestEnableNotification(this.context).then(() => { hilog.info(0x0000, 'testTag', `[ANS] requestEnableNotification success`); }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', `[ANS] requestEnableNotification failed, code is ${err.code}, message is ${err.message}`); }); }); } } ``` ## notificationManager.isDistributedEnabled isDistributedEnabled(callback: AsyncCallback\): void 查询设备是否支持分布式通知。使用callback异步回调。 **系统能力**:SystemCapability.Notification.Notification **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------ | ---- | -------------------------- | | callback | AsyncCallback\ | 是 | 设备是否支持分布式通知的回调函数(true:支持,false:不支持)。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600010 | Distributed operation failed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; let isDistributedEnabledCallback = (err: BusinessError, data: boolean): void => { if (err) { console.error(`isDistributedEnabled failed, code is ${err.code}, message is ${err.message}`); } else { console.info("isDistributedEnabled success " + JSON.stringify(data)); } }; notificationManager.isDistributedEnabled(isDistributedEnabledCallback); ``` ## notificationManager.isDistributedEnabled isDistributedEnabled(): Promise\ 查询设备是否支持分布式通知。使用Promise异步回调。 **系统能力**:SystemCapability.Notification.Notification **返回值:** | 类型 | 说明 | | ------------------ | --------------------------------------------- | | Promise\ | Promise方式返回设备是否支持分布式通知的结果(true:支持,false:不支持)。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | | 1600001 | Internal error. | | 1600002 | Marshalling or unmarshalling error. | | 1600003 | Failed to connect to the service. | | 1600010 | Distributed operation failed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; notificationManager.isDistributedEnabled().then((data: boolean) => { console.info("isDistributedEnabled success, data: " + JSON.stringify(data)); }).catch((err: BusinessError) => { console.error(`isDistributedEnabled fail: ${JSON.stringify(err)}`); }); ``` ## notificationManager.openNotificationSettings13+ openNotificationSettings(context: UIAbilityContext): Promise\ 拉起应用的通知设置界面,该页面以半模态形式呈现,可用于设置通知开关、通知提醒方式等。使用Promise异步回调。 **模型约束**:此接口仅可在Stage模型下使用。 **系统能力**:SystemCapability.Notification.NotificationSettings **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------------------------ | ---- |--------------------| | context | [UIAbilityContext](../../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) | 是 | 通知设置页面绑定Ability的上下文。 | **返回值:** | 类型 | 说明 | |---------|-----------| | Promise\ | 无返回结果的Promise对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 1600001 | Internal error. | | 1600003 | Failed to connect to the service. | | 1600018 | the notification settings window is already displayed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; import { UIAbility } from '@kit.AbilityKit'; import { window } from '@kit.ArkUI'; import { hilog } from '@kit.PerformanceAnalysisKit'; class MyAbility extends UIAbility { onWindowStageCreate(windowStage: window.WindowStage) { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/Index', (err, data) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); notificationManager.openNotificationSettings(this.context).then(() => { hilog.info(0x0000, 'testTag', `[ANS] openNotificationSettings success`); }).catch((err: BusinessError) => { hilog.error(0x0000, 'testTag', `[ANS] openNotificationSettings failed, code is ${err.code}, message is ${err.message}`); }); }); } } ``` ## ContentType 通知内容类型。 **原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 **系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification | 名称 | 值 | 说明 | | --------------------------------- | ----------- |------------------| | NOTIFICATION_CONTENT_BASIC_TEXT | 0 | 普通类型通知。 | | NOTIFICATION_CONTENT_LONG_TEXT | 1 | 长文本类型通知。 | | NOTIFICATION_CONTENT_PICTURE | 2 | 图片类型通知。 | | NOTIFICATION_CONTENT_CONVERSATION | 3 | 社交类型通知。预留能力,暂未支持。| | NOTIFICATION_CONTENT_MULTILINE | 4 | 多行文本类型通知。 | | NOTIFICATION_CONTENT_SYSTEM_LIVE_VIEW11+ | 5 | 实况窗类型通知。不支持三方应用直接创建该类型通知,可以由系统代理创建系统实况窗类型通知后,三方应用发布同ID的通知来更新指定内容。| | NOTIFICATION_CONTENT_LIVE_VIEW11+ | 6 | 普通实况窗类型通知。只支持系统应用。 | ## SlotLevel 通知级别。 **系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification | 名称 | 值 | 说明 | | --------------------------------- | ----------- | ------------------ | | LEVEL_NONE | 0 | 表示关闭通知功能。 | | LEVEL_MIN | 1 | 表示通知功能已启用,但状态栏中不显示通知图标,且没有横幅或提示音。 | | LEVEL_LOW | 2 | 表示通知功能已启用,且状态栏中显示通知图标,但没有横幅或提示音。 | | LEVEL_DEFAULT | 3 | 表示通知功能已启用,状态栏中显示通知图标,没有横幅但有提示音。 | | LEVEL_HIGH | 4 | 表示通知功能已启用,状态栏中显示通知图标,有横幅和提示音。 | ## SlotType 通知渠道类型。 **原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 **系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification | 名称 | 值 | 说明 | | -------------------- | -------- | ---------- | | UNKNOWN_TYPE | 0 | 未知类型。该类型对应[SlotLevel](#slotlevel)为LEVEL_MIN。 | | SOCIAL_COMMUNICATION | 1 | 社交通信。该类型对应[SlotLevel](#slotlevel)为LEVEL_HIGH。 | | SERVICE_INFORMATION | 2 | 服务提醒。该类型对应[SlotLevel](#slotlevel)为LEVEL_HIGH。| | CONTENT_INFORMATION | 3 | 内容资讯。该类型对应[SlotLevel](#slotlevel)为LEVEL_MIN。 | | LIVE_VIEW11+ | 4 | 实况窗。不支持三方应用直接创建该渠道类型通知,可以由系统代理创建后,三方应用发布同ID的通知来更新指定内容。该类型对应[SlotLevel](#slotlevel)为LEVEL_DEFAULT。 | | CUSTOMER_SERVICE11+ | 5 | 客服消息。该类型用于用户与商家之间的客服消息,需由用户主动发起。该类型对应[SlotLevel](#slotlevel)为LEVEL_DEFAULT。 | | OTHER_TYPES | 0xFFFF | 其他。该类型对应[SlotLevel](#slotlevel)为LEVEL_MIN。 |