本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明。
本模块提供通知管理的能力,应用可使用本模块完成通知的完整生命周期管理。其中涉及通知的发布、更新与取消,通知渠道的创建与查询、通知能力授权状态的查询与申请、应用角标的设置、通知中心存量通知的查询等操作。
API组合使用关系说明:
本模块的接口围绕通知的“授权→发布→取消→渠道管理”的完整流程展开,各接口间存在明确的组合使用关系:
授权查询与申请流程:发布通知前,先通过isNotificationEnabled查询通知能力的授权状态。如果通知能力未授权,通过requestEnableNotification引导用户开启通知权限。
通知发布与更新流程:通过publish发布通知,通知内容通过NotificationRequest指定。如果新发布通知与已有通知的ID和标签相同,将自动更新已有通知。如果新发布通知与已有通知的ID或标签不相同,将创建新的通知。
通知取消流程:通过cancel取消指定ID的通知,通过cancelAll取消本应用所有通知,通过cancelGroup取消指定分组下的通知。
通知渠道管理流程:通过addSlot创建通知渠道,通过getSlot/getSlots查询通知渠道配置,通过removeSlot/removeAllSlots删除通知渠道。建议在发布通知前先创建对应类型的通知渠道。除了可以使用addSlot创建通知渠道,还可以在发布通知的NotificationRequest中携带notificationSlotType字段,如果对应类型的渠道不存在,会自动创建。
角标管理流程:通过setBadgeNumber设置角标数字,或者通过publish接口发布通知时,在NotificationRequest的badgeNumber字段里携带需要增加的角标数量。
存量通知查询流程:通过getActiveNotificationCount获取通知中心本应用存量通知数量,通过getActiveNotifications获取通知中心本应用存量通知详情。
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
import { notificationManager } from '@kit.NotificationKit'; publish(request: NotificationRequest, callback: AsyncCallback<void>): void
发布通知。使用callback异步回调。
发布通知后,通知将以通知卡片的形式展示在设备的通知中心、状态栏等位置。如果新发布通知与已发布通知的ID和标签都相同,则新通知将取代原有通知,实现通知的更新效果。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| request | NotificationRequest | 是 | 设置发布通知的内容和相关配置信息。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当发布通知成功,err为undefined,否则为错误对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码、通知错误码和HTTP错误码。
| 错误码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. 适用版本:11+ |
| 1600009 | The notification sending frequency reaches the upper limit. |
| 1600012 | No memory space. |
| 1600014 | No permission. 适用版本:11+ |
| 1600015 | The current notification status does not support duplicate configurations. 适用版本:11+ |
| 1600016 | The notification version for this update is too low. 适用版本:11+ |
| 1600020 | The application is not allowed to send notifications due to permission settings. 适用版本:12+ |
| 1600029 | The system failed to find the ExtensionAbility instance for the custom Live View widget template. 适用版本:26.0.0+ |
| 2300007 | Network unreachable. 适用版本:11+ |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// publish回调
let publishCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in publishing notification.`);
}
}
// 通知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); publish(request: NotificationRequest): Promise<void>
发布通知。使用Promise异步回调。
发布通知后,通知将以通知卡片的形式展示在设备的通知中心、状态栏等位置。如果新发布通知与已发布通知的ID和标签都相同,则新通知将取代原有通知,实现通知的更新效果。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| request | NotificationRequest | 是 | 设置发布通知的内容和相关配置信息。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码、通知错误码和HTTP错误码。
| 错误码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. 适用版本:11+ |
| 1600009 | The notification sending frequency reaches the upper limit. |
| 1600012 | No memory space. |
| 1600014 | No permission. 适用版本:11+ |
| 1600015 | The current notification status does not support duplicate configurations. 适用版本:11+ |
| 1600016 | The notification version for this update is too low. 适用版本:11+ |
| 1600020 | The application is not allowed to send notifications due to permission settings. 适用版本:12+ |
| 1600029 | The system failed to find the ExtensionAbility instance for the custom Live View widget template. 适用版本:26.0.0+ |
| 2300007 | Network unreachable. 适用版本:11+ |
示例:
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(`Succeeded in publishing notification.`);
}).catch((err: BusinessError) => {
console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
}); cancel(id: number, label: string, callback: AsyncCallback<void>): void
根据通知ID和标签label取消已发布的通知。使用callback异步回调。
取消后,对应的通知将从通知中心、状态栏等位置移除,用户不再可见。适用于需要精确取消某一条带有特定标签的通知的场景。
与仅传入通知ID的notificationManager.cancel(id, callback)相比,此接口额外传入label参数,可精确取消同一ID,不同标签的通知。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | number | 是 | 通知ID,用于标识目标通知。该值由发布通知时NotificationRequest的id字段指定。 |
| label | string | 是 | 通知标签。该值由发布通知时NotificationRequest的label字段指定。 - 若标签为空,则取消与指定通知ID匹配,标签为空的已发布通知。 - 若标签不为空,则取消与指定通知ID和标签同时匹配的已发布通知。 |
| callback | AsyncCallback<void> | 是 | 回调函数。根据通知ID和标签取消已发布的通知成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// cancel回调
let cancelCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to cancel notification. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in canceling notification.`);
}
}
notificationManager.cancel(0, 'label', cancelCallback); cancel(id: number, label?: string): Promise<void>
根据通知ID和标签label取消已发布的通知。使用Promise异步回调。
取消后,对应的通知将从通知中心、状态栏等位置移除,用户不再可见。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | number | 是 | 通知ID,用于标识目标通知。该值由发布通知时NotificationRequest的id字段指定。 |
| label | string | 否 | 通知标签,默认为空。该值由发布通知时NotificationRequest的label字段指定。 - 若标签为空,则取消与指定通知ID匹配,标签为空的已发布通知。 - 若标签不为空,则取消与指定通知ID和标签同时匹配的已发布通知。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.cancel(0).then(() => {
console.info(`Succeeded in canceling notification.`);
}).catch((err: BusinessError) => {
console.error(`Failed to cancel notification. Code is ${err.code}, message is ${err.message}`);
}); cancel(id: number, callback: AsyncCallback<void>): void
根据指定的通知ID取消已发布的通知。使用callback异步回调。
取消后,对应的通知将从通知中心、状态栏等位置移除,用户不再可见。
与带label参数的notificationManager.cancel(id, label, callback)相比,此接口不传入label,将取消与指定ID匹配的通知。当发布通知,label不为空时,则需使用接口notificationManager.cancel(id, label, callback)取消通知。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | number | 是 | 通知ID,用于标识目标通知。该值由发布通知时NotificationRequest的id字段指定。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当取消已发布的通知成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// cancel回调
let cancelCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to cancel notification. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in canceling notification.`);
}
}
notificationManager.cancel(0, cancelCallback); cancelAll(callback: AsyncCallback<void>): void
取消当前应用所有已发布的通知。使用callback异步回调。
取消后,当前应用的所有通知将从通知中心、状态栏等位置移除,用户不再可见。适用于应用退出或用户手动清除全部通知的场景。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<void> | 是 | 回调函数。当取消当前应用所有已发布的通知成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// cancelAll回调
let cancelAllCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to cancel all notification. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in canceling all notification.`);
}
}
notificationManager.cancelAll(cancelAllCallback); cancelAll(): Promise<void>
取消当前应用所有已发布的通知。使用Promise异步回调。
取消后,当前应用的所有通知将从通知中心、状态栏等位置移除,用户不再可见。适用于应用退出或用户手动清除全部通知的场景。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.cancelAll().then(() => {
console.info(`Succeeded in canceling all notification.`);
}).catch((err: BusinessError) => {
console.error(`Failed to cancel all notification. Code is ${err.code}, message is ${err.message}`);
}); addSlot(type: SlotType, callback: AsyncCallback<void>): void
创建指定类型的通知渠道。使用callback异步回调。
通知渠道NotificationSlot定义了通知的提醒方式(如提示音、振动、横幅等)和级别。发布通知前,应用需先创建对应类型的通知渠道,或者发布通知时系统将自动创建对应类型的通知渠道。同一类型的通知渠道只能创建一个。
系统能力:SystemCapability.Notification.Notification
参数:
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// addSlot回调
let addSlotCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to add slot. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in adding slot.`);
}
}
notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION, addSlotCallback); addSlot(type: SlotType): Promise<void>
创建指定类型的通知渠道。使用Promise异步回调。
通知渠道NotificationSlot定义了通知的提醒方式(如提示音、振动、横幅等)和级别。发布通知前,应用需先创建对应类型的通知渠道,或者发布通知时系统将自动创建对应类型的通知渠道。同一类型的通知渠道只能创建一个。
系统能力:SystemCapability.Notification.Notification
参数:
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION).then(() => {
console.info(`Succeeded in adding slot.`);
}).catch((err: BusinessError) => {
console.error(`Failed to add slot. Code is ${err.code}, message is ${err.message}`);
}); getSlot(slotType: SlotType, callback: AsyncCallback<NotificationSlot>): void
获取指定类型的通知渠道。使用callback异步回调。
用于查询已创建的通知渠道的详细配置信息,包括提醒方式、级别、锁屏显示等设置。需先通过addSlot创建对应类型的通知渠道,否则获取结果为空。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| slotType | SlotType | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。 |
| callback | AsyncCallback<NotificationSlot> | 是 | 回调函数。当获取通知渠道成功,err为undefined,data为获取到的NotificationSlot,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// getSlot回调
let getSlotCallback = (err: BusinessError, data: notificationManager.NotificationSlot): void => {
if (err) {
console.error(`Failed to get slot. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in getting slot, data is ${JSON.stringify(data)}`);
}
}
let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
notificationManager.getSlot(slotType, getSlotCallback); getSlot(slotType: SlotType): Promise<NotificationSlot>
获取指定类型的通知渠道。使用Promise异步回调。
用于查询已创建的通知渠道的详细配置信息,包括提醒方式、级别、锁屏显示等设置。需先通过addSlot创建对应类型的通知渠道,否则获取结果为空。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| slotType | SlotType | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<NotificationSlot> | Promise对象,返回通知渠道对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
notificationManager.getSlot(slotType).then((data: notificationManager.NotificationSlot) => {
console.info(`Succeeded in getting slot, data is ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`Failed to get slot. Code is ${err.code}, message is ${err.message}`);
}); getSlots(callback: AsyncCallback<Array<NotificationSlot>>): void
获取当前应用的所有通知渠道。使用callback异步回调。
用于批量查询当前应用已创建的所有通知渠道的配置信息,包括各渠道的类型、提醒方式、级别等设置。适用于需要查看所有渠道配置的场景。需先通过addSlot创建对应类型的通知渠道,否则获取结果为空。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<Array<NotificationSlot>> | 是 | 回调函数。当获取通知渠道成功,err为undefined,data为获取到的NotificationSlot数组,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// getSlots回调
let getSlotsCallback = (err: BusinessError, data: Array<notificationManager.NotificationSlot>): void => {
if (err) {
console.error(`Failed to get slots. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in getting slots, data is ${JSON.stringify(data)}`);
}
}
notificationManager.getSlots(getSlotsCallback); getSlots(): Promise<Array<NotificationSlot>>
获取当前应用的所有通知渠道。使用Promise异步回调。
用于批量查询当前应用已创建的所有通知渠道的配置信息,包括各渠道的类型、提醒方式、级别等设置。适用于需要查看所有渠道配置的场景。需先通过addSlot创建对应类型的通知渠道,否则获取结果为空。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<Array<NotificationSlot>> | Promise对象,返回通知渠道对象数组。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.getSlots().then((data: Array<notificationManager.NotificationSlot>) => {
console.info(`Succeeded in getting slots, data is ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`Failed to get slots. Code is ${err.code}, message is ${err.message}`);
}); removeSlot(slotType: SlotType, callback: AsyncCallback<void>): void
删除当前应用指定类型的通知渠道。使用callback异步回调。
删除后,对应类型的通知渠道及其配置将被永久移除,后续发布该类型通知时系统将自动创建默认渠道。已通过该渠道发布的通知不受影响,仍可在通知中心查看。适用于需要重新配置渠道时先删除再创建的场景。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| slotType | SlotType | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。需传入已创建的渠道类型,否则删除操作无效。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当删除指定类型的通知渠道成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
// removeSlot回调
let removeSlotCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to remove slot. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in removing slot.`);
}
}
let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
notificationManager.removeSlot(slotType, removeSlotCallback); removeSlot(slotType: SlotType): Promise<void>
删除当前应用指定类型的通知渠道。使用Promise异步回调。
删除后,对应类型的通知渠道及其配置将被永久移除,后续发布该类型通知时系统将自动创建默认渠道。已通过该渠道发布的通知不受影响,仍可在通知中心查看。适用于需要重新配置渠道时先删除再创建的场景。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| slotType | SlotType | 是 | 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。需传入已创建的渠道类型,否则删除操作无效。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
notificationManager.removeSlot(slotType).then(() => {
console.info(`Succeeded in removing slot.`);
}).catch((err: BusinessError) => {
console.error(`Failed to remove slot. Code is ${err.code}, message is ${err.message}`);
}); removeAllSlots(callback: AsyncCallback<void>): void
删除当前应用所有通知渠道。使用callback异步回调。
删除后,当前应用的所有通知渠道及其配置将被永久移除,后续发布通知时系统将自动创建对应类型的渠道。已通过这些渠道发布的通知不受影响,仍可在通知中心查看。适用于需要一次性清除所有渠道配置的场景。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<void> | 是 | 回调函数。当删除当前应用所有通知渠道成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let removeAllSlotsCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to remove all slots. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in removing all slots.`);
}
}
notificationManager.removeAllSlots(removeAllSlotsCallback); removeAllSlots(): Promise<void>
删除当前应用所有通知渠道。使用Promise异步回调。
删除后,当前应用的所有通知渠道及其配置将被永久移除,后续发布通知时系统将自动创建对应类型的渠道。已通过这些渠道发布的通知不受影响,仍可在通知中心查看。适用于需要一次性清除所有渠道配置的场景。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.removeAllSlots().then(() => {
console.info(`Succeeded in removing all slots.`);
}).catch((err: BusinessError) => {
console.error(`Failed to remove all slots. Code is ${err.code}, message is ${err.message}`);
}); isNotificationEnabled(callback: AsyncCallback<boolean>): void
查询当前应用通知授权状态。使用callback异步回调。
用于在发布通知前检查当前应用是否被允许发送通知,避免在通知授权关闭时发布导致失败。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<boolean> | 是 | 回调函数。返回true,表示允许发布通知;返回false,表示禁止发布通知;调用失败返回错误对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码、通知错误码和包管理子系统通用错误码。
| 错误码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. |
示例:
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); isNotificationEnabled(): Promise<boolean>
查询当前应用通知授权状态。使用Promise异步回调。
用于在发布通知前检查当前应用是否被允许发送通知,避免在通知授权关闭时发布导致失败。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<boolean> | Promise对象。返回true,表示允许发布通知;返回false,表示禁止发布通知。 |
错误码:
以下错误码的详细介绍请参见通知错误码、包管理子系统通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.isNotificationEnabled().then((data: boolean) => {
console.info(`isNotificationEnabled success, data: ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`isNotificationEnabled failed, code is ${err.code}, message is ${err.message}`);
}); isNotificationEnabledSync(): boolean
同步查询当前应用通知授权状态。
用于在发布通知前快速检查当前应用是否被允许发送通知。此接口为同步接口,调用后立即返回结果,适用于需要在同步代码流程中获取通知授权状态的场景。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 返回查询通知授权状态的结果。返回true,表示允许发布通知;返回false,表示禁止发布通知。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
let enabled: boolean = notificationManager.isNotificationEnabledSync();
console.info(`isNotificationEnabledSync success, data is : ${JSON.stringify(enabled)}`); setBadgeNumber(badgeNumber: number): Promise<void>
设定角标个数,在应用的桌面图标上呈现。使用Promise异步回调。
角标是应用桌面图标右上角显示的数字标识,用于提示用户有未处理的通知数量。设定后,桌面图标将显示对应角标数字。适用于需要在桌面图标上提示用户待处理消息数量的场景,如未读消息数、待办事项数等。
系统能力:SystemCapability.Notification.Notification
设备行为差异:该接口在Wearable中返回801错误码,在其他设备类型中可正常调用。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| badgeNumber | number | 是 | 角标个数。当角标设定个数取值小于或等于0时,清除角标。取值大于99时,通知角标将显示99+。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
| 801 | Capability not supported. 适用版本:18+ |
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
| 1600012 | No memory space. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let badgeNumber: number = 10;
notificationManager.setBadgeNumber(badgeNumber).then(() => {
console.info(`Succeeded in setting badge number.`);
}).catch((err: BusinessError) => {
console.error(`Failed to set badge number. Code is ${err.code}, message is ${err.message}`);
}); setBadgeNumber(badgeNumber: number, callback: AsyncCallback<void>): void
设定角标个数,在应用的桌面图标上呈现。使用callback异步回调。
角标是应用桌面图标右上角显示的数字标识,用于提示用户有未处理的通知数量。设定后,桌面图标将显示对应角标数字。适用于需要在桌面图标上提示用户待处理消息数量的场景,如未读消息数、待办事项数等。
系统能力:SystemCapability.Notification.Notification
设备行为差异:该接口在Wearable中返回801错误码,在其他设备类型中可正常调用。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| badgeNumber | number | 是 | 角标个数。当角标设定个数取值小于或等于0时,清除角标。取值大于99时,通知角标将显示99+。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当设定角标个数成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
| 801 | Capability not supported. 适用版本:18+ |
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
| 1600012 | No memory space. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let setBadgeNumberCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to set badge number. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in setting badge number.`);
}
}
let badgeNumber: number = 10;
notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback); getBadgeNumber(): Promise<number>
获取当前应用角标数量。使用Promise异步回调。
用于查询当前应用桌面图标上显示的角标数字。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<number> | Promise对象,返回当前应用角标数量。(查询的角标数量与当前应用通知开关,桌面角标开关是否开启无关) |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.getBadgeNumber().then((badgeNumber: number) => {
console.info(`Succeeded in getting badge number, badgeNumber is ${JSON.stringify(badgeNumber)}`);
}).catch((err: BusinessError) => {
console.error(`Failed to get badge number. Code is ${err.code}, message is ${err.message}`);
}); getActiveNotificationCount(callback: AsyncCallback<number>): void
获取当前应用的通知数量。使用callback异步回调。
用于查询当前应用在通知中心中已发布的存量通知数量。适用于需要展示未读通知数量提示的场景。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<number> | 是 | 回调函数。当获取当前应用未删除的通知数成功,err为undefined,data为当前应用未删除的通知数,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let getActiveNotificationCountCallback = (err: BusinessError, data: number): void => {
if (err) {
console.error(`Failed to get active notification count. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in getting active notification count, data is ${JSON.stringify(data)}`);
}
}
notificationManager.getActiveNotificationCount(getActiveNotificationCountCallback); getActiveNotificationCount(): Promise<number>
获取当前应用的通知数量。使用Promise异步回调。
用于查询当前应用在通知中心中已发布的存量通知数量。适用于需要展示未读通知数量提示的场景。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<number> | Promise对象,返回当前应用在通知中心的存量通知数量。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.getActiveNotificationCount().then((data: number) => {
console.info(`Succeeded in getting active notification count, data is ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`Failed to get active notification count. Code is ${err.code}, message is ${err.message}`);
}); getActiveNotifications(callback: AsyncCallback<Array<NotificationRequest>>): void
获取当前应用未删除的通知列表。使用callback异步回调。
用于查询当前应用在通知中心中所有存量通知的详细信息列表,包括每条通知的ID、标签、内容、创建时间等。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<Array<NotificationRequest>> | 是 | 回调函数。当获取未删除的通知列表成功,err为undefined,data为获取到的通知列表,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let getActiveNotificationsCallback = (err: BusinessError, data: Array<notificationManager.NotificationRequest>): void => {
if (err) {
console.error(`Failed to get active notifications. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in getting active notifications, data is ${JSON.stringify(data)}`);
}
}
notificationManager.getActiveNotifications(getActiveNotificationsCallback); getActiveNotifications(): Promise<Array<NotificationRequest>>
获取当前应用未删除的通知列表。使用Promise异步回调。
用于查询当前应用在通知中心中所有存量通知的详细信息列表,包括每条通知的ID、标签、内容、创建时间等。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<Array<NotificationRequest>> | Promise对象,返回当前应用未删除的通知列表,包含每条通知的详细信息。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.getActiveNotifications().then((data: Array<notificationManager.NotificationRequest>) => {
console.info(`Succeeded in getting active notifications, data is ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`Failed to get active notifications. Code is ${err.code}, message is ${err.message}`);
}); getNotificationParameters(id: number, label?: string): Promise<NotificationParameters>
获取通知NotificationRequest中wantAgent字段的部分信息。使用Promise异步回调。
模型约束:此接口仅可在Stage模型下使用。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | number | 是 | 通知ID,用于标识目标通知。该值由发布通知时NotificationRequest的id字段指定。 |
| label | string | 否 | 通知标签,默认为空。该值由发布通知时NotificationRequest的label字段指定。 - 若标签为空,则获取与指定通知ID匹配,标签为空的已发布通知的部分信息。 - 若标签不为空,则获取与指定通知ID和标签同时匹配的已发布通知的部分信息。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<NotificationParameters> | Promise对象,返回wantAgent的部分信息。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
| 1600007 | The notification does not exist. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let id: number = 0;
let label: string = '';
notificationManager.getNotificationParameters(id, label).then((data: notificationManager.NotificationParameters) => {
console.info(`Succeeded in getting notification parameters, data is ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`Failed to get notification parameters. Code is ${err.code}, message is ${err.message}`);
}); cancelGroup(groupName: string, callback: AsyncCallback<void>): void
取消当前应用指定组下的通知。使用callback异步回调。
通知组groupName是在发布通知时通过NotificationRequest的groupName字段指定的分组标识。取消后,该组下所有通知将从通知中心移除。适用于需要按业务分组批量取消通知的场景。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| groupName | string | 是 | 通知组名称,此名称需要在发布通知时通过NotificationRequest对象指定。 大小不超过202字节,超出部分会被截断。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当取消当前应用指定组下的通知成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let cancelGroupCallback = (err: BusinessError): void => {
if (err) {
console.error(`Failed to cancel group. Code is ${err.code}, message is ${err.message}`);
} else {
console.info(`Succeeded in canceling group.`);
}
}
let groupName: string = 'GroupName';
notificationManager.cancelGroup(groupName, cancelGroupCallback); cancelGroup(groupName: string): Promise<void>
取消当前应用指定组下的通知。使用Promise异步回调。
通知组groupName是在发布通知时通过NotificationRequest的groupName字段指定的分组标识。取消后,该组下所有通知将从通知中心移除。适用于需要按业务分组批量取消通知的场景。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| groupName | string | 是 | 通知组名称,此名称需要在发布通知时通过NotificationRequest对象指定。 大小不超过202字节,超出部分会被截断。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码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. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
let groupName: string = 'GroupName';
notificationManager.cancelGroup(groupName).then(() => {
console.info(`Succeeded in canceling group.`);
}).catch((err: BusinessError) => {
console.error(`Failed to cancel group. Code is ${err.code}, message is ${err.message}`);
}); isSupportTemplate(templateName: string, callback: AsyncCallback<boolean>): void
在使用NotificationTemplate发布通知前,可以通过该接口查询是否支持对应的通知模板。使用callback异步回调。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| templateName | string | 是 | 模板名称。当前仅支持'downloadTemplate'。 |
| callback | AsyncCallback<boolean> | 是 | 回调函数。返回true表示支持该模板;返回false表示不支持该模板;调用失败返回错误对象。 |
错误码:
| 错误码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. |
示例:
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); isSupportTemplate(templateName: string): Promise<boolean>
在使用NotificationTemplate发布通知前,可以通过该接口查询是否支持对应的通知模板。使用Promise异步回调。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| templateName | string | 是 | 模板名称。当前仅支持'downloadTemplate'。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<boolean> | Promise对象。返回true表示支持该模板;返回false表示不支持该模板。 |
错误码:
| 错误码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. |
示例:
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 failed, code is ${err.code}, message is ${err.message}`);
}); requestEnableNotification(context: UIAbilityContext, callback: AsyncCallback<void>): void
应用需要获取用户授权才能发送通知。在通知发布前调用该接口,可以拉起通知授权弹窗,让用户选择是否允许发送通知。使用callback异步回调。
模型约束:此接口仅可在Stage模型下使用。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | UIAbilityContext | 是 | 通知弹窗绑定Ability的上下文。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当应用通过弹窗获取用户授权成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. 适用版本:11+ |
| 1600013 | A notification dialog box is already displayed. 适用版本:11+ |
示例:
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: ${JSON.stringify(err) ?? ''}`);
return;
}
hilog.info(0x0000, 'testTag', `Succeeded in loading the content. Data: ${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);
});
}
} requestEnableNotification(context: UIAbilityContext): Promise<void>
应用需要获取用户授权才能发送通知。在通知发布前调用该接口,可以拉起通知授权弹窗,让用户选择是否允许发送通知。使用Promise异步回调。
模型约束:此接口仅可在Stage模型下使用。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | UIAbilityContext | 是 | 通知弹窗绑定的Ability上下文。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码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. 适用版本:11+ |
| 1600013 | A notification dialog box is already displayed. 适用版本:11+ |
示例:
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: ${JSON.stringify(err) ?? ''}`);
return;
}
hilog.info(0x0000, 'testTag', `Succeeded in loading the content. Data: ${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}`);
});
});
}
} requestEnableNotification(callback: AsyncCallback<void>): void
当前应用请求通知授权。使用callback异步回调。
从API version 9开始支持,从API version 12开始废弃,建议使用有context入参的requestEnableNotification替代。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<void> | 是 | 回调函数。当应用请求通知授权成功,err为undefined,否则为错误对象。 |
错误码:
| 错误码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. 适用版本:11+ |
| 1600013 | A notification dialog box is already displayed. 适用版本:11+ |
示例:
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); requestEnableNotification(): Promise<void>
当前应用请求通知授权。使用Promise异步回调。
从API version 9开始支持,从API version 12开始废弃,建议使用有context入参的requestEnableNotification替代。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
| 1600004 | Notification disabled. 适用版本:11+ |
| 1600013 | A notification dialog box is already displayed. 适用版本:11+ |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.requestEnableNotification().then(() => {
console.info(`requestEnableNotification success`);
}).catch((err: BusinessError) => {
console.error(`requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
}); isDistributedEnabled(callback: AsyncCallback<boolean>): void
查询设备是否支持跨设备协同通知。使用callback异步回调。
从API version 9开始支持,从API version 26.0.0开始废弃。
系统能力:SystemCapability.Notification.Notification
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | AsyncCallback<boolean> | 是 | 回调函数。返回true表示支持跨设备协同通知;返回false表示不支持跨设备协同通知;调用失败返回错误对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. |
| 801 | Capability not supported. 适用版本:26.0.0+ |
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
| 1600010 | Distributed operation failed. |
示例:
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); isDistributedEnabled(): Promise<boolean>
查询设备是否支持跨设备协同通知。使用Promise异步回调。
从API version 9开始支持,从API version 26.0.0开始废弃。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<boolean> | Promise对象。返回true表示支持跨设备协同通知;返回false表示不支持跨设备协同通知。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 801 | Capability not supported. 适用版本:26.0.0+ |
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
| 1600010 | Distributed operation failed. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.isDistributedEnabled().then((data: boolean) => {
console.info(`isDistributedEnabled success, data: ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`isDistributedEnabled failed, code is ${err.code}, message is ${err.message}`);
}); openNotificationSettings(context: UIAbilityContext): Promise<void>
拉起应用的通知设置界面,该页面以半模态形式呈现,可用于设置通知开关、通知提醒方式等。使用Promise异步回调。
适用于用户需要手动修改通知设置的场景,如用户拒绝授权后二次申请,或需要修改通知提醒方式(振动、铃声等)。当requestEnableNotification弹窗被用户拒绝后,开发者可调用此接口引导用户前往通知设置页面手动开启。
模型约束:此接口仅可在Stage模型下使用。
系统能力:SystemCapability.Notification.NotificationSettings
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | UIAbilityContext | 是 | 通知设置页面绑定Ability的上下文。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 801 | Capability not supported. 适用版本:18+ |
| 1600001 | Internal error. |
| 1600003 | Failed to connect to the service. |
| 1600018 | The notification settings window is already displayed. |
示例:
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: ${JSON.stringify(err) ?? ''}`);
return;
}
hilog.info(0x0000, 'testTag', `Succeeded in loading the content. Data: ${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}`);
});
});
}
} openNotificationSettingsWithResult(context: UIAbilityContext): Promise<NotificationSetting>
拉起应用的通知设置界面,该页面以半模态形式呈现,可用于设置通知开关、通知提醒方式等。使用Promise异步回调,当半模态窗口关闭时返回用户设置的状态。
与openNotificationSettings相比,此接口在半模态窗口关闭时返回NotificationSetting对象,开发者可根据返回结果判断用户是否开启了通知权限,从而决定后续逻辑。
起始版本:26.0.0
模型约束:此接口仅可在Stage模型下使用。
系统能力:SystemCapability.Notification.NotificationSettings
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | UIAbilityContext | 是 | 通知设置页面绑定Ability的上下文。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<NotificationSetting> | Promise对象,返回此应用的通知设置。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 801 | Capability not supported. |
| 1600001 | Internal error. |
| 1600003 | Failed to connect to the service. |
| 1600018 | The notification settings window is already displayed. |
示例:
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: ${JSON.stringify(err) ?? ''}`);
return;
}
hilog.info(0x0000, 'testTag', `Succeeded in loading the content. Data: ${JSON.stringify(data) ?? ''}`);
notificationManager.openNotificationSettingsWithResult(this.context).then((data) => {
hilog.info(0x0000, 'testTag', `[ANS] openNotificationSettingsWithResult success, data: ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', `[ANS] openNotificationSettingsWithResult failed, code is ${err.code}, message is ${err.message}`);
});
});
}
} getNotificationSetting(): Promise<NotificationSetting>
获取应用的通知设置,包括锁屏通知、横幅通知、桌面角标、振动、铃声等开关状态。使用Promise异步回调。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<NotificationSetting> | Promise对象,返回此应用的通知设置。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.getNotificationSetting().then((data: notificationManager.NotificationSetting) => {
console.info(`getNotificationSetting success, data: ${JSON.stringify(data)}`);
}).catch((err: BusinessError) => {
console.error(`getNotificationSetting failed, code is ${err.code}, message is ${err.message}`);
}); isGeofenceEnabled(): Promise<boolean>
检查地理围栏功能是否已启用。使用Promise异步回调。
系统能力:SystemCapability.Notification.Notification
返回值:
| 类型 | 说明 |
|---|---|
| Promise<boolean> | Promise对象,返回地理围栏开关状态的Promise对象。返回true表示地理围栏功能已启用,返回false表示地理围栏功能未启用。 |
错误码:
以下错误码的详细介绍请参见通知错误码。
| 错误码ID | 错误信息 |
|---|---|
| 1600001 | Internal error. |
| 1600002 | Marshalling or unmarshalling error. |
| 1600003 | Failed to connect to the service. |
| 1600012 | No memory space. |
示例:
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
notificationManager.isGeofenceEnabled().then((data: boolean) => {
hilog.info(0x0000, 'testTag', '%{public}s', `isGeofenceEnabled success, enabled: ${JSON.stringify(data)}.`);
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'testTag', '%{public}s',`isGeofenceEnabled failed, code is ${err.code}, message is ${err.message}`);
}); 元服务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 | 普通实况窗类型通知。仅系统应用可用。 |
通知级别。
用于定义NotificationSlot的通知提醒行为级别,影响通知在状态栏的显示方式,是否展示横幅和提示音等。
系统能力:SystemCapability.Notification.Notification
| 名称 | 值 | 说明 |
|---|---|---|
| LEVEL_NONE | 0 | 表示关闭通知功能。 |
| LEVEL_MIN | 1 | 表示通知功能已启用,状态栏中不显示通知图标,无横幅,无提示音。 |
| LEVEL_LOW | 2 | 表示通知功能已启用,状态栏中显示通知图标,无横幅,无提示音。 |
| LEVEL_DEFAULT | 3 | 表示通知功能已启用,状态栏中显示通知图标,无横幅,有提示音。 |
| LEVEL_HIGH | 4 | 表示通知功能已启用,状态栏中显示通知图标,有横幅,有提示音。 |
通知渠道类型。
不同类型对应不同的SlotLevel,决定通知的提醒行为。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Notification.Notification
| 名称 | 值 | 说明 |
|---|---|---|
| UNKNOWN_TYPE | 0 | 未知类型。该类型对应SlotLevel为LEVEL_MIN。 |
| SOCIAL_COMMUNICATION | 1 | 社交通讯。该类型对应SlotLevel为LEVEL_HIGH。 |
| SERVICE_INFORMATION | 2 | 服务提醒。该类型对应SlotLevel为LEVEL_HIGH。 |
| CONTENT_INFORMATION | 3 | 内容资讯。该类型对应SlotLevel为LEVEL_MIN。 |
| LIVE_VIEW11+ | 4 | 实况窗。不支持三方应用直接创建该渠道类型通知,可以由系统代理创建后,三方应用发布同ID的通知来更新指定内容,更新时默认无提示音。该类型对应SlotLevel为LEVEL_DEFAULT。 |
| CUSTOMER_SERVICE11+ | 5 | 客服消息。该类型用于用户与商家之间的客服消息,需由用户主动发起。该类型对应SlotLevel为LEVEL_DEFAULT。 |
| OTHER_TYPES | 0xFFFF | 其他。该类型对应SlotLevel为LEVEL_MIN。 |
通知提醒方式开关的设置状态。
系统能力:SystemCapability.Notification.Notification
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| vibrationEnabled | boolean | 否 | 否 | 表示是否开启振动。 - true:开启。 - false:关闭。 |
| soundEnabled | boolean | 否 | 否 | 表示是否开启铃声。 - true:开启。 - false:关闭。 |
| lockScreenEnabled | boolean | 否 | 是 | 表示是否开启锁屏通知。 模型约束: 此接口仅可在Stage模型下使用。 起始版本:26.0.0 - true:开启。 - false:关闭。 |
| bannerEnabled | boolean | 否 | 是 | 表示是否开启横幅通知。 模型约束: 此接口仅可在Stage模型下使用。 起始版本:26.0.0 - true:开启。 - false:关闭。 |
| badgeNumberEnabled | boolean | 否 | 是 | 表示是否开启通知角标数字展示。 模型约束: 此接口仅可在Stage模型下使用。 起始版本:26.0.0 - true:开启。 - false:关闭。 |
| notificationEnabled | boolean | 否 | 是 | 表示应用通知授权状态。 模型约束: 此接口仅可在Stage模型下使用。 起始版本:26.0.0 - true:开启。 - false:关闭。 |
描述通知的优先级类型。
系统能力:SystemCapability.Notification.Notification
| 名称 | 值 | 说明 |
|---|---|---|
| OTHER | 'OTHER' | 表示通知优先级类型为默认。 |
| PRIMARY_CONTACT | 'PRIMARY_CONTACT' | 表示通知优先级类型为重要联系人。 |
| AT_ME | 'AT_ME' | 表示通知优先级类型为@我。 |
| URGENT_MESSAGE | 'URGENT_MESSAGE' | 表示通知优先级类型为加急消息。 |
| SCHEDULE_REMINDER | 'SCHEDULE_REMINDER' | 表示通知优先级类型为日程待办。 |
type BundleOption = _BundleOption
指定应用的包信息。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _BundleOption | 指定应用的包信息。 |
type NotificationActionButton = _NotificationActionButton
通知中显示的操作按钮。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationActionButton | 通知中显示的操作按钮。 |
type NotificationBasicContent = _NotificationBasicContent
普通文本通知。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationBasicContent | 描述普通文本通知。 |
type NotificationContent = _NotificationContent
通知内容。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationContent | 描述通知内容。 |
type NotificationLongTextContent = _NotificationLongTextContent
长文本通知。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationLongTextContent | 描述长文本通知。 |
type NotificationMultiLineContent = _NotificationMultiLineContent
多行文本通知。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationMultiLineContent | 描述多行文本通知。 |
type NotificationPictureContent = _NotificationPictureContent
附有图片的通知。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationPictureContent | 附有图片的通知。 |
type NotificationSystemLiveViewContent = _NotificationSystemLiveViewContent
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationSystemLiveViewContent | 系统实况窗通知内容。 |
type NotificationRequest = _NotificationRequest
通知请求。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationRequest | 通知请求。 |
type NotificationParameters = _NotificationParameters
描述通知请求中wantAgent的部分信息。
模型约束:此接口仅可在Stage模型下使用。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationParameters | 描述通知请求中wantAgent的部分信息。 |
type DistributedOptions = _DistributedOptions
分布式选项。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _DistributedOptions | 分布式选项。 |
type NotificationSlot = _NotificationSlot
通知渠道。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationSlot | 通知渠道。 |
type NotificationTemplate = _NotificationTemplate
通知模板。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationTemplate | 通知模板。 |
type NotificationUserInput = _NotificationUserInput
保存用户输入的通知消息。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationUserInput | 保存用户输入的通知消息。 |
type NotificationCapsule = _NotificationCapsule
通知胶囊。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationCapsule | 通知胶囊。 |
type NotificationButton = _NotificationButton
通知按钮。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationButton | 通知按钮。 |
type NotificationTime = _NotificationTime
通知计时信息。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationTime | 描述通知计时信息。 |
type NotificationProgress = _NotificationProgress
通知进度。
系统能力:SystemCapability.Notification.Notification
| 类型 | 说明 |
|---|---|
| _NotificationProgress | 描述通知进度。 |