@ohos.notificationManager (NotificationManager)

The notificationManager module provides notification management capabilities, covering notifications, notification slots, notification enabled status, and notification badge status.

NOTE

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

Modules to Import

import Notification from '@ohos.notificationManager';

Notification.publish

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

Publishes a notification. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
request NotificationRequest Yes Content and related configuration of the notification to publish.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600004 Notification is not enabled.
1600005 Notification slot is not enabled.
1600009 Over max number notifications per second.

Example

// publish callback
function publishCallback(err) {
    if (err) {
        console.info("publish failed " + JSON.stringify(err));
    } else {
        console.info("publish success");
    }
}
// NotificationRequest object
let notificationRequest = {
    id: 1,
    content: {
        contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};
Notification.publish(notificationRequest, publishCallback);

Notification.publish

publish(request: NotificationRequest): Promise<void>

Publishes a notification. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
request NotificationRequest Yes Content and related configuration of the notification to publish.

Error codes

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

ID Error Message
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600004 Notification is not enabled.
1600005 Notification slot is not enabled.
1600009 Over max number notifications per second.

Example

// NotificationRequest object
let notificationRequest = {
    notificationId: 1,
    content: {
        contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};
Notification.publish(notificationRequest).then(() => {
	console.info("publish success");
});

Notification.publish

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

Publishes a notification to a specified user. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
request NotificationRequest Yes Content and related configuration of the notification to publish.
userId number Yes User ID.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600004 Notification is not enabled.
1600005 Notification slot is not enabled.
1600008 The user is not exist.
1600009 Over max number notifications per second.

Example

// publish callback
function publishCallback(err) {
    if (err) {
        console.info("publish failed " + JSON.stringify(err));
    } else {
        console.info("publish success");
    }
}
// User ID
let userId = 1;
// NotificationRequest object
let notificationRequest = {
    id: 1,
    content: {
        contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};
Notification.publish(notificationRequest, userId, publishCallback);

Notification.publish

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

Publishes a notification to a specified user. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
request NotificationRequest Yes Content and related configuration of the notification to publish.
userId number Yes User ID.

Error codes

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

ID Error Message
201 Permission denied.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600004 Notification is not enabled.
1600005 Notification slot is not enabled.
1600008 The user is not exist.
1600009 Over max number notifications per second.

Example

let notificationRequest = {
    notificationId: 1,
    content: {
        contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};

let userId = 1;

Notification.publish(notificationRequest, userId).then(() => {
	console.info("publish success");
});

Notification.cancel

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

Cancels a notification with the specified ID and label. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
id number Yes Notification ID.
label string Yes Notification label.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600007 The notification is not exist.

Example

// cancel callback
function cancelCallback(err) {
    if (err) {
        console.info("cancel failed " + JSON.stringify(err));
    } else {
        console.info("cancel success");
    }
}
Notification.cancel(0, "label", cancelCallback);

Notification.cancel

cancel(id: number, label?: string): Promise<void>

Cancels a notification with the specified ID and optional label. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
id number Yes Notification ID.
label string No Notification label.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600007 The notification is not exist.

Example

Notification.cancel(0).then(() => {
	console.info("cancel success");
});

Notification.cancel

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

Cancels a notification with the specified ID. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
id number Yes Notification ID.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600007 The notification is not exist.

Example

// cancel callback
function cancelCallback(err) {
    if (err) {
        console.info("cancel failed " + JSON.stringify(err));
    } else {
        console.info("cancel success");
    }
}
Notification.cancel(0, cancelCallback);

Notification.cancelAll

cancelAll(callback: AsyncCallback<void>): void

Cancels all notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result.

Example

// cancel callback
function cancelAllCallback(err) {
    if (err) {
        console.info("cancelAll failed " + JSON.stringify(err));
    } else {
        console.info("cancelAll success");
    }
}
Notification.cancelAll(cancelAllCallback);

Notification.cancelAll

cancelAll(): Promise<void>

Cancels all notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.cancelAll().then(() => {
	console.info("cancelAll success");
});

Notification.addSlot

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

Adds a notification slot. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
slot NotificationSlot Yes Notification slot to add.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// addSlot callback
function addSlotCallBack(err) {
    if (err) {
        console.info("addSlot failed " + JSON.stringify(err));
    } else {
        console.info("addSlot success");
    }
}
// NotificationSlot object
let notificationSlot = {
    type: Notification.SlotType.SOCIAL_COMMUNICATION
};
Notification.addSlot(notificationSlot, addSlotCallBack);

Notification.addSlot

addSlot(slot: NotificationSlot): Promise<void>

Adds a notification slot. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
slot NotificationSlot Yes Notification slot to add.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// NotificationSlot object
let notificationSlot = {
    type: Notification.SlotType.SOCIAL_COMMUNICATION
};
Notification.addSlot(notificationSlot).then(() => {
	console.info("addSlot success");
});

Notification.addSlot

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

Adds a notification slot of a specified type. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
type SlotType Yes Type of the notification slot to add.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// addSlot callback
function addSlotCallBack(err) {
    if (err) {
        console.info("addSlot failed " + JSON.stringify(err));
    } else {
        console.info("addSlot success");
    }
}
Notification.addSlot(Notification.SlotType.SOCIAL_COMMUNICATION, addSlotCallBack);

Notification.addSlot

addSlot(type: SlotType): Promise<void>

Adds a notification slot of a specified type. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
type SlotType Yes Type of the notification slot to add.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.addSlot(Notification.SlotType.SOCIAL_COMMUNICATION).then(() => {
	console.info("addSlot success");
});

Notification.addSlots

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

Adds an array of notification slots. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
slots Array<NotificationSlot> Yes Notification slots to add.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// addSlots callback
function addSlotsCallBack(err) {
    if (err) {
        console.info("addSlots failed " + JSON.stringify(err));
    } else {
        console.info("addSlots success");
    }
}
// NotificationSlot object
let notificationSlot = {
    type: Notification.SlotType.SOCIAL_COMMUNICATION
};
// NotificationSlotArray object
let notificationSlotArray = new Array();
notificationSlotArray[0] = notificationSlot;

Notification.addSlots(notificationSlotArray, addSlotsCallBack);

Notification.addSlots

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

Adds an array of notification slots. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
slots Array<NotificationSlot> Yes Notification slots to add.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// NotificationSlot object
let notificationSlot = {
    type: Notification.SlotType.SOCIAL_COMMUNICATION
};
// NotificationSlotArray object
let notificationSlotArray = new Array();
notificationSlotArray[0] = notificationSlot;

Notification.addSlots(notificationSlotArray).then(() => {
	console.info("addSlots success");
});

Notification.getSlot

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

Obtains a notification slot of a specified type. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
slotType SlotType Yes Type of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.
callback AsyncCallback<NotificationSlot> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// getSlot callback
function getSlotCallback(err,data) {
    if (err) {
        console.info("getSlot failed " + JSON.stringify(err));
    } else {
        console.info("getSlot success");
    }
}
let slotType = Notification.SlotType.SOCIAL_COMMUNICATION;
Notification.getSlot(slotType, getSlotCallback);

Notification.getSlot

getSlot(slotType: SlotType): Promise<NotificationSlot>

Obtains a notification slot of a specified type. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
slotType SlotType Yes Type of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.

Return value

Type Description
Promise<NotificationSlot> Promise used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

let slotType = Notification.SlotType.SOCIAL_COMMUNICATION;
Notification.getSlot(slotType).then((data) => {
	console.info("getSlot success, data: " + JSON.stringify(data));
});

Notification.getSlots

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

Obtains all notification slots of this application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
callback AsyncCallback<Array<NotificationSlot>> Yes Callback used to return all notification slots of the current application.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// getSlots callback
function getSlotsCallback(err,data) {
    if (err) {
        console.info("getSlots failed " + JSON.stringify(err));
    } else {
        console.info("getSlots success");
    }
}
Notification.getSlots(getSlotsCallback);

Notification.getSlots

getSlots(): Promise<Array<NotificationSlot>>

Obtains all notification slots of this application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Return value

Type Description
Promise<Array<NotificationSlot>> Promise used to return all notification slots of the current application.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.getSlots().then((data) => {
	console.info("getSlots success, data: " + JSON.stringify(data));
});

Notification.removeSlot

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

Removes a notification slot of a specified type. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
slotType SlotType Yes Type of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

// removeSlot callback
function removeSlotCallback(err) {
    if (err) {
        console.info("removeSlot failed " + JSON.stringify(err));
    } else {
        console.info("removeSlot success");
    }
}
let slotType = Notification.SlotType.SOCIAL_COMMUNICATION;
Notification.removeSlot(slotType,removeSlotCallback);

Notification.removeSlot

removeSlot(slotType: SlotType): Promise<void>

Removes a notification slot of a specified type. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
slotType SlotType Yes Type of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

let slotType = Notification.SlotType.SOCIAL_COMMUNICATION;
Notification.removeSlot(slotType).then(() => {
	console.info("removeSlot success");
});

Notification.removeAllSlots

removeAllSlots(callback: AsyncCallback<void>): void

Removes all notification slots. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function removeAllCallBack(err) {
    if (err) {
        console.info("removeAllSlots failed " + JSON.stringify(err));
    } else {
        console.info("removeAllSlots success");
    }
}
Notification.removeAllSlots(removeAllCallBack);

Notification.removeAllSlots

removeAllSlots(): Promise<void>

Removes all notification slots. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.removeAllSlots().then(() => {
	console.info("removeAllSlots success");
});

Notification.setNotificationEnable

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

Sets whether to enable notification for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
enable boolean Yes Whether to enable notification.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function setNotificationEnablenCallback(err) {
    if (err) {
        console.info("setNotificationEnablenCallback failed " + JSON.stringify(err));
    } else {
        console.info("setNotificationEnablenCallback success");
    }
}
let bundle = {
    bundle: "bundleName1",
};
Notification.setNotificationEnable(bundle, false, setNotificationEnablenCallback);

Notification.setNotificationEnable

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

Sets whether to enable notification for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
enable boolean Yes Whether to enable notification.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};
Notification.setNotificationEnable(bundle, false).then(() => {
	console.info("setNotificationEnable success");
});

Notification.isNotificationEnabled

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

Checks whether notification is enabled for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function isNotificationEnabledCallback(err, data) {
    if (err) {
        console.info("isNotificationEnabled failed " + JSON.stringify(err));
    } else {
        console.info("isNotificationEnabled success");
    }
}
let bundle = {
    bundle: "bundleName1",
};
Notification.isNotificationEnabled(bundle, isNotificationEnabledCallback);

Notification.isNotificationEnabled

isNotificationEnabled(bundle: BundleOption): Promise<boolean>

Checks whether notification is enabled for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};
Notification.isNotificationEnabled(bundle).then((data) => {
	console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
});

Notification.isNotificationEnabled

isNotificationEnabled(callback: AsyncCallback<boolean>): void

Checks whether notification is enabled for this application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function isNotificationEnabledCallback(err, data) {
    if (err) {
        console.info("isNotificationEnabled failed " + JSON.stringify(err));
    } else {
        console.info("isNotificationEnabled success");
    }
}

Notification.isNotificationEnabled(isNotificationEnabledCallback);

Notification.isNotificationEnabled

isNotificationEnabled(): Promise<boolean>

Checks whether notification is enabled for the current application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

Notification.isNotificationEnabled().then((data) => {
	console.info("isNotificationEnabled success, data: " + JSON.stringify(data));
});

Notification.displayBadge

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

Sets whether to enable the notification badge for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
enable boolean Yes Whether to enable notification.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function displayBadgeCallback(err) {
    if (err) {
        console.info("displayBadge failed " + JSON.stringify(err));
    } else {
        console.info("displayBadge success");
    }
}
let bundle = {
    bundle: "bundleName1",
};
Notification.displayBadge(bundle, false, displayBadgeCallback);

Notification.displayBadge

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

Sets whether to enable the notification badge for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
enable boolean Yes Whether to enable notification.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};
Notification.displayBadge(bundle, false).then(() => {
	console.info("displayBadge success");
});

Notification.isBadgeDisplayed

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

Checks whether the notification badge is enabled for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function isBadgeDisplayedCallback(err, data) {
    if (err) {
        console.info("isBadgeDisplayed failed " + JSON.stringify(err));
    } else {
        console.info("isBadgeDisplayed success");
    }
}
let bundle = {
    bundle: "bundleName1",
};
Notification.isBadgeDisplayed(bundle, isBadgeDisplayedCallback);

Notification.isBadgeDisplayed

isBadgeDisplayed(bundle: BundleOption): Promise<boolean>

Checks whether the notification badge is enabled for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};
Notification.isBadgeDisplayed(bundle).then((data) => {
	console.info("isBadgeDisplayed success, data: " + JSON.stringify(data));
});

notificationManager.setSlotByBundle

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

Sets the notification slot for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
slot NotificationSlot Yes Notification slot.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function setSlotByBundleCallback(err) {
    if (err) {
        console.info("setSlotByBundle failed " + JSON.stringify(err));
    } else {
        console.info("setSlotByBundle success");
    }
}
let bundle = {
    bundle: "bundleName1",
};
let notificationSlot = {
    type: Notification.SlotType.SOCIAL_COMMUNICATION
};
Notification.setSlotByBundle(bundle, notificationSlot, setSlotByBundleCallback);

Notification.setSlotByBundle

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

Sets the notification slot for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
slot NotificationSlot Yes Notification slot.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};
let notificationSlot = {
    type: Notification.SlotType.SOCIAL_COMMUNICATION
};
Notification.setSlotByBundle(bundle, notificationSlot).then(() => {
	console.info("setSlotByBundle success");
});

Notification.getSlotsByBundle

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

Obtains the notification slots of a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
callback AsyncCallback<Array<NotificationSlot>> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function getSlotsByBundleCallback(err, data) {
    if (err) {
        console.info("getSlotsByBundle failed " + JSON.stringify(err));
    } else {
        console.info("getSlotsByBundle success");
    }
}
let bundle = {
    bundle: "bundleName1",
};
Notification.getSlotsByBundle(bundle, getSlotsByBundleCallback);

Notification.getSlotsByBundle

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

Obtains the notification slots of a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.

Return value

Type Description
Promise<Array<NotificationSlot>> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};
Notification.getSlotsByBundle(bundle).then((data) => {
	console.info("getSlotsByBundle success, data: " + JSON.stringify(data));
});

Notification.getSlotNumByBundle

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

Obtains the number of notification slots of a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.
callback AsyncCallback<number> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function getSlotNumByBundleCallback(err, data) {
    if (err) {
        console.info("getSlotNumByBundle failed " + JSON.stringify(err));
    } else {
        console.info("getSlotNumByBundle success");
    }
}
let bundle = {
    bundle: "bundleName1",
};
Notification.getSlotNumByBundle(bundle, getSlotNumByBundleCallback);

Notification.getSlotNumByBundle

getSlotNumByBundle(bundle: BundleOption): Promise<number>

Obtains the number of notification slots of a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle of the application.

Return value

Type Description
Promise<number> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};
Notification.getSlotNumByBundle(bundle).then((data) => {
	console.info("getSlotNumByBundle success, data: " + JSON.stringify(data));
});

Notification.getAllActiveNotifications

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

Obtains all active notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
callback AsyncCallback<Array<NotificationRequest>> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function getAllActiveNotificationsCallback(err, data) {
    if (err) {
        console.info("getAllActiveNotifications failed " + JSON.stringify(err));
    } else {
        console.info("getAllActiveNotifications success");
    }
}

Notification.getAllActiveNotifications(getAllActiveNotificationsCallback);

Notification.getAllActiveNotifications

getAllActiveNotifications(): Promise<Array<NotificationRequest>>

Obtains all active notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Return value

Type Description
Promise<Array<NotificationRequest>> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.getAllActiveNotifications().then((data) => {
	console.info("getAllActiveNotifications success, data: " + JSON.stringify(data));
});

Notification.getActiveNotificationCount

getActiveNotificationCount(callback: AsyncCallback<number>): void

Obtains the number of active notifications of this application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
callback AsyncCallback<number> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function getActiveNotificationCountCallback(err, data) {
    if (err) {
        console.info("getActiveNotificationCount failed " + JSON.stringify(err));
    } else {
        console.info("getActiveNotificationCount success");
    }
}

Notification.getActiveNotificationCount(getActiveNotificationCountCallback);

Notification.getActiveNotificationCount

getActiveNotificationCount(): Promise<number>

Obtains the number of active notifications of this application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Return value

Type Description
Promise<number> Promise used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.getActiveNotificationCount().then((data) => {
	console.info("getActiveNotificationCount success, data: " + JSON.stringify(data));
});

Notification.getActiveNotifications

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

Obtains active notifications of this application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
callback AsyncCallback<Array<NotificationRequest>> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function getActiveNotificationsCallback(err, data) {
    if (err) {
        console.info("getActiveNotifications failed " + JSON.stringify(err));
    } else {
        console.info("getActiveNotifications success");
    }
}

Notification.getActiveNotifications(getActiveNotificationsCallback);

Notification.getActiveNotifications

getActiveNotifications(): Promise<Array<NotificationRequest>>

Obtains active notifications of this application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Return value

Type Description
Promise<Array<NotificationRequest>> Promise used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.getActiveNotifications().then((data) => {
	console.info("removeGroupByBundle success, data: " + JSON.stringify(data));
});

Notification.cancelGroup

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

Cancels notifications under a notification group of this application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
groupName string Yes Name of the notification group, which is specified through NotificationRequest when the notification is published.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function cancelGroupCallback(err) {
    if (err) {
        console.info("cancelGroup failed " + JSON.stringify(err));
    } else {
        console.info("cancelGroup success");
    }
}

var groupName = "GroupName";

Notification.cancelGroup(groupName, cancelGroupCallback);

Notification.cancelGroup

cancelGroup(groupName: string): Promise<void>

Cancels notifications under a notification group of this application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
groupName string Yes Name of the notification group.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

let groupName = "GroupName";
Notification.cancelGroup(groupName).then(() => {
	console.info("cancelGroup success");
});

Notification.removeGroupByBundle

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

Removes notifications under a notification group of a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
groupName string Yes Name of the notification group.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

function removeGroupByBundleCallback(err) {
    if (err) {
        console.info("removeGroupByBundle failed " + JSON.stringify(err));
    } else {
        console.info("removeGroupByBundle success");
    }
}

let bundleOption = {bundle: "Bundle"};
let groupName = "GroupName";

Notification.removeGroupByBundle(bundleOption, groupName, removeGroupByBundleCallback);

Notification.removeGroupByBundle

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

Removes notifications under a notification group of a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
groupName string Yes Name of the notification group.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

let bundleOption = {bundle: "Bundle"};
let groupName = "GroupName";
Notification.removeGroupByBundle(bundleOption, groupName).then(() => {
	console.info("removeGroupByBundle success");
});

Notification.setDoNotDisturbDate

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

Sets the DND time. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
date DoNotDisturbDate Yes DND time to set.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function setDoNotDisturbDateCallback(err) {
    if (err) {
        console.info("setDoNotDisturbDate failed " + JSON.stringify(err));
    } else {
        console.info("setDoNotDisturbDate success");
    }
}

let doNotDisturbDate = {
    type: Notification.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};

Notification.setDoNotDisturbDate(doNotDisturbDate, setDoNotDisturbDateCallback);

Notification.setDoNotDisturbDate

setDoNotDisturbDate(date: DoNotDisturbDate): Promise<void>

Sets the DND time. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
date DoNotDisturbDate Yes DND time to set.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

let doNotDisturbDate = {
    type: Notification.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};
Notification.setDoNotDisturbDate(doNotDisturbDate).then(() => {
	console.info("setDoNotDisturbDate success");
});

Notification.setDoNotDisturbDate

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

Sets the DND time for a specified user. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
date DoNotDisturbDate Yes DND time to set.
userId number Yes ID of the user for whom you want to set the DND time.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

function setDoNotDisturbDateCallback(err) {
    if (err) {
        console.info("setDoNotDisturbDate failed " + JSON.stringify(err));
    } else {
        console.info("setDoNotDisturbDate success");
    }
}

let doNotDisturbDate = {
    type: Notification.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};

let userId = 1;

Notification.setDoNotDisturbDate(doNotDisturbDate, userId, setDoNotDisturbDateCallback);

Notification.setDoNotDisturbDate

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

Sets the DND time for a specified user. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
date DoNotDisturbDate Yes DND time to set.
userId number Yes ID of the user for whom you want to set the DND time.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

let doNotDisturbDate = {
    type: Notification.DoNotDisturbType.TYPE_ONCE,
    begin: new Date(),
    end: new Date(2021, 11, 15, 18, 0)
};

let userId = 1;

Notification.setDoNotDisturbDate(doNotDisturbDate, userId).then(() => {
	console.info("setDoNotDisturbDate success");
});

Notification.getDoNotDisturbDate

getDoNotDisturbDate(callback: AsyncCallback<DoNotDisturbDate>): void

Obtains the DND time. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
callback AsyncCallback<DoNotDisturbDate> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function getDoNotDisturbDateCallback(err,data) {
    if (err) {
        console.info("getDoNotDisturbDate failed " + JSON.stringify(err));
    } else {
        console.info("getDoNotDisturbDate success");
    }
}

Notification.getDoNotDisturbDate(getDoNotDisturbDateCallback);

Notification.getDoNotDisturbDate

getDoNotDisturbDate(): Promise<DoNotDisturbDate>

Obtains the DND time. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Return value

Type Description
Promise<DoNotDisturbDate> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.getDoNotDisturbDate().then((data) => {
	console.info("getDoNotDisturbDate success, data: " + JSON.stringify(data));
});

Notification.getDoNotDisturbDate

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

Obtains the DND time of a specified user. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
callback AsyncCallback<DoNotDisturbDate> Yes Callback used to return the result.
userId number Yes User ID.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

function getDoNotDisturbDateCallback(err,data) {
    if (err) {
        console.info("getDoNotDisturbDate failed " + JSON.stringify(err));
    } else {
        console.info("getDoNotDisturbDate success");
    }
}

let userId = 1;

Notification.getDoNotDisturbDate(userId, getDoNotDisturbDateCallback);

Notification.getDoNotDisturbDate

getDoNotDisturbDate(userId: number): Promise<DoNotDisturbDate>

Obtains the DND time of a specified user. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
userId number Yes User ID.

Return value

Type Description
Promise<DoNotDisturbDate> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

let userId = 1;

Notification.getDoNotDisturbDate(userId).then((data) => {
	console.info("getDoNotDisturbDate success, data: " + JSON.stringify(data));
});

Notification.isSupportDoNotDisturbMode

isSupportDoNotDisturbMode(callback: AsyncCallback<boolean>): void

Checks whether DND mode is supported. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
callback AsyncCallback<boolean> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function isSupportDoNotDisturbModeCallback(err,data) {
    if (err) {
        console.info("isSupportDoNotDisturbMode failed " + JSON.stringify(err));
    } else {
        console.info("isSupportDoNotDisturbMode success");
    }
}

Notification.isSupportDoNotDisturbMode(supportDoNotDisturbModeCallback);

Notification.isSupportDoNotDisturbMode

isSupportDoNotDisturbMode(): Promise<boolean>

Checks whether DND mode is supported. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.isSupportDoNotDisturbMode().then((data) => {
	console.info("isSupportDoNotDisturbMode success, data: " + JSON.stringify(data));
});

Notification.isSupportTemplate

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

Checks whether a specified template is supported. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
templateName string Yes Template name.
callback AsyncCallback<boolean> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600011 Read template config failed.

Example

let templateName = 'process';
function isSupportTemplateCallback(err, data) {
    if (err) {
        console.info("isSupportTemplate failed " + JSON.stringify(err));
    } else {
        console.info("isSupportTemplate success");
    }
}

Notification.isSupportTemplate(templateName, isSupportTemplateCallback);

Notification.isSupportTemplate

isSupportTemplate(templateName: string): Promise<boolean>

Checks whether a specified template is supported. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
templateName string Yes Template name.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600011 Read template config failed.

Example

let templateName = 'process';

Notification.isSupportTemplate(templateName).then((data) => {
    console.info("isSupportTemplate success, data: " + JSON.stringify(data));
});

Notification.requestEnableNotification

requestEnableNotification(callback: AsyncCallback<void>): void

Requests notification to be enabled for this application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function requestEnableNotificationCallback(err) {
    if (err) {
        console.info("requestEnableNotification failed " + JSON.stringify(err));
    } else {
        console.info("requestEnableNotification success");
    }
};

Notification.requestEnableNotification(requestEnableNotificationCallback);

Notification.requestEnableNotification

requestEnableNotification(): Promise<void>

Requests notification to be enabled for this application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.requestEnableNotification().then(() => {
    console.info("requestEnableNotification success");
});

Notification.setDistributedEnable

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

Sets whether this device supports distributed notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
enable boolean Yes Whether the device supports distributed notifications.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.

Example

function setDistributedEnableCallback() {
    if (err) {
        console.info("setDistributedEnable failed " + JSON.stringify(err));
    } else {
        console.info("setDistributedEnable success");
    }
};

let enable = true;

Notification.setDistributedEnable(enable, setDistributedEnableCallback);

Notification.setDistributedEnable

setDistributedEnable(enable: boolean): Promise<void>

Sets whether this device supports distributed notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
enable boolean Yes Whether the device supports distributed notifications.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.

Example

let enable = true;

Notification.setDistributedEnable(enable).then(() => {
        console.info("setDistributedEnable success");
    });

Notification.isDistributedEnabled

isDistributedEnabled(callback: AsyncCallback<boolean>): void

Checks whether this device supports distributed notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
callback AsyncCallback<boolean> Yes Callback used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.

Example

function isDistributedEnabledCallback(err, data) {
    if (err) {
        console.info("isDistributedEnabled failed " + JSON.stringify(err));
    } else {
        console.info("isDistributedEnabled success " + JSON.stringify(data));
    }
};

Notification.isDistributedEnabled(isDistributedEnabledCallback);

Notification.isDistributedEnabled

isDistributedEnabled(): Promise<boolean>

Checks whether this device supports distributed notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.

Example

Notification.isDistributedEnabled()
    .then((data) => {
        console.info("isDistributedEnabled success, data: " + JSON.stringify(data));
    });

Notification.setDistributedEnableByBundle

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

Sets whether a specified application supports distributed notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
enable boolean Yes Whether the application supports distributed notifications.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.
17700001 The specified bundle name was not found.

Example

function setDistributedEnableByBundleCallback(err) {
    if (err) {
        console.info("enableDistributedByBundle failed " + JSON.stringify(err));
    } else {
        console.info("enableDistributedByBundle success");
    }
};

let bundle = {
    bundle: "bundleName1",
};

let enable = true

Notification.setDistributedEnableByBundle(bundle, enable, setDistributedEnableByBundleCallback);

Notification.setDistributedEnableByBundle

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

Sets whether a specified application supports distributed notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
enable boolean Yes Whether the application supports distributed notifications.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};

let enable = true

Notification.setDistributedEnableByBundle(bundle, enable).then(() => {
        console.info("setDistributedEnableByBundle success");
    });

Notification.isDistributedEnabledByBundle

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

Checks whether a specified application supports distributed notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
callback AsyncCallback<boolean> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.
17700001 The specified bundle name was not found.

Example

function isDistributedEnabledByBundleCallback(err, data) {
    if (err) {
        console.info("isDistributedEnabledByBundle failed " + JSON.stringify(err));
    } else {
        console.info("isDistributedEnabledByBundle success" + JSON.stringify(data));
    }
};

let bundle = {
    bundle: "bundleName1",
};

Notification.isDistributedEnabledByBundle(bundle, isDistributedEnabledByBundleCallback);

Notification.isDistributedEnabledByBundle

isDistributedEnabledByBundle(bundle: BundleOption): Promise<boolean>

Checks whether a specified application supports distributed notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600010 Distributed operation failed.
17700001 The specified bundle name was not found.

Example

let bundle = {
    bundle: "bundleName1",
};

Notification.isDistributedEnabledByBundle(bundle).then((data) => {
    console.info("isDistributedEnabledByBundle success, data: " + JSON.stringify(data));
});

Notification.getDeviceRemindType

getDeviceRemindType(callback: AsyncCallback<DeviceRemindType>): void

Obtains the notification reminder type. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
callback AsyncCallback<DeviceRemindType> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

function getDeviceRemindTypeCallback(err, data) {
    if (err) {
        console.info("getDeviceRemindType failed " + JSON.stringify(err));
    } else {
        console.info("getDeviceRemindType success");
    }
};

Notification.getDeviceRemindType(getDeviceRemindTypeCallback);

Notification.getDeviceRemindType

getDeviceRemindType(): Promise<DeviceRemindType>

Obtains the notification reminder type. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Return value

Type Description
Promise<DeviceRemindType> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.

Example

Notification.getDeviceRemindType().then((data) => {
    console.info("getDeviceRemindType success, data: " + JSON.stringify(data));
});

Notification.publishAsBundle

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

Publishes a notification through the reminder agent. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER, ohos.permission.NOTIFICATION_AGENT_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
request NotificationRequest Yes Content and related configuration of the notification to publish.
representativeBundle string Yes Bundle name of the application whose notification function is taken over by the reminder agent.
userId number Yes User ID.
callback AsyncCallback Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600004 Notification is not enabled.
1600005 Notification slot is not enabled.
1600008 The user is not exist.
1600009 Over max number notifications per second.

Example

// publishAsBundle callback
function callback(err) {
    if (err) {
        console.info("publishAsBundle failed " + JSON.stringify(err));
    } else {
        console.info("publishAsBundle success");
    }
}
// Bundle name of the application whose notification function is taken over by the reminder agent
let representativeBundle = "com.example.demo";
// User ID
let userId = 100;
// NotificationRequest object
let request = {
    id: 1,
    content: {
        contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};

Notification.publishAsBundle(request, representativeBundle, userId, callback);

Notification.publishAsBundle

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

Publishes a notification through the reminder agent. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER, ohos.permission.NOTIFICATION_AGENT_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
request NotificationRequest Yes Content and related configuration of the notification to publish.
representativeBundle string Yes Bundle name of the application whose notification function is taken over by the reminder agent.
userId number Yes User ID.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600004 Notification is not enabled.
1600005 Notification slot is not enabled.
1600008 The user is not exist.
1600009 Over max number notifications per second.

Example

// Bundle name of the application whose notification function is taken over by the reminder agent
let representativeBundle = "com.example.demo";
// User ID
let userId = 100;
// NotificationRequest object
let request = {
    id: 1,
    content: {
        contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
        normal: {
            title: "test_title",
            text: "test_text",
            additionalText: "test_additionalText"
        }
    }
};

Notification.publishAsBundle(request, representativeBundle, userId).then(() => {
	console.info("publishAsBundle success");
});

Notification.cancelAsBundle

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

Cancels a notification published by the reminder agent. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER, ohos.permission.NOTIFICATION_AGENT_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
id number Yes Notification ID.
representativeBundle string Yes Bundle name of the application whose notification function is taken over by the reminder agent.
userId number Yes User ID.
callback AsyncCallback Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600007 The notification is not exist.
1600008 The user is not exist.

Example

// cancelAsBundle
function cancelAsBundleCallback(err) {
    if (err) {
        console.info("cancelAsBundle failed " + JSON.stringify(err));
    } else {
        console.info("cancelAsBundle success");
    }
}
// Bundle name of the application whose notification function is taken over by the reminder agent
let representativeBundle = "com.example.demo";
// User ID
let userId = 100;

Notification.cancelAsBundle(0, representativeBundle, userId, cancelAsBundleCallback);

Notification.cancelAsBundle

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

Cancels a notification published by the reminder agent. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER, ohos.permission.NOTIFICATION_AGENT_CONTROLLER

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
id number Yes Notification ID.
representativeBundle string Yes Bundle name of the application whose notification function is taken over by the reminder agent.
userId number Yes User ID.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600007 The notification is not exist.
1600008 The user is not exist.

Example

// Bundle name of the application whose notification function is taken over by the reminder agent
let representativeBundle = "com.example.demo";
// User ID
let userId = 100;

Notification.cancelAsBundle(0, representativeBundle, userId).then(() => {
	console.info("cancelAsBundle success");
});

Notification.setNotificationEnableSlot

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

Sets whether to enable a specified notification slot type for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
type SlotType Yes Notification slot type.
enable boolean Yes Whether to enable the notification slot type.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

// setNotificationEnableSlot
function setNotificationEnableSlotCallback(err) {
    if (err) {
        console.info("setNotificationEnableSlot failed " + JSON.stringify(err));
    } else {
        console.info("setNotificationEnableSlot success");
    }
};

Notification.setNotificationEnableSlot(
    { bundle: "ohos.samples.notification", },
    Notification.SlotType.SOCIAL_COMMUNICATION,
    true,
    setNotificationEnableSlotCallback);

Notification.setNotificationEnableSlot

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

Sets whether to enable a specified notification slot type for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
type SlotType Yes Notification slot type.
enable boolean Yes Whether to enable the notification slot type.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

// setNotificationEnableSlot
Notification.setNotificationEnableSlot(
    { bundle: "ohos.samples.notification", },
    Notification.SlotType.SOCIAL_COMMUNICATION,
    true).then(() => {
        console.info("setNotificationEnableSlot success");
    });

Notification.isNotificationSlotEnabled

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

Checks whether a specified notification slot type is enabled for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
type SlotType Yes Notification slot type.
callback AsyncCallback<boolean> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

// isNotificationSlotEnabled
function getEnableSlotCallback(err, data) {
    if (err) {
        console.info("isNotificationSlotEnabled failed " + JSON.stringify(err));
    } else {
        console.info("isNotificationSlotEnabled success");
    }
};

Notification.isNotificationSlotEnabled(
    { bundle: "ohos.samples.notification", },
    Notification.SlotType.SOCIAL_COMMUNICATION,
    getEnableSlotCallback);

Notification.isNotificationSlotEnabled

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

Checks whether a specified notification slot type is enabled for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
bundle BundleOption Yes Bundle information of the application.
type SlotType Yes Notification slot type.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
17700001 The specified bundle name was not found.

Example

// isNotificationSlotEnabled
Notification.isNotificationSlotEnabled({ bundle: "ohos.samples.notification", },
    Notification.SlotType.SOCIAL_COMMUNICATION).then((data) => {
    console.info("isNotificationSlotEnabled success, data: " + JSON.stringify(data));
});

Notification.setSyncNotificationEnabledWithoutApp

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

Sets whether to enable the notification sync feature for devices where the application is not installed. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
userId number Yes User ID.
enable boolean Yes Whether the feature is enabled.
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

let userId = 100;
let enable = true;

function callback(err) {
    if (err) {
        console.info("setSyncNotificationEnabledWithoutApp failed " + JSON.stringify(err));
    } else {
        console.info("setSyncNotificationEnabledWithoutApp success");
    }
}

Notification.setSyncNotificationEnabledWithoutApp(userId, enable, callback);

Notification.setSyncNotificationEnabledWithoutApp

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

Sets whether to enable the notification sync feature for devices where the application is not installed. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
userId number Yes User ID.
enable boolean Yes Whether the feature is enabled.

Return value

Type Description
Promise<void> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

let userId = 100;
let enable = true;

Notification.setSyncNotificationEnabledWithoutApp(userId, enable).then(() => {
    console.info('setSyncNotificationEnabledWithoutApp success');
}).catch((err) => {
    console.info('setSyncNotificationEnabledWithoutApp, err:' + JSON.stringify(err));
});

Notification.getSyncNotificationEnabledWithoutApp

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

Obtains whether the notification sync feature is enabled for devices where the application is not installed. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
userId number Yes User ID.
callback AsyncCallback<boolean> Yes Callback used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

let userId = 100;

function getSyncNotificationEnabledWithoutAppCallback(err, data) {
    if (err) {
        console.info('getSyncNotificationEnabledWithoutAppCallback, err:' + err);
    } else {
        console.info('getSyncNotificationEnabledWithoutAppCallback, data:' + data);
    }
}

Notification.getSyncNotificationEnabledWithoutApp(userId, getSyncNotificationEnabledWithoutAppCallback);

Notification.getSyncNotificationEnabledWithoutApp

getSyncNotificationEnabledWithoutApp(userId: number): Promise<boolean>

Obtains whether the notification sync feature is enabled for devices where the application is not installed. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

Name Type Mandatory Description
userId number Yes User ID.

Return value

Type Description
Promise<boolean> Promise used to return the result.

Error codes

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

ID Error Message
201 Permission denied.
202 Not system application to call the interface.
401 The parameter check failed.
1600001 Internal error.
1600002 Marshalling or unmarshalling error.
1600003 Failed to connect service.
1600008 The user is not exist.

Example

let userId = 100;
Notification.getSyncNotificationEnabledWithoutApp(userId).then((data) => {
    console.info('getSyncNotificationEnabledWithoutApp, data:' + data);
}).catch((err) => {
    console.info('getSyncNotificationEnabledWithoutApp, err:' + err);
});
    .catch((err) => {
        console.info('getSyncNotificationEnabledWithoutApp, err:', err);
    });

DoNotDisturbDate

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Name Type Readable Writable Description
type DoNotDisturbType Yes Yes DND time type.
begin Date Yes Yes DND start time.
end Date Yes Yes DND end time.

DoNotDisturbType

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Name Value Description
TYPE_NONE 0 Non-DND.
TYPE_ONCE 1 One-shot DND at the specified time segment (only considering the hour and minute).
TYPE_DAILY 2 Daily DND at the specified time segment (only considering the hour and minute).
TYPE_CLEARLY 3 DND at the specified time segment (considering the year, month, day, hour, and minute).

ContentType

System capability: SystemCapability.Notification.Notification

Name Value Description
NOTIFICATION_CONTENT_BASIC_TEXT NOTIFICATION_CONTENT_BASIC_TEXT Normal text notification.
NOTIFICATION_CONTENT_LONG_TEXT NOTIFICATION_CONTENT_LONG_TEXT Long text notification.
NOTIFICATION_CONTENT_PICTURE NOTIFICATION_CONTENT_PICTURE Picture-attached notification.
NOTIFICATION_CONTENT_CONVERSATION NOTIFICATION_CONTENT_CONVERSATION Conversation notification.
NOTIFICATION_CONTENT_MULTILINE NOTIFICATION_CONTENT_MULTILINE Multi-line text notification.

SlotLevel

System capability: SystemCapability.Notification.Notification

Name Value Description
LEVEL_NONE 0 Notification is disabled.
LEVEL_MIN 1 Notification is enabled, but the notification icon is not displayed in the status bar, with no banner or alert tone.
LEVEL_LOW 2 Notification is enabled, and the notification icon is displayed in the status bar, with no banner or alert tone.
LEVEL_DEFAULT 3 Notification is enabled, and the notification icon is displayed in the status bar, with an alert tone but no banner.
LEVEL_HIGH 4 Notification is enabled, and the notification icon is displayed in the status bar, with an alert tone and banner.

SlotType

System capability: SystemCapability.Notification.Notification

Name Value Description
UNKNOWN_TYPE 0 Unknown type.
SOCIAL_COMMUNICATION 1 Notification slot for social communication.
SERVICE_INFORMATION 2 Notification slot for service information.
CONTENT_INFORMATION 3 Notification slot for content consultation.
OTHER_TYPES 0xFFFF Notification slot for other purposes.

DeviceRemindType

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Name Value Description
IDLE_DONOT_REMIND 0 The device is not in use. No notification is required.
IDLE_REMIND 1 The device is not in use.
ACTIVE_DONOT_REMIND 2 The device is in use. No notification is required.
ACTIVE_REMIND 3 The device is in use.

SourceType

System capability: SystemCapability.Notification.Notification

System API: This is a system API and cannot be called by third-party applications.

Name Value Description
TYPE_NORMAL 0 Normal notification.
TYPE_CONTINUOUS 1 Continuous notification.
TYPE_TIMER 2 Timed notification.