HarmonyOS实战开发:@ohos.notificationManager (NotificationManager模块)

本模块提供通知管理的能力,包括发布、取消发布通知,创建、获取、移除通知通道,获取通知的使能状态、角标使能状态,获取通知的相关信息等。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import notificationManager from '@ohos.notificationManager';

notificationManager.publish

publish(request: NotificationRequest, callback: AsyncCallback<void>): void

发布通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
requestNotificationRequest用于设置要发布通知的内容和相关配置信息。
callbackAsyncCallback<void>发布通知的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600004Notification is not enabled.
1600005Notification slot is not enabled.
1600009Over max number notifications per second.
1600012No memory space.

示例:

import Base from '@ohos.base';

//publish回调
let publishCallback = (err: Base.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: {
        contentType: 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<void>

发布通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
requestNotificationRequest用于设置要发布通知的内容和相关配置信息。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600004Notification is not enabled.
1600005Notification slot is not enabled.
1600009Over max number notifications per second.
1600012No memory space.

示例:

import Base from '@ohos.base';

// 通知Request对象
let notificationRequest: notificationManager.NotificationRequest = {
    id: 1,
    content: {
        contentType: 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: Base.BusinessError) => {
    console.error(`publish fail: ${JSON.stringify(err)}`);
});

notificationManager.publish

publish(request: NotificationRequest, userId: number, callback: AsyncCallback<void>): void

发布通知给指定的用户(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
requestNotificationRequest用于设置要发布通知的内容和相关配置信息。
userIdnumber用户ID。
callbackAsyncCallback<void>被指定的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600004Notification is not enabled.
1600005Notification slot is not enabled.
1600008The user is not exist.
1600009Over max number notifications per second.
1600012No memory space.

示例:

import Base from '@ohos.base';

// publish回调
let publishCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("publish success");
    }
}
// 用户ID
let userId: number = 1;
// 通知Request对象
let notificationRequest: notificationManager.NotificationRequest = {
    id: 1,
    content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};
notificationManager.publish(notificationRequest, userId, publishCallback);

notificationManager.publish

publish(request: NotificationRequest, userId: number): Promise<void>

发布通知给指定的用户(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
requestNotificationRequest用于设置要发布通知的内容和相关配置信息。
userIdnumber用户ID。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600004Notification is not enabled.
1600005Notification slot is not enabled.
1600008The user is not exist.
1600009Over max number notifications per second.
1600012No memory space.

示例:

import Base from '@ohos.base';

let notificationRequest: notificationManager.NotificationRequest = {
    id: 1,
    content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};
let userId: number = 1;
notificationManager.publish(notificationRequest, userId).then(() => {
    console.info("publish success");
}).catch((err: Base.BusinessError) => {
    console.error(`publish fail: ${JSON.stringify(err)}`);
});

notificationManager.cancel

cancel(id: number, label: string, callback: AsyncCallback<void>): void

通过通知ID和通知标签取消已发布的通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
idnumber通知ID。
labelstring通知标签。
callbackAsyncCallback<void>表示被指定通知的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.

示例:

import Base from '@ohos.base';

// cancel回调
let cancelCallback = (err: Base.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<void>

通过通知ID和通知标签取消已发布的通知,若label为空表示取消与指定通知ID相匹配的已发布通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
idnumber通知ID。
labelstring通知标签,默认为空。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.

示例:

import Base from '@ohos.base';

notificationManager.cancel(0).then(() => {
    console.info("cancel success");
}).catch((err: Base.BusinessError) => {
    console.error(`cancel fail: ${JSON.stringify(err)}`);
});

notificationManager.cancel

cancel(id: number, callback: AsyncCallback<void>): void

取消与指定通知ID相匹配的已发布通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
idnumber通知ID。
callbackAsyncCallback<void>表示被指定通知的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.

示例:

import Base from '@ohos.base';

// cancel回调
let cancelCallback = (err: Base.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>): void

取消当前应用所有已发布的通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

参数:

参数名类型必填说明
callbackAsyncCallback<void>表示被指定通知的回调方法。

示例:

import Base from '@ohos.base';

// cancel回调
let cancelAllCallback = (err: Base.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<void>

取消当前应用所有已发布的通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.cancelAll().then(() => {
    console.info("cancelAll success");
}).catch((err: Base.BusinessError) => {
    console.error(`cancelAll fail: ${JSON.stringify(err)}`);
});

notificationManager.addSlot

addSlot(slot: NotificationSlot, callback: AsyncCallback<void>): void

创建通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
slotNotificationSlot要创建的通知通道对象。
callbackAsyncCallback<void>表示被指定通道的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

// addslot回调
let addSlotCallBack = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`addSlot failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("addSlot success");
    }
}
// 通知slot对象
let notificationSlot: notificationManager.NotificationSlot = {
    type: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
notificationManager.addSlot(notificationSlot, addSlotCallBack);

notificationManager.addSlot

addSlot(slot: NotificationSlot): Promise<void>

创建通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
slotNotificationSlot要创建的通知通道对象。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

// 通知slot对象
let notificationSlot: notificationManager.NotificationSlot = {
    type: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
notificationManager.addSlot(notificationSlot).then(() => {
    console.info("addSlot success");
}).catch((err: Base.BusinessError) => {
    console.error(`addSlot fail: ${JSON.stringify(err)}`);
});

notificationManager.addSlot

addSlot(type: SlotType, callback: AsyncCallback<void>): void

创建指定类型的通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
typeSlotType要创建的通知通道的类型。
callbackAsyncCallback<void>表示被指定通道的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

// addslot回调
let addSlotCallBack = (err: Base.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<void>

创建指定类型的通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
typeSlotType要创建的通知通道的类型。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

notificationManager.addSlot(notificationManager.SlotType.SOCIAL_COMMUNICATION).then(() => {
    console.info("addSlot success");
}).catch((err: Base.BusinessError) => {
    console.error(`addSlot fail: ${JSON.stringify(err)}`);
});

notificationManager.addSlots

addSlots(slots: Array<NotificationSlot>, callback: AsyncCallback<void>): void

创建多个通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
slotsArray<NotificationSlot>要创建的通知通道对象数组。
callbackAsyncCallback<void>表示被指定通道的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

// addSlots回调
let addSlotsCallBack = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`addSlots failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("addSlots success");
    }
}
// 通知slot对象
let notificationSlot: notificationManager.NotificationSlot = {
    type: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
// 通知slot array 对象
let notificationSlotArray: notificationManager.NotificationSlot[] = new Array();
notificationSlotArray[0] = notificationSlot;
notificationManager.addSlots(notificationSlotArray, addSlotsCallBack);

notificationManager.addSlots

addSlots(slots: Array<NotificationSlot>): Promise<void>

创建多个通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
slotsArray<NotificationSlot>要创建的通知通道对象数组。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

// 通知slot对象
let notificationSlot: notificationManager.NotificationSlot = {
    type: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
// 通知slot array 对象
let notificationSlotArray: notificationManager.NotificationSlot[] = new Array();
notificationSlotArray[0] = notificationSlot;
notificationManager.addSlots(notificationSlotArray).then(() => {
    console.info("addSlots success");
}).catch((err: Base.BusinessError) => {
    console.error(`addSlot fail: ${JSON.stringify(err)}`);
});

notificationManager.getSlot

getSlot(slotType: SlotType, callback: AsyncCallback<NotificationSlot>): void

获取一个指定类型的通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
slotTypeSlotType通知渠道类型,目前分为社交通信、服务提醒、内容咨询和其他类型。
callbackAsyncCallback<NotificationSlot>表示被指定的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

// getSlot回调
let getSlotCallback = (err: Base.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<NotificationSlot>

获取一个指定类型的通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
slotTypeSlotType通知渠道类型,目前分为社交通信、服务提醒、内容咨询和其他类型。

返回值:

类型说明
Promise<NotificationSlot>以Promise形式返回获取一个通知通道。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

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: Base.BusinessError) => {
    console.error(`getSlot fail: ${JSON.stringify(err)}`);
});

notificationManager.getSlots

getSlots(callback: AsyncCallback<Array<NotificationSlot>>): void

获取此应用程序的所有通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
callbackAsyncCallback<Array<NotificationSlot>>以callback形式返回获取此应用程序的所有通知通道的结果。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

// getSlots回调
let getSlotsCallback = (err: Base.BusinessError, data: Array<notificationManager.NotificationSlot>): 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<Array<NotificationSlot>>

获取此应用程序的所有通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

返回值:

类型说明
Promise<Array<NotificationSlot>>以Promise形式返回获取此应用程序的所有通知通道的结果。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.getSlots().then((data: Array<notificationManager.NotificationSlot>) => {
    console.info("getSlots success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getSlots fail: ${JSON.stringify(err)}`);
});

notificationManager.removeSlot

removeSlot(slotType: SlotType, callback: AsyncCallback<void>): void

删除此应用程序指定类型的通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
slotTypeSlotType通知渠道类型,目前分为社交通信、服务提醒、内容咨询和其他类型。
callbackAsyncCallback<void>表示被指定通道的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

// removeSlot回调
let removeSlotCallback = (err: Base.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<void>

删除此应用程序指定类型的通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
slotTypeSlotType通知渠道类型,目前分为社交通信、服务提醒、内容咨询和其他类型。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let slotType: notificationManager.SlotType = notificationManager.SlotType.SOCIAL_COMMUNICATION;
notificationManager.removeSlot(slotType).then(() => {
    console.info("removeSlot success");
}).catch((err: Base.BusinessError) => {
    console.error(`removeSlot fail: ${JSON.stringify(err)}`);
});

notificationManager.removeAllSlots

removeAllSlots(callback: AsyncCallback<void>): void

删除所有通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
callbackAsyncCallback<void>表示被指定通道的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let removeAllCallBack = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`removeAllSlots failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("removeAllSlots success");
    }
}
notificationManager.removeAllSlots(removeAllCallBack);

notificationManager.removeAllSlots

removeAllSlots(): Promise<void>

删除此应用程序所有通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.removeAllSlots().then(() => {
    console.info("removeAllSlots success");
}).catch((err: Base.BusinessError) => {
    console.error(`removeAllSlots fail: ${JSON.stringify(err)}`);
});

notificationManager.setNotificationEnable

setNotificationEnable(bundle: BundleOption, enable: boolean, callback: AsyncCallback<void>): void

设定指定应用的通知使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
enableboolean使能状态。
callbackAsyncCallback<void>设定通知使能回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let setNotificationEnableCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setNotificationEnableCallback failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setNotificationEnableCallback success");
    }
}
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.setNotificationEnable(bundle, false, setNotificationEnableCallback);

notificationManager.setNotificationEnable

setNotificationEnable(bundle: BundleOption, enable: boolean): Promise<void>

设定指定应用的通知使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
enableboolean使能状态。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.setNotificationEnable(bundle, false).then(() => {
    console.info("setNotificationEnable success");
}).catch((err: Base.BusinessError) => {
    console.error(`setNotificationEnable fail: ${JSON.stringify(err)}`);
});

notificationManager.isNotificationEnabled

isNotificationEnabled(bundle: BundleOption, callback: AsyncCallback<boolean>): void

获取指定应用的通知使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
callbackAsyncCallback<boolean>获取通知使能状态回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let isNotificationEnabledCallback = (err: Base.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)}`);
    }
}
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.isNotificationEnabled(bundle, isNotificationEnabledCallback);

notificationManager.isNotificationEnabled

isNotificationEnabled(bundle: BundleOption): Promise<boolean>

获取指定应用的通知使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。

返回值:

类型说明
Promise<boolean>以Promise形式返回获取指定应用的通知使能状态的结果。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.isNotificationEnabled(bundle).then((data: boolean) => {
    console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isNotificationEnabled fail: ${JSON.stringify(err)}`);
});

notificationManager.isNotificationEnabled

isNotificationEnabled(callback: AsyncCallback<boolean>): void

获取通知使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
callbackAsyncCallback<boolean>获取通知使能状态回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let isNotificationEnabledCallback = (err: Base.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.isNotificationEnabled

isNotificationEnabled(): Promise<boolean>

获取通知使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

返回值:

类型说明
Promise<boolean>以Promise形式返回获取通知使能状态的结果。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.isNotificationEnabled().then((data: boolean) => {
    console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isNotificationEnabled fail: ${JSON.stringify(err)}`);
});

notificationManager.isNotificationEnabled

isNotificationEnabled(userId: number, callback: AsyncCallback<boolean>): void

获取指定用户ID下的通知使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
userIdnumber指定的用户ID。
callbackAsyncCallback<boolean>获取通知使能状态回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.

示例:

import Base from '@ohos.base';

let isNotificationEnabledCallback = (err: Base.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)}`);
    }
}
let userId: number = 1;
notificationManager.isNotificationEnabled(userId, isNotificationEnabledCallback);

notificationManager.isNotificationEnabled

isNotificationEnabled(userId: number): Promise<boolean>

获取制定用户下的通知使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
userIdnumber指定的用户ID。

返回值:

类型说明
Promise<boolean>以Promise形式返回获取通知使能状态的结果。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist..

示例:

import Base from '@ohos.base';

let userId: number = 1;
notificationManager.isNotificationEnabled(userId).then((data: boolean) => {
    console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isNotificationEnabled fail: ${JSON.stringify(err)}`);
});

notificationManager.displayBadge

displayBadge(bundle: BundleOption, enable: boolean, callback: AsyncCallback<void>): void

设定指定应用的角标使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
enableboolean使能状态。
callbackAsyncCallback<void>设定角标使能回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let displayBadgeCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`displayBadge failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("displayBadge success");
    }
}
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.displayBadge(bundle, false, displayBadgeCallback);

notificationManager.displayBadge

displayBadge(bundle: BundleOption, enable: boolean): Promise<void>

设定指定应用的角标使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
enableboolean使能状态。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.displayBadge(bundle, false).then(() => {
    console.info("displayBadge success");
}).catch((err: Base.BusinessError) => {
    console.error(`displayBadge fail: ${JSON.stringify(err)}`);
});

notificationManager.isBadgeDisplayed

isBadgeDisplayed(bundle: BundleOption, callback: AsyncCallback<boolean>): void

获取指定应用的角标使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
callbackAsyncCallback<boolean>获取角标使能状态回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let isBadgeDisplayedCallback = (err: Base.BusinessError, data: boolean): void => {
    if (err) {
        console.error(`isBadgeDisplayed failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`isBadgeDisplayed success, data is ${JSON.stringify(data)}`);
    }
}
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.isBadgeDisplayed(bundle, isBadgeDisplayedCallback);

notificationManager.isBadgeDisplayed

isBadgeDisplayed(bundle: BundleOption): Promise<boolean>

获取指定应用的角标使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。

返回值:

类型说明
Promise<boolean>以Promise形式返回获取指定应用的角标使能状态。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
  bundle: "bundleName1",
};
notificationManager.isBadgeDisplayed(bundle).then((data: boolean) => {
    console.info("isBadgeDisplayed success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isBadgeDisplayed fail: ${JSON.stringify(err)}`);
});

notificationManager.setBadgeNumber10+

setBadgeNumber(badgeNumber: number): Promise<void>

设定角标个数,在应用的桌面图标上呈现(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
badgeNumbernumber角标个数。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

let badgeNumber: number = 10;
notificationManager.setBadgeNumber(badgeNumber).then(() => {
    console.info("displayBadge success");
}).catch((err: Base.BusinessError) => {
    console.error(`displayBadge fail: ${JSON.stringify(err)}`);
});

notificationManager.setBadgeNumber10+

setBadgeNumber(badgeNumber: number, callback: AsyncCallback<void>): void

设定角标个数,在应用的桌面图标上呈现(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
badgeNumbernumber角标个数。
callbackAsyncCallback<void>设定角标回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

let setBadgeNumberCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.info(`displayBadge failed code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("displayBadge success");
    }
}
let badgeNumber: number = 10;
notificationManager.setBadgeNumber(badgeNumber, setBadgeNumberCallback);

notificationManager.setSlotByBundle

setSlotByBundle(bundle: BundleOption, slot: NotificationSlot, callback: AsyncCallback<void>): void

设定指定应用的通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
slotNotificationSlot通知通道。
callbackAsyncCallback<void>设定通知通道回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let setSlotByBundleCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setSlotByBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setSlotByBundle success");
    }
}
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
let notificationSlot: notificationManager.NotificationSlot = {
    type: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
notificationManager.setSlotByBundle(bundle, notificationSlot, setSlotByBundleCallback);

notificationManager.setSlotByBundle

setSlotByBundle(bundle: BundleOption, slot: NotificationSlot): Promise<void>

设定指定应用的通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
slotNotificationSlot通知通道。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
let notificationSlot: notificationManager.NotificationSlot = {
    type: notificationManager.SlotType.SOCIAL_COMMUNICATION
};
notificationManager.setSlotByBundle(bundle, notificationSlot).then(() => {
    console.info("setSlotByBundle success");
}).catch((err: Base.BusinessError) => {
    console.error(`setSlotByBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.getSlotsByBundle

getSlotsByBundle(bundle: BundleOption, callback: AsyncCallback<Array<NotificationSlot>>): void

获取指定应用的所有通知通道(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
callbackAsyncCallback<Array<NotificationSlot>>获取通知通道回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let getSlotsByBundleCallback = (err: Base.BusinessError, data: Array<notificationManager.NotificationSlot>): void => {
    if (err) {
        console.error(`getSlotByBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`getSlotsByBundle success, data is ${JSON.stringify(data)}`);
    }
}
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.getSlotsByBundle(bundle, getSlotsByBundleCallback);

notificationManager.getSlotsByBundle

getSlotsByBundle(bundle: BundleOption): Promise<Array<NotificationSlot>>

获取指定应用的所有通知通道(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。

返回值:

类型说明
Promise<Array<NotificationSlot>>以Promise形式返回获取指定应用的通知通道。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.getSlotsByBundle(bundle).then((data: Array<notificationManager.NotificationSlot>) => {
    console.info("getSlotsByBundle success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getSlotsByBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.getSlotNumByBundle

getSlotNumByBundle(bundle: BundleOption, callback: AsyncCallback<number>): void

获取指定应用的通知通道数量(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。
callbackAsyncCallback<number>获取通知通道数量回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let getSlotNumByBundleCallback = (err: Base.BusinessError, data: number): void => {
    if (err) {
        console.error(`getSlotByBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`getSlotNumByBundle success data is ${JSON.stringify(data)}`);
    }
}
let bundle: notificationManager.BundleOption = {
  bundle: "bundleName1",
};
notificationManager.getSlotNumByBundle(bundle, getSlotNumByBundleCallback);

notificationManager.getSlotNumByBundle

getSlotNumByBundle(bundle: BundleOption): Promise<number>

获取指定应用的通知通道数量(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption指定应用的包信息。

返回值:

类型说明
Promise<number>以Promise形式返回获取指定应用的通知通道数量。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
  bundle: "bundleName1",
};
notificationManager.getSlotNumByBundle(bundle).then((data: number) => {
    console.info("getSlotNumByBundle success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getSlotsByBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.getAllActiveNotifications

getAllActiveNotifications(callback: AsyncCallback<Array<NotificationRequest>>): void

获取当前未删除的所有通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
callbackAsyncCallback<Array<NotificationRequest>>获取活动通知回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let getAllActiveNotificationsCallback = (err: Base.BusinessError, data: Array<notificationManager.NotificationRequest>): void => {
    if (err) {
        console.error(`getAllActiveNotifications failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`getAllActiveNotifications success, data is ${JSON.stringify(data)}`);
    }
}
notificationManager.getAllActiveNotifications(getAllActiveNotificationsCallback);

notificationManager.getAllActiveNotifications

getAllActiveNotifications(): Promise<Array<NotificationRequest>>

获取当前未删除的所有通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

返回值:

类型说明
Promise<Array<NotificationRequest>>以Promise形式返回获取活动通知。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.getAllActiveNotifications().then((data: Array<notificationManager.NotificationRequest>) => {
    console.info("getAllActiveNotifications success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getAllActiveNotifications fail: ${JSON.stringify(err)}`);
});

notificationManager.getActiveNotificationCount

getActiveNotificationCount(callback: AsyncCallback<number>): void

获取当前应用未删除的通知数(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
callbackAsyncCallback<number>获取未删除通知数回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let getActiveNotificationCountCallback = (err: Base.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<number>

获取当前应用未删除的通知数(Promise形式)。

系统能力:SystemCapability.Notification.Notification

返回值:

类型说明
Promise<number>以Promise形式返回获取当前应用未删除通知数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.getActiveNotificationCount().then((data: number) => {
    console.info("getActiveNotificationCount success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getActiveNotificationCount fail: ${JSON.stringify(err)}`);
});

notificationManager.getActiveNotifications

getActiveNotifications(callback: AsyncCallback<Array<NotificationRequest>>): void

获取当前应用未删除的通知列表(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
callbackAsyncCallback<Array<NotificationRequest>>获取当前应用通知列表回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let getActiveNotificationsCallback = (err: Base.BusinessError, data: Array<notificationManager.NotificationRequest>): void => {
    if (err) {
        console.error(`getActiveNotifications failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("getActiveNotifications success");
    }
}
notificationManager.getActiveNotifications(getActiveNotificationsCallback);

notificationManager.getActiveNotifications

getActiveNotifications(): Promise<Array<NotificationRequest>>

获取当前应用未删除的通知列表(Promise形式)。

系统能力:SystemCapability.Notification.Notification

返回值:

类型说明
Promise<Array<NotificationRequest>>以Promise形式返回获取当前应用通知列表。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.getActiveNotifications().then((data: Array<notificationManager.NotificationRequest>) => {
    console.info("removeGroupByBundle success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getActiveNotificationCount fail: ${JSON.stringify(err)}`);
});

notificationManager.cancelGroup

cancelGroup(groupName: string, callback: AsyncCallback<void>): void

取消本应用指定组下的通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
groupNamestring通知组名称,此名称需要在发布通知时通过NotificationRequest对象指定。
callbackAsyncCallback<void>取消本应用指定组下通知的回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let cancelGroupCallback = (err: Base.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<void>

取消本应用指定组下的通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
groupNamestring通知组名称。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let groupName: string = "GroupName";
notificationManager.cancelGroup(groupName).then(() => {
    console.info("cancelGroup success");
}).catch((err: Base.BusinessError) => {
    console.error(`cancelGroup fail: ${JSON.stringify(err)}`);
});

notificationManager.removeGroupByBundle

removeGroupByBundle(bundle: BundleOption, groupName: string, callback: AsyncCallback<void>): void

删除指定应用的指定组下的通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption应用的包信息。
groupNamestring通知组名称。
callbackAsyncCallback<void>删除指定应用指定组下通知的回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let removeGroupByBundleCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`removeGroupByBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("removeGroupByBundle success");
    }
}
let bundleOption: notificationManager.BundleOption = { bundle: "Bundle" };
let groupName: string = "GroupName";
notificationManager.removeGroupByBundle(bundleOption, groupName, removeGroupByBundleCallback);

notificationManager.removeGroupByBundle

removeGroupByBundle(bundle: BundleOption, groupName: string): Promise<void>

删除指定应用的指定组下的通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption应用的包信息。
groupNamestring通知组名称。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundleOption: notificationManager.BundleOption = { bundle: "Bundle" };
let groupName: string = "GroupName";

notificationManager.removeGroupByBundle(bundleOption, groupName).then(() => {
    console.info("removeGroupByBundle success");
}).catch((err: Base.BusinessError) => {
    console.error(`removeGroupByBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.setDoNotDisturbDate

setDoNotDisturbDate(date: DoNotDisturbDate, callback: AsyncCallback<void>): void

设置免打扰时间(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
dateDoNotDisturbDate免打扰时间选项。
callbackAsyncCallback<void>设置免打扰时间回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

let setDoNotDisturbDateCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setDoNotDisturbDate failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setDoNotDisturbDate success");
    }
}
let doNotDisturbDate: notificationManager.DoNotDisturbDate = {
    type: notificationManager.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};
notificationManager.setDoNotDisturbDate(doNotDisturbDate, setDoNotDisturbDateCallback);

notificationManager.setDoNotDisturbDate

setDoNotDisturbDate(date: DoNotDisturbDate): Promise<void>

设置免打扰时间(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
dateDoNotDisturbDate免打扰时间选项。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

let doNotDisturbDate: notificationManager.DoNotDisturbDate = {
    type: notificationManager.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};
notificationManager.setDoNotDisturbDate(doNotDisturbDate).then(() => {
    console.info("setDoNotDisturbDate success");
}).catch((err: Base.BusinessError) => {
    console.error(`setDoNotDisturbDate fail: ${JSON.stringify(err)}`);
});

notificationManager.setDoNotDisturbDate

setDoNotDisturbDate(date: DoNotDisturbDate, userId: number, callback: AsyncCallback<void>): void

指定用户设置免打扰时间(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
dateDoNotDisturbDate免打扰时间选项。
userIdnumber设置免打扰时间的用户ID。
callbackAsyncCallback<void>设置免打扰时间回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.
1600012No memory space.

示例:

import Base from '@ohos.base';

let setDoNotDisturbDateCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setDoNotDisturbDate failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setDoNotDisturbDate success");
    }
}
let doNotDisturbDate: notificationManager.DoNotDisturbDate = {
    type: notificationManager.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};
let userId: number = 1;
notificationManager.setDoNotDisturbDate(doNotDisturbDate, userId, setDoNotDisturbDateCallback);

notificationManager.setDoNotDisturbDate

setDoNotDisturbDate(date: DoNotDisturbDate, userId: number): Promise<void>

指定用户设置免打扰时间(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
dateDoNotDisturbDate免打扰时间选项。
userIdnumber设置免打扰时间的用户ID。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.
1600012No memory space.

示例:

import Base from '@ohos.base';

let doNotDisturbDate: notificationManager.DoNotDisturbDate = {
    type: notificationManager.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};
let userId: number = 1;
notificationManager.setDoNotDisturbDate(doNotDisturbDate, userId).then(() => {
    console.info("setDoNotDisturbDate success");
}).catch((err: Base.BusinessError) => {
    console.error(`setDoNotDisturbDate fail: ${JSON.stringify(err)}`);
});

notificationManager.getDoNotDisturbDate

getDoNotDisturbDate(callback: AsyncCallback<DoNotDisturbDate>): void

查询免打扰时间(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
callbackAsyncCallback<DoNotDisturbDate>查询免打扰时间回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

let getDoNotDisturbDateCallback = (err: Base.BusinessError, data: notificationManager.DoNotDisturbDate): void => {
    if (err) {
        console.error(`getDoNotDisturbDate failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`getDoNotDisturbDate success, data is ${JSON.stringify(data)}`);
    }
}
notificationManager.getDoNotDisturbDate(getDoNotDisturbDateCallback);

notificationManager.getDoNotDisturbDate

getDoNotDisturbDate(): Promise<DoNotDisturbDate>

查询免打扰时间(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

返回值:

类型说明
Promise<DoNotDisturbDate>以Promise形式返回获取查询到的免打扰时间。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

示例:

import Base from '@ohos.base';

notificationManager.getDoNotDisturbDate().then((data: notificationManager.DoNotDisturbDate) => {
  console.info("getDoNotDisturbDate success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getDoNotDisturbDate fail: ${JSON.stringify(err)}`);
});

notificationManager.getDoNotDisturbDate

getDoNotDisturbDate(userId: number, callback: AsyncCallback<DoNotDisturbDate>): void

查询指定用户的免打扰时间(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
callbackAsyncCallback<DoNotDisturbDate>查询免打扰时间回调函数。
userIdnumber用户ID。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.
1600012No memory space.

示例:

import Base from '@ohos.base';

let getDoNotDisturbDateCallback = (err: Base.BusinessError, data: notificationManager.DoNotDisturbDate): void => {
    if (err) {
        console.error(`getDoNotDisturbDate failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`getDoNotDisturbDate success, data is ${JSON.stringify(data)}`);
    }
}
let userId: number = 1;
notificationManager.getDoNotDisturbDate(userId, getDoNotDisturbDateCallback);

notificationManager.getDoNotDisturbDate

getDoNotDisturbDate(userId: number): Promise<DoNotDisturbDate>

查询指定用户的免打扰时间(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
userIdnumber用户ID。

返回值:

类型说明
Promise<DoNotDisturbDate>以Promise形式返回获取查询到的免打扰时间。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.
1600012No memory space.

示例:

import Base from '@ohos.base';

let userId: number = 1;
notificationManager.getDoNotDisturbDate(userId).then((data: notificationManager.DoNotDisturbDate) => {
    console.info("getDoNotDisturbDate success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getDoNotDisturbDate fail: ${JSON.stringify(err)}`);
});

notificationManager.isSupportDoNotDisturbMode

isSupportDoNotDisturbMode(callback: AsyncCallback<boolean>): void

查询是否支持免打扰功能(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
callbackAsyncCallback<boolean>查询是否支持免打扰功能回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let isSupportDoNotDisturbModeCallback = (err: Base.BusinessError, data: boolean): void => {
    if (err) {
        console.error(`isSupportDoNotDisturbMode failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("isSupportDoNotDisturbMode success");
    }
}
notificationManager.isSupportDoNotDisturbMode(isSupportDoNotDisturbModeCallback);

notificationManager.isSupportDoNotDisturbMode

isSupportDoNotDisturbMode(): Promise<boolean>

查询是否支持免打扰功能(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

返回值:

类型说明
Promise<boolean>以Promise形式返回获取是否支持免打扰功能的结果(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.isSupportDoNotDisturbMode().then((data: boolean) => {
    console.info("supportDoNotDisturbMode success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`supportDoNotDisturbMode fail: ${JSON.stringify(err)}`);
});

notificationManager.isSupportTemplate

isSupportTemplate(templateName: string, callback: AsyncCallback<boolean>): void

查询模板是否存在(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
templateNamestring模板名称。
callbackAsyncCallback<boolean>查询模板是否存在的回调函数(true:存在,false:不存在)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let templateName: string = 'process';
let isSupportTemplateCallback = (err: Base.BusinessError, data: boolean): void => {
    if (err) {
        console.error(`isSupportTemplate failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("isSupportTemplate success");
    }
}
notificationManager.isSupportTemplate(templateName, isSupportTemplateCallback);

notificationManager.isSupportTemplate

isSupportTemplate(templateName: string): Promise<boolean>

查询模板是否存在(Promise形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
templateNamestring模板名称。

返回值:

类型说明
Promise<boolean>Promise方式返回模板是否存在的结果(true:存在,false:不存在)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let templateName: string = 'process';

notificationManager.isSupportTemplate(templateName).then((data: boolean) => {
    console.info("isSupportTemplate success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isSupportTemplate fail: ${JSON.stringify(err)}`);
});

notificationManager.requestEnableNotification

requestEnableNotification(callback: AsyncCallback<void>): void

应用请求通知使能(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
callbackAsyncCallback<void>应用请求通知使能的回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let requestEnableNotificationCallback = (err: Base.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

requestEnableNotification(): Promise<void>

应用请求通知使能(Promise形式)。

系统能力:SystemCapability.Notification.Notification

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.requestEnableNotification().then(() => {
    console.info("requestEnableNotification success");
}).catch((err: Base.BusinessError) => {
    console.error(`requestEnableNotification fail: ${JSON.stringify(err)}`);
});

notificationManager.requestEnableNotification10+

requestEnableNotification(context: UIAbilityContext, callback: AsyncCallback<void>): void

应用请求通知使能模态弹窗(callback形式)。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
contextUIAbilityContext通知弹窗绑定Ability的上下文。
callbackAsyncCallback<void>应用请求通知使能的回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import Want from '@ohos.app.ability.Want';

class MyAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    let requestEnableNotificationCallback = (err: Base.BusinessError): void => {
      if (err) {
        console.error(`requestEnableNotification failed, code is ${err.code}, message is ${err.message}`);
      } else {
        console.info("requestEnableNotification success");
      }
    };
    notificationManager.requestEnableNotification(this.context, requestEnableNotificationCallback);
  }
}

notificationManager.requestEnableNotification10+

requestEnableNotification(context: UIAbilityContext): Promise<void>

应用请求通知使能模态弹窗(Promise形式)。

模型约束:此接口仅可在Stage模型下使用。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
contextUIAbilityContext通知弹窗绑定Ability的上下文。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';
import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import Want from '@ohos.app.ability.Want';

class MyAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    notificationManager.requestEnableNotification(this.context).then(() => {
      console.info("requestEnableNotification success");
    }).catch((err: Base.BusinessError) => {
      console.error(`requestEnableNotification fail: ${JSON.stringify(err)}`);
    });
  }
}

notificationManager.setDistributedEnable

setDistributedEnable(enable: boolean, callback: AsyncCallback<void>): void

设置设备是否支持分布式通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
enableboolean是否支持(true:支持,false:不支持)。
callbackAsyncCallback<void>设置设备是否支持分布式通知的回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.

示例:

import Base from '@ohos.base';

let setDistributedEnableCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setDistributedEnable failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setDistributedEnable success");
    }
};
let enable: boolean = true;
notificationManager.setDistributedEnable(enable, setDistributedEnableCallback);

notificationManager.setDistributedEnable

setDistributedEnable(enable: boolean): Promise<void>

设置设备是否支持分布式通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
enableboolean是否支持(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.

示例:

import Base from '@ohos.base';

let enable: boolean = true;
notificationManager.setDistributedEnable(enable).then(() => {
    console.info("setDistributedEnable success");
}).catch((err: Base.BusinessError) => {
    console.error(`setDistributedEnable fail: ${JSON.stringify(err)}`);
});

notificationManager.isDistributedEnabled

isDistributedEnabled(callback: AsyncCallback<boolean>): void

查询设备是否支持分布式通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

参数:

参数名类型必填说明
callbackAsyncCallback<boolean>设备是否支持分布式通知的回调函数(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.

示例:

import Base from '@ohos.base';

let isDistributedEnabledCallback = (err: Base.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<boolean>

查询设备是否支持分布式通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

返回值:

类型说明
Promise<boolean>Promise方式返回设备是否支持分布式通知的结果(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.

示例:

import Base from '@ohos.base';

notificationManager.isDistributedEnabled()
.then((data: boolean) => {
    console.info("isDistributedEnabled success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isDistributedEnabled fail: ${JSON.stringify(err)}`);
});

notificationManager.setDistributedEnableByBundle

setDistributedEnableByBundle(bundle: BundleOption, enable: boolean, callback: AsyncCallback<void>): void

设置指定应用是否支持分布式通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption应用的包信息。
enableboolean是否支持。
callbackAsyncCallback<void>应用程序是否支持分布式通知的回调函数(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let setDistributedEnableByBundleCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setDistributedEnableByBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("enableDistributedByBundle success");
    }
};
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
let enable: boolean = true;
notificationManager.setDistributedEnableByBundle(bundle, enable, setDistributedEnableByBundleCallback);

notificationManager.setDistributedEnableByBundle

setDistributedEnableByBundle(bundle: BundleOption, enable: boolean): Promise<void>

设置指定应用是否支持分布式通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption应用的包。
enableboolean指定应用是否支持分布式通知(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
let enable: boolean = true;
notificationManager.setDistributedEnableByBundle(bundle, enable).then(() => {
    console.info("setDistributedEnableByBundle success");
}).catch((err: Base.BusinessError) => {
    console.error(`setDistributedEnableByBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.isDistributedEnabledByBundle

isDistributedEnabledByBundle(bundle: BundleOption, callback: AsyncCallback<boolean>): void

根据应用的包获取应用程序是否支持分布式通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption应用的包。
callbackAsyncCallback<boolean>查询指定应用是否支持分布式通知的回调函数(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let isDistributedEnabledByBundleCallback = (err: Base.BusinessError, data: boolean): void => {
    if (err) {
        console.error(`isDistributedEnabledByBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("isDistributedEnabledByBundle success" + JSON.stringify(data));
    }
};
let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.isDistributedEnabledByBundle(bundle, isDistributedEnabledByBundleCallback);

notificationManager.isDistributedEnabledByBundle

isDistributedEnabledByBundle(bundle: BundleOption): Promise<boolean>

查询指定应用是否支持分布式通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleBundleOption应用的包。

返回值:

类型说明
Promise<boolean>Promise方式返回指定应用是否支持分布式通知的结果(true:支持,false:不支持)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600010Distributed operation failed.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

let bundle: notificationManager.BundleOption = {
    bundle: "bundleName1",
};
notificationManager.isDistributedEnabledByBundle(bundle).then((data: boolean) => {
    console.info("isDistributedEnabledByBundle success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isDistributedEnabledByBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.getDeviceRemindType

getDeviceRemindType(callback: AsyncCallback<DeviceRemindType>): void

获取通知的提醒方式(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
callbackAsyncCallback<DeviceRemindType>获取通知提醒方式的回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

let getDeviceRemindTypeCallback = (err: Base.BusinessError, data: notificationManager.DeviceRemindType): void => {
    if (err) {
        console.error(`getDeviceRemindType failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`getDeviceRemindType success, data is ${JSON.stringify(data)}`);
    }
};
notificationManager.getDeviceRemindType(getDeviceRemindTypeCallback);

notificationManager.getDeviceRemindType

getDeviceRemindType(): Promise<DeviceRemindType>

获取通知的提醒方式(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

返回值:

类型说明
Promise<DeviceRemindType>Promise方式返回获取通知提醒方式的结果。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

示例:

import Base from '@ohos.base';

notificationManager.getDeviceRemindType().then((data: notificationManager.DeviceRemindType) => {
    console.info("getDeviceRemindType success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`getDeviceRemindType fail: ${JSON.stringify(err)}`);
});

notificationManager.publishAsBundle

publishAsBundle(request: NotificationRequest, representativeBundle: string, userId: number, callback: AsyncCallback<void>): void

发布代理通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER,ohos.permission.NOTIFICATION_AGENT_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
requestNotificationRequest用于设置要发布通知的内容和相关配置信息。
representativeBundlestring被代理应用的包名。
userIdnumber用户ID。
callbackAsyncCallback<void>发布代理通知的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600004Notification is not enabled.
1600005Notification slot is not enabled.
1600008The user is not exist.
1600009Over max number notifications per second.
1600012No memory space.

示例:

import Base from '@ohos.base';

//publishAsBundle回调
let callback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`publishAsBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("publishAsBundle success");
    }
}
// 被代理应用的包名
let representativeBundle: string = "com.example.demo";
// 用户ID
let userId: number = 100;
// NotificationRequest对象
let request: notificationManager.NotificationRequest = {
    id: 1,
    content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};
notificationManager.publishAsBundle(request, representativeBundle, userId, callback);

notificationManager.publishAsBundle

publishAsBundle(request: NotificationRequest, representativeBundle: string, userId: number): Promise<void>

发布代理通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER,ohos.permission.NOTIFICATION_AGENT_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
requestNotificationRequest用于设置要发布通知的内容和相关配置信息。
representativeBundlestring被代理应用的包名。
userIdnumber用户ID。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600004Notification is not enabled.
1600005Notification slot is not enabled.
1600008The user is not exist.
1600009Over max number notifications per second.
1600012No memory space.

示例:

import Base from '@ohos.base';

// 被代理应用的包名
let representativeBundle: string = "com.example.demo";
// 用户ID
let userId: number = 100;
// NotificationRequest对象
let request: notificationManager.NotificationRequest = {
    id: 1,
    content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};
notificationManager.publishAsBundle(request, representativeBundle, userId).then(() => {
    console.info("publishAsBundle success");
}).catch((err: Base.BusinessError) => {
    console.error(`publishAsBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.cancelAsBundle

cancelAsBundle(id: number, representativeBundle: string, userId: number, callback: AsyncCallback<void>): void

取消代理通知(callback形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER,ohos.permission.NOTIFICATION_AGENT_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
idnumber通知ID。
representativeBundlestring被代理应用的包名。
userIdnumber用户ID。
callbackAsyncCallback取消代理通知的回调方法。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.
1600008The user is not exist.

示例:

import Base from '@ohos.base';

// cancelAsBundle
let cancelAsBundleCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`cancelAsBundle failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("cancelAsBundle success");
    }
}
// 被代理应用的包名
let representativeBundle: string = "com.example.demo";
// 用户ID
let userId: number = 100;
notificationManager.cancelAsBundle(0, representativeBundle, userId, cancelAsBundleCallback);

notificationManager.cancelAsBundle

cancelAsBundle(id: number, representativeBundle: string, userId: number): Promise<void>

取消代理通知(Promise形式)。

系统能力:SystemCapability.Notification.Notification

需要权限: ohos.permission.NOTIFICATION_CONTROLLER,ohos.permission.NOTIFICATION_AGENT_CONTROLLER

系统API: 此接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
idnumber通知ID。
representativeBundlestring被代理应用的包名。
userIdnumber用户ID。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.
1600008The user is not exist.

示例:

import Base from '@ohos.base';

// 被代理应用的包名
let representativeBundle: string = "com.example.demo";
// 用户ID
let userId: number = 100;
notificationManager.cancelAsBundle(0, representativeBundle, userId).then(() => {
    console.info("cancelAsBundle success");
}).catch((err: Base.BusinessError) => {
    console.error(`cancelAsBundle fail: ${JSON.stringify(err)}`);
});

notificationManager.setNotificationEnableSlot

setNotificationEnableSlot(bundle: BundleOption, type: SlotType, enable: boolean, callback: AsyncCallback<void>): void

设置指定应用的指定渠道类型的使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
bundleBundleOption应用的包信息。
typeSlotType指定渠道类型。
enableboolean使能状态(true:使能,false:禁止)。
callbackAsyncCallback<void>设置渠道使能回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

// setNotificationEnableSlot
let setNotificationEnableSlotCallback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setNotificationEnableSlot failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setNotificationEnableSlot success");
    }
};
notificationManager.setNotificationEnableSlot(
    { bundle: "ohos.samples.notification", },
    notificationManager.SlotType.SOCIAL_COMMUNICATION,
    true,
    setNotificationEnableSlotCallback);

notificationManager.setNotificationEnableSlot

setNotificationEnableSlot(bundle: BundleOption, type: SlotType, enable: boolean): Promise<void>

设置指定应用的指定渠道类型的使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
bundleBundleOption应用的包信息。
typeSlotType渠道类型。
enableboolean使能状态(true:使能,false:禁止)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

// setNotificationEnableSlot
notificationManager.setNotificationEnableSlot(
    { bundle: "ohos.samples.notification", },
    notificationManager.SlotType.SOCIAL_COMMUNICATION,
    true).then(() => {
        console.info("setNotificationEnableSlot success");
    }).catch((err: Base.BusinessError) => {
        console.error(`setNotificationEnableSlot fail: ${JSON.stringify(err)}`);
    });

notificationManager.isNotificationSlotEnabled

isNotificationSlotEnabled(bundle: BundleOption, type: SlotType, callback: AsyncCallback<boolean>): void

获取指定应用的指定渠道类型的使能状态(callback形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
bundleBundleOption应用的包信息。
typeSlotType渠道类型。
callbackAsyncCallback<boolean>获取渠道使能状态回调函数(true:使能,false:禁止)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

// isNotificationSlotEnabled
let getEnableSlotCallback = (err: Base.BusinessError, data: boolean): void => {
    if (err) {
        console.error(`isNotificationSlotEnabled failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info(`isNotificationSlotEnabled success, data is ${JSON.stringify(data)}`);
    }
};
notificationManager.isNotificationSlotEnabled(
    { bundle: "ohos.samples.notification", },
    notificationManager.SlotType.SOCIAL_COMMUNICATION,
    getEnableSlotCallback);

notificationManager.isNotificationSlotEnabled

isNotificationSlotEnabled(bundle: BundleOption, type: SlotType): Promise<boolean>

获取指定应用的指定渠道类型的使能状态(Promise形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
bundleBundleOption应用的包信息。
typeSlotType渠道类型。

返回值:

类型说明
Promise<boolean>以Promise形式返回指定类型的渠道使能状态。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

示例:

import Base from '@ohos.base';

// isNotificationSlotEnabled
notificationManager.isNotificationSlotEnabled({ bundle: "ohos.samples.notification", },
    notificationManager.SlotType.SOCIAL_COMMUNICATION).then((data: boolean) => {
    console.info("isNotificationSlotEnabled success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
    console.error(`isNotificationSlotEnabled fail: ${JSON.stringify(err)}`);
});

notificationManager.setSyncNotificationEnabledWithoutApp

setSyncNotificationEnabledWithoutApp(userId: number, enable: boolean, callback: AsyncCallback<void>): void

设置是否将通知同步到未安装应用程序的设备(callback形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
userIdnumber用户ID。
enableboolean是否启用(true:使能,false:禁止)。
callbackAsyncCallback<void>设置是否将通知同步到未安装应用程序的设备的回调函数。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.

示例:

import Base from '@ohos.base';

let userId: number = 100;
let enable: boolean = true;
let callback = (err: Base.BusinessError): void => {
    if (err) {
        console.error(`setSyncNotificationEnabledWithoutApp failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setSyncNotificationEnabledWithoutApp success");
    }
}
notificationManager.setSyncNotificationEnabledWithoutApp(userId, enable, callback);

notificationManager.setSyncNotificationEnabledWithoutApp

setSyncNotificationEnabledWithoutApp(userId: number, enable: boolean): Promise<void>

设置是否将通知同步到未安装应用程序的设备(Promise形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
userIdnumber用户ID。
enableboolean是否启用(true:使能,false:禁止)。

返回值:

类型说明
Promise<void>以Promise形式返回设置是否将通知同步到未安装应用程序的设备的结果。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.

示例:

import Base from '@ohos.base';

let userId: number = 100;
let enable: boolean = true;
notificationManager.setSyncNotificationEnabledWithoutApp(userId, enable).then(() => {
    console.info('setSyncNotificationEnabledWithoutApp success');
}).catch((err: Base.BusinessError) => {
    console.error(`setSyncNotificationEnabledWithoutApp fail: ${JSON.stringify(err)}`);
});

notificationManager.getSyncNotificationEnabledWithoutApp

getSyncNotificationEnabledWithoutApp(userId: number, callback: AsyncCallback<boolean>): void

获取同步通知到未安装应用程序设备的开关是否开启(callback形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
userIdnumber用户ID。
callbackAsyncCallback<boolean>获取同步通知到未安装应用程序设备的开关是否开启的回调函数(true:开启,false:未开启)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.

示例:

import Base from '@ohos.base';

let userId: number = 100;
let getSyncNotificationEnabledWithoutAppCallback = (err: Base.BusinessError, data: boolean): void => {
    if (err) {
        console.info('getSyncNotificationEnabledWithoutAppCallback, err:' + err);
    } else {
        console.info('getSyncNotificationEnabledWithoutAppCallback, data:' + data);
    }
}
notificationManager.getSyncNotificationEnabledWithoutApp(userId, getSyncNotificationEnabledWithoutAppCallback);

notificationManager.getSyncNotificationEnabledWithoutApp

getSyncNotificationEnabledWithoutApp(userId: number): Promise<boolean>

获取同步通知到未安装应用程序设备的开关是否开启(Promise形式)。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER

参数:

参数名类型必填说明
userIdnumber用户ID。

返回值:

类型说明
Promise<boolean>以Promise形式返回获取同步通知到未安装应用程序设备的开关是否开启的结果(true:开启,false:未开启)。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.

示例:

import Base from '@ohos.base';

let userId: number = 100;
notificationManager.getSyncNotificationEnabledWithoutApp(userId).then((data: boolean) => {
  console.info('getSyncNotificationEnabledWithoutApp, data:' + data);
}).catch((err: Base.BusinessError) => {
    console.error(`getSyncNotificationEnabledWithoutApp fail: ${JSON.stringify(err)}`);
});

notificationManager.on10+

on(type: 'checkNotification', callback: (checkInfo: NotificationCheckInfo) => NotificationCheckResult): void;

注册通知监听回调。通知服务将通知信息回调给校验程序,校验程序返回校验结果决定该通知是否发布,如营销类通知发布频率控制等。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER 和 ohos.permission.NOTIFICATION_AGENT_CONTROLLER

参数:

参数名类型必填说明
typestring回调函数类型名,固定为'checkNotification'。
callback(checkInfo: NotificationCheckInfo) => NotificationCheckResult消息验证函数指针。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.

示例:

import Base from '@ohos.base';

let onCheckNotification = (info : notificationManager.NotificationCheckInfo): notificationManager.NotificationCheckResult => {
    console.info(`====>OnCheckNotification info: ${JSON.stringify(info)}`);
    if(info.notificationId == 1){
        let result: notificationManager.NotificationCheckResult =  { code: 1, message: "testMsg1"};
        return result;
    } else {
        let result: notificationManager.NotificationCheckResult =   { code: 0, message: "testMsg0"};
        return result;
    }
}
try{
    notificationManager.on("checkNotification", onCheckNotification);
} catch (error){
    console.error(`notificationManager.on error: ${JSON.stringify(error as Base.BusinessError)}`);
}

notificationManager.off10+

off(type: 'checkNotification', callback?: (checkInfo: NotificationCheckInfo) => NotificationCheckResult): void;

取消通知监听回调。

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER 和 ohos.permission.NOTIFICATION_AGENT_CONTROLLER

参数:

参数名类型必填说明
typestring回调函数类型名,固定为'checkNotification'。
callback(checkInfo: NotificationCheckInfo) => NotificationCheckResult消息验证函数指针。

错误码:

错误码详细介绍请参考errcode-notification

错误码ID错误信息
1600001Internal error.

示例:

import Base from '@ohos.base';

try{
    notificationManager.off("checkNotification");
} catch (error){
    console.error(`notificationManager.off error: ${JSON.stringify(error as Base.BusinessError)}`);
}

DoNotDisturbDate

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

名称类型必填说明
typeDoNotDisturbType免打扰设置的时间类型。
beginDate免打扰设置的起点时间。
endDate免打扰设置的终点时间。

DoNotDisturbType

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

系统API: 此接口为系统接口,三方应用不支持调用。

名称说明
TYPE_NONE0非通知勿扰类型。
TYPE_ONCE1以设置时间段(只看小时和分钟)一次执行勿扰。
TYPE_DAILY2以设置时间段(只看小时和分钟)每天执行勿扰。
TYPE_CLEARLY3以设置时间段(明确月日时)执行勿扰。

ContentType

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

名称说明
NOTIFICATION_CONTENT_BASIC_TEXT0普通类型通知。
NOTIFICATION_CONTENT_LONG_TEXT1长文本类型通知。
NOTIFICATION_CONTENT_PICTURE2图片类型通知。
NOTIFICATION_CONTENT_CONVERSATION3社交类型通知(暂不支持该类型)。
NOTIFICATION_CONTENT_MULTILINE4多行文本类型通知。

SlotLevel

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

名称说明
LEVEL_NONE0表示关闭通知功能。
LEVEL_MIN1表示通知功能已启用,但状态栏中不显示通知图标,且没有横幅或提示音。
LEVEL_LOW2表示通知功能已启用,且状态栏中显示通知图标,但没有横幅或提示音。
LEVEL_DEFAULT3表示通知功能已启用,状态栏中显示通知图标,没有横幅但有提示音。
LEVEL_HIGH4表示通知功能已启用,状态栏中显示通知图标,有横幅和提示音。

SlotType

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

名称说明
UNKNOWN_TYPE0未知类型。
SOCIAL_COMMUNICATION1社交类型。
SERVICE_INFORMATION2服务类型。
CONTENT_INFORMATION3内容类型。
OTHER_TYPES0xFFFF其他类型。

DeviceRemindType

系统能力:SystemCapability.Notification.Notification

系统API: 此接口为系统接口,三方应用不支持调用。

名称说明
IDLE_DONOT_REMIND0设备未被使用,无需提醒。
IDLE_REMIND1提醒设备未被使用。
ACTIVE_DONOT_REMIND2设备正在使用,无需提醒。
ACTIVE_REMIND3提醒设备正在使用。

SourceType

系统能力:SystemCapability.Notification.Notification

系统API: 此接口为系统接口,三方应用不支持调用。

名称说明
TYPE_NORMAL0一般通知。
TYPE_CONTINUOUS1连续通知。
TYPE_TIMER2计划通知。

NotificationCheckInfo10+

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER 和 ohos.permission.NOTIFICATION_AGENT_CONTROLLER

名称类型必填说明
bundleNamestringbundle名称。
notificationIdnumber通知Id。
contentTypeContentType通知类型。

NotificationCheckResult10+

系统能力:SystemCapability.Notification.Notification

系统API:此接口为系统接口,三方应用不支持调用。

需要权限:ohos.permission.NOTIFICATION_CONTROLLER 和 ohos.permission.NOTIFICATION_AGENT_CONTROLLER

名称类型必填说明
codenumber0-display, 1-no display。
messagestring结果信息。

最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

鸿蒙(HarmonyOS NEXT)最新学习路线

  •  HarmonOS基础技能

  • HarmonOS就业必备技能 
  •  HarmonOS多媒体技术

  • 鸿蒙NaPi组件进阶

  • HarmonOS高级技能

  • 初识HarmonOS内核 
  • 实战就业级设备开发

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

《鸿蒙 (OpenHarmony)开发入门教学视频》

《鸿蒙生态应用开发V2.0白皮书》

图片

《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

图片

 《鸿蒙开发基础》

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……

图片

 《鸿蒙开发进阶》

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

图片

《鸿蒙进阶实战》

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

图片

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料

总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值