harmony 鸿蒙@ohos.notification (Notification)

2022-08-09 浏览 (761)

@ohos.notification (Notification)

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

NOTE

The APIs provided by this module are no longer maintained since API version 9. You are advised to use @ohos.notificationManager.

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

Notification subscription and unsubscription APIs are available only to system applications.

Modules to Import

import Notification from '@ohos.notification';

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

NameTypeMandatoryDescription
requestNotificationRequestYesContent and related configuration of the notification to publish.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

// publish callback
let publishCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`publish failed, code is ${err}`);
  } else {
    console.info("publish success");
  }
}
// NotificationRequest object
let notificationRequest: NotificationManager.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

NameTypeMandatoryDescription
requestNotificationRequestYesContent and related configuration of the notification to publish.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

// NotificationRequest object
let notificationRequest: NotificationManager.NotificationRequest = {
  id: 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");
}).catch((err: Base.BusinessError) => {
  console.error(`publish failed, code is ${err}`);
});

Notification.publish8+

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

NameTypeMandatoryDescription
requestNotificationRequestYesContent and related configuration of the notification to publish.
userIdnumberYesUser ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

// publish callback
let publishCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`publish failed, code is ${err.code}`);
  } else {
    console.info("publish success");
  }
}
// User ID
let userId: number = 1;
// NotificationRequest object
let notificationRequest: NotificationManager.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.publish8+

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

NameTypeMandatoryDescription
requestNotificationRequestYesContent and related configuration of the notification to publish.
userIdnumberYesUser ID.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

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

let userId: number = 1;

Notification.publish(notificationRequest, userId).then(() => {
  console.info("publish success");
}).catch((err: Base.BusinessError) => {
  console.error(`publish failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
idnumberYesNotification ID.
labelstringYesNotification label.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

// cancel callback
let cancelCallback = (err: Base.BusinessError) => {
  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

NameTypeMandatoryDescription
idnumberYesNotification ID.
labelstringNoNotification label. This parameter is left empty by default.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

Notification.cancel(0).then(() => {
	console.info("cancel success");
}).catch((err: Base.BusinessError) => {
  console.error(`cancel failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
idnumberYesNotification ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

// cancel callback
let cancelCallback = (err: Base.BusinessError) => {
  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

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

// cancel callback
let cancelAllCallback = (err: Base.BusinessError) => {
  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

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

Notification.cancelAll().then(() => {
	console.info("cancelAll success");
}).catch((err: Base.BusinessError) => {
  console.error(`cancelAll failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
slotNotificationSlotYesNotification slot to add.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

// addSlot callback
let addSlotCallBack = (err: Base.BusinessError) => {
  if (err) {
    console.info("addSlot failed " + JSON.stringify(err));
  } else {
    console.info("addSlot success");
  }
}
// NotificationSlot object
let notificationSlot: NotificationManager.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

NameTypeMandatoryDescription
slotNotificationSlotYesNotification slot to add.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

// NotificationSlot object
let notificationSlot: NotificationManager.NotificationSlot = {
    type: Notification.SlotType.SOCIAL_COMMUNICATION
};
Notification.addSlot(notificationSlot).then(() => {
	console.info("addSlot success");
}).catch((err: Base.BusinessError) => {
  console.error(`addSlot failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
typeSlotTypeYesType of the notification slot to add.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

// addSlot callback
let addSlotCallBack = (err: Base.BusinessError) => {
  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

NameTypeMandatoryDescription
typeSlotTypeYesType of the notification slot to add.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

Notification.addSlot(Notification.SlotType.SOCIAL_COMMUNICATION).then(() => {
  console.info("addSlot success");
}).catch((err: Base.BusinessError) => {
  console.error(`addSlot failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
slotsArray<NotificationSlot>YesNotification slots to add.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

// addSlots callback
let addSlotsCallBack = (err: Base.BusinessError) => {
  if (err) {
    console.info("addSlots failed " + JSON.stringify(err));
  } else {
    console.info("addSlots success");
  }
}
// NotificationSlot object
let notificationSlot: NotificationManager.NotificationSlot = {
  type: Notification.SlotType.SOCIAL_COMMUNICATION
};
// NotificationSlotArray object
let notificationSlotArray: NotificationManager.NotificationSlot[] = 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

NameTypeMandatoryDescription
slotsArray<NotificationSlot>YesNotification slots to add.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import NotificationManager from '@ohos.notificationManager';
import Base from '@ohos.base';

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

Notification.addSlots(notificationSlotArray).then(() => {
  console.info("addSlots success");
}).catch((err: Base.BusinessError) => {
  console.error(`addSlot failed, code is ${err}`);
});

Notification.getSlot

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

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

System capability: SystemCapability.Notification.Notification

Parameters

NameTypeMandatoryDescription
slotTypeSlotTypeYesType of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.
callbackAsyncCallback<NotificationSlot>YesCallback used to return the result.

Example

import Base from '@ohos.base';

// getSlot callback
let getSlotCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("getSlot failed " + JSON.stringify(err));
  } else {
    console.info("getSlot success");
  }
}
let slotType: Notification.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

NameTypeMandatoryDescription
slotTypeSlotTypeYesType of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.

Return value

TypeDescription
Promise<NotificationSlot>Promise used to return the result.

Example

import Base from '@ohos.base';

let slotType: Notification.SlotType = Notification.SlotType.SOCIAL_COMMUNICATION;
Notification.getSlot(slotType).then((data) => {
  console.info("getSlot success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
  console.error(`getSlot failed, code is ${err}`);
});

Notification.getSlots

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

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

System capability: SystemCapability.Notification.Notification

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<Array<NotificationSlot>>YesCallback used to return the result.

Example

import Base from '@ohos.base';

// getSlots callback
function getSlotsCallback(err: Base.BusinessError) {
  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

TypeDescription
Promise<Array<NotificationSlot>>Promise used to return the result.

Example

import Base from '@ohos.base';

Notification.getSlots().then((data) => {
  console.info("getSlots success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
  console.error(`getSlots failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
slotTypeSlotTypeYesType of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

// removeSlot callback
let removeSlotCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("removeSlot failed " + JSON.stringify(err));
  } else {
    console.info("removeSlot success");
  }
}
let slotType: Notification.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

NameTypeMandatoryDescription
slotTypeSlotTypeYesType of the notification slot, which can be used for social communication, service information, content consultation, and other purposes.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let slotType: Notification.SlotType = Notification.SlotType.SOCIAL_COMMUNICATION;
Notification.removeSlot(slotType).then(() => {
  console.info("removeSlot success");
}).catch((err: Base.BusinessError) => {
  console.error(`removeSlot failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let removeAllCallBack = (err: Base.BusinessError) => {
  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

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

Notification.removeAllSlots().then(() => {
  console.info("removeAllSlots success");
}).catch((err: Base.BusinessError) => {
  console.error(`removeAllSlots failed, code is ${err}`);
});

Notification.subscribe

subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback<void>): void

Subscribes to a notification with the subscription information specified. 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

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
infoNotificationSubscribeInfoYesNotification subscription information.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationSubscribe from '@ohos.notificationSubscribe';

// subscribe callback
let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("subscribe failed " + JSON.stringify(err));
  } else {
    console.info("subscribe success");
  }
}
let onConsumeCallback = (data: NotificationSubscribe.SubscribeCallbackData) => {
  console.info("Consume callback: " + JSON.stringify(data));
}
let subscriber: NotificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
};
let info: NotificationSubscribe.NotificationSubscribeInfo = {
  bundleNames: ["bundleName1", "bundleName2"]
};
Notification.subscribe(subscriber, info, subscribeCallback);

Notification.subscribe

subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback<void>): void

Subscribes to notifications of all applications under this 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

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationSubscribe from '@ohos.notificationSubscribe';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("subscribe failed " + JSON.stringify(err));
  } else {
    console.info("subscribe success");
  }
}
function onConsumeCallback(data: NotificationSubscribe.SubscribeCallbackData) {
  console.info("Consume callback: " + JSON.stringify(data));
}
let subscriber: NotificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
};
Notification.subscribe(subscriber, subscribeCallback);

Notification.subscribe

subscribe(subscriber: NotificationSubscriber, info?: NotificationSubscribeInfo): Promise<void>

Subscribes to a notification with the subscription information specified. 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

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
infoNotificationSubscribeInfoNoNotification subscription information. This parameter is left empty by default.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';
import NotificationSubscribe from '@ohos.notificationSubscribe';

function onConsumeCallback(data: NotificationSubscribe.SubscribeCallbackData) {
  console.info("Consume callback: " + JSON.stringify(data));
}
let subscriber: NotificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
};
Notification.subscribe(subscriber).then(() => {
  console.info("subscribe success");
}).catch((err: Base.BusinessError) => {
  console.error(`subscribe failed, code is ${err}`);
});

Notification.unsubscribe

unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback<void>): void

Unsubscribes from a notification. 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

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationSubscribe from '@ohos.notificationSubscribe';

let unsubscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("unsubscribe failed " + JSON.stringify(err));
  } else {
    console.info("unsubscribe success");
  }
}
let onDisconnectCallback = () => {
  console.info("subscribe disconnect");
}
let subscriber: NotificationSubscribe.NotificationSubscriber = {
  onDisconnect: onDisconnectCallback
};
Notification.unsubscribe(subscriber, unsubscribeCallback);

Notification.unsubscribe

unsubscribe(subscriber: NotificationSubscriber): Promise<void>

Unsubscribes from a notification. 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

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';
import NotificationSubscribe from '@ohos.notificationSubscribe';

function onDisconnectCallback() {
  console.info("subscribe disconnect");
}
let subscriber: NotificationSubscribe.NotificationSubscriber = {
  onDisconnect: onDisconnectCallback
};
Notification.unsubscribe(subscriber).then(() => {
  console.info("unsubscribe success");
}).catch((err: Base.BusinessError) => {
  console.error(`unsubscribe failed, code is ${err}`);
});

Notification.enableNotification

enableNotification(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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
enablebooleanYesWhether to enable notification.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let enableNotificationCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("enableNotification failed " + JSON.stringify(err));
  } else {
    console.info("enableNotification success");
  }
}
let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
Notification.enableNotification(bundle, false, enableNotificationCallback);

Notification.enableNotification

enableNotification(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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
enablebooleanYesWhether to enable notification.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
Notification.enableNotification(bundle, false).then(() => {
  console.info("enableNotification success");
}).catch((err: Base.BusinessError) => {
  console.error(`enableNotification failed, code is ${err}`);
});

Notification.isNotificationEnabled

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

Checks whether notification 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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let isNotificationEnabledCallback = (err: Base.BusinessError, data: boolean) => {
  if (err) {
    console.info("isNotificationEnabled failed " + JSON.stringify(err));
  } else {
    console.info("isNotificationEnabled success");
  }
}
let bundle: Notification.BundleOption = {
  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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Example

import Base from '@ohos.base';

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

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

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let isNotificationEnabledCallback = (err: Base.BusinessError, data: boolean) => {
  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 this 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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Example

import Base from '@ohos.base';

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

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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
enablebooleanYesWhether to enable notification.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let displayBadgeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("displayBadge failed " + JSON.stringify(err));
  } else {
    console.info("displayBadge success");
  }
}
let bundle: Notification.BundleOption = {
  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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
enablebooleanYesWhether to enable notification.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
Notification.displayBadge(bundle, false).then(() => {
  console.info("displayBadge success");
}).catch((err: Base.BusinessError) => {
  console.error(`displayBadge failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let isBadgeDisplayedCallback = (err: Base.BusinessError, data: boolean) => {
  if (err) {
    console.info("isBadgeDisplayed failed " + JSON.stringify(err));
  } else {
    console.info("isBadgeDisplayed success");
  }
}
let bundle: Notification.BundleOption = {
  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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Example

import Base from '@ohos.base';

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

Notification.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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
slotNotificationSlotYesNotification slot.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let setSlotByBundleCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("setSlotByBundle failed " + JSON.stringify(err));
  } else {
    console.info("setSlotByBundle success");
  }
}
let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
let notificationSlot: NotificationManager.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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
slotNotificationSlotYesNotification slot.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
let notificationSlot: NotificationManager.NotificationSlot = {
  type: Notification.SlotType.SOCIAL_COMMUNICATION
};
Notification.setSlotByBundle(bundle, notificationSlot).then(() => {
  console.info("setSlotByBundle success");
}).catch((err: Base.BusinessError) => {
  console.error(`setSlotByBundle failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
callbackAsyncCallback<Array<NotificationSlot>>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let getSlotsByBundleCallback = (err: Base.BusinessError, data: NotificationManager.NotificationSlot[]) => {
  if (err) {
    console.info("getSlotsByBundle failed " + JSON.stringify(err));
  } else {
    console.info("getSlotsByBundle success");
  }
}
let bundle: Notification.BundleOption = {
  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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.

Return value

TypeDescription
Promise<Array<NotificationSlot>>Promise used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
Notification.getSlotsByBundle(bundle).then((data: NotificationManager.NotificationSlot[]) => {
  console.info("getSlotsByBundle success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
  console.error(`getSlotsByBundle failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
callbackAsyncCallback<number>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let getSlotNumByBundleCallback = (err: Base.BusinessError, data: number) => {
  if (err) {
    console.info("getSlotNumByBundle failed " + JSON.stringify(err));
  } else {
    console.info("getSlotNumByBundle success");
  }
}
let bundle: Notification.BundleOption = {
  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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.

Return value

TypeDescription
Promise<number>Promise used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

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

Notification.remove

remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason, callback: AsyncCallback<void>): void

Removes a notification for a specified bundle. 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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
notificationKeyNotificationKeyYesNotification key.
reasonRemoveReasonYesReason for deleting a notification.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("remove failed " + JSON.stringify(err));
  } else {
    console.info("remove success");
  }
}
let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
let notificationKey: Notification.NotificationKey = {
  id: 0,
  label: "label",
};
let reason: Notification.RemoveReason = Notification.RemoveReason.CLICK_REASON_REMOVE;
Notification.remove(bundle, notificationKey, reason, removeCallback);

Notification.remove

remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason): Promise<void>

Removes a notification for a specified bundle. 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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
notificationKeyNotificationKeyYesNotification key.
reasonRemoveReasonYesReason for deleting the notification.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
let notificationKey: Notification.NotificationKey = {
  id: 0,
  label: "label",
};
let reason: Notification.RemoveReason = Notification.RemoveReason.CLICK_REASON_REMOVE;
Notification.remove(bundle, notificationKey, reason).then(() => {
  console.info("remove success");
}).catch((err: Base.BusinessError) => {
  console.error(`remove failed, code is ${err}`);
});

Notification.remove

remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback<void>): void

Removes a notification for a specified bundle. 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

NameTypeMandatoryDescription
hashCodestringYesUnique notification ID. It is the hashCode in the NotificationRequest object of SubscribeCallbackData of the onConsume callback.
reasonRemoveReasonYesReason for deleting a notification.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let hashCode: string = 'hashCode';

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("remove failed " + JSON.stringify(err));
  } else {
    console.info("remove success");
  }
}
let reason: Notification.RemoveReason = Notification.RemoveReason.CANCEL_REASON_REMOVE;
Notification.remove(hashCode, reason, removeCallback);

Notification.remove

remove(hashCode: string, reason: RemoveReason): Promise<void>

Removes a notification for a specified bundle. 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

NameTypeMandatoryDescription
hashCodestringYesUnique notification ID.
reasonRemoveReasonYesReason for deleting the notification.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let hashCode: string = 'hashCode';
let reason: Notification.RemoveReason = Notification.RemoveReason.CLICK_REASON_REMOVE;
Notification.remove(hashCode, reason).then(() => {
  console.info("remove success");
}).catch((err: Base.BusinessError) => {
  console.error(`remove failed, code is ${err}`);
});

Notification.removeAll

removeAll(bundle: BundleOption, callback: AsyncCallback<void>): void

Removes all notifications 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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let removeAllCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("removeAll failed " + JSON.stringify(err));
  } else {
    console.info("removeAll success");
  }
}
let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};
Notification.removeAll(bundle, removeAllCallback);

Notification.removeAll

removeAll(callback: AsyncCallback<void>): void

Removes all 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

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let removeAllCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("removeAll failed " + JSON.stringify(err));
  } else {
    console.info("removeAll success");
  }
}

Notification.removeAll(removeAllCallback);

Notification.removeAll

removeAll(bundle?: BundleOption): Promise<void>

Removes all notifications 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

NameTypeMandatoryDescription
bundleBundleOptionNoBundle information of the application. By default, this parameter is left empty, indicating that all notifications will be removed.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

// If no application is specified, notifications of all applications are deleted.
Notification.removeAll().then(() => {
  console.info("removeAll success");
}).catch((err: Base.BusinessError) => {
  ar
});

Notification.removeAll8+

removeAll(userId: number, callback: AsyncCallback<void>): void

Removes all notifications 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

NameTypeMandatoryDescription
userIdnumberYesUser ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

function removeAllCallback(err: Base.BusinessError) {
  if (err) {
    console.info("removeAll failed " + JSON.stringify(err));
  } else {
    console.info("removeAll success");
  }
}

let userId: number = 1;
Notification.removeAll(userId, removeAllCallback);

Notification.removeAll8+

removeAll(userId: number): Promise<void>

Removes all notifications 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

NameTypeMandatoryDescription
userIdnumberYesUser ID.

Example

import Base from '@ohos.base';

let userId: number = 1;
Notification.removeAll(userId).then(() => {
  console.info("removeAll success");
}).catch((err: Base.BusinessError) => {
  ar
});

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

NameTypeMandatoryDescription
callbackAsyncCallback<Array<NotificationRequest>>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

function getAllActiveNotificationsCallback(err: Base.BusinessError, data: NotificationManager.NotificationRequest[]) {
  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

TypeDescription
Promise<Array<NotificationRequest>>Promise used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

Notification.getAllActiveNotifications().then((data: NotificationManager.NotificationRequest[]) => {
  console.info("getAllActiveNotifications success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
  console.error(`getAllActiveNotifications failed, code is ${err}`);
});

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

NameTypeMandatoryDescription
callbackAsyncCallback<number>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let getActiveNotificationCountCallback = (err: Base.BusinessError, data: number) => {
  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

TypeDescription
Promise<number>Promise used to return the result.

Example

import Base from '@ohos.base';

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

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

NameTypeMandatoryDescription
callbackAsyncCallback<Array<NotificationRequest>>YesCallback used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

let getActiveNotificationsCallback = (err: Base.BusinessError, data: NotificationManager.NotificationRequest[]) => {
  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

TypeDescription
Promise<Array<NotificationRequest>>Promise used to return the result.

Example

import Base from '@ohos.base';
import NotificationManager from '@ohos.notificationManager';

Notification.getActiveNotifications().then((data: NotificationManager.NotificationRequest[]) => {
  console.info("removeGroupByBundle success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
  console.error(`removeGroupByBundle failed, code is ${err}`);
});

Notification.cancelGroup8+

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

NameTypeMandatoryDescription
groupNamestringYesName of the notification group, which is specified through NotificationRequest when the notification is published.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let cancelGroupCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("cancelGroup failed " + JSON.stringify(err));
  } else {
    console.info("cancelGroup success");
  }
}

let groupName: string = "GroupName";

Notification.cancelGroup(groupName, cancelGroupCallback);

Notification.cancelGroup8+

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

NameTypeMandatoryDescription
groupNamestringYesName of the notification group.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let groupName: string = "GroupName";
Notification.cancelGroup(groupName).then(() => {
	console.info("cancelGroup success");
}).catch((err: Base.BusinessError) => {
  console.error(`cancelGroup failed, code is ${err}`);
});

Notification.removeGroupByBundle8+

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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
groupNamestringYesName of the notification group.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let removeGroupByBundleCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("removeGroupByBundle failed " + JSON.stringify(err));
  } else {
    console.info("removeGroupByBundle success");
  }
}

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

Notification.removeGroupByBundle(bundleOption, groupName, removeGroupByBundleCallback);

Notification.removeGroupByBundle8+

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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
groupNamestringYesName of the notification group.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let bundleOption: Notification.BundleOption = {bundle: "Bundle"};
let groupName: string = "GroupName";
Notification.removeGroupByBundle(bundleOption, groupName).then(() => {
  console.info("removeGroupByBundle success");
}).catch((err: Base.BusinessError) => {
  console.error(`removeGroupByBundle failed, code is ${err}`);
});

Notification.setDoNotDisturbDate8+

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

NameTypeMandatoryDescription
dateDoNotDisturbDateYesDND time to set.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let setDoNotDisturbDateCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("setDoNotDisturbDate failed " + JSON.stringify(err));
  } else {
    console.info("setDoNotDisturbDate success");
  }
}

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

Notification.setDoNotDisturbDate(doNotDisturbDate, setDoNotDisturbDateCallback);

Notification.setDoNotDisturbDate8+

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

NameTypeMandatoryDescription
dateDoNotDisturbDateYesDND time to set.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

let doNotDisturbDate: Notification.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");
}).catch((err: Base.BusinessError) => {
  console.error(`setDoNotDisturbDate failed, code is ${err}`);
});

Notification.setDoNotDisturbDate8+

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

NameTypeMandatoryDescription
dateDoNotDisturbDateYesDND time to set.
userIdnumberYesID of the user for whom you want to set the DND time.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let setDoNotDisturbDateCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("setDoNotDisturbDate failed " + JSON.stringify(err));
  } else {
    console.info("setDoNotDisturbDate success");
  }
}

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

let userId: number = 1;
Notification.setDoNotDisturbDate(doNotDisturbDate, userId, setDoNotDisturbDateCallback);

Notification.setDoNotDisturbDate8+

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

NameTypeMandatoryDescription
dateDoNotDisturbDateYesDND time to set.
userIdnumberYesID of the user for whom you want to set the DND time.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

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

let userId: number = 1;

Notification.setDoNotDisturbDate(doNotDisturbDate, userId).then(() => {
  console.info("setDoNotDisturbDate success");
}).catch((err: Base.BusinessError) => {
  console.error(`setDoNotDisturbDate failed, code is ${err}`);
});

Notification.getDoNotDisturbDate8+

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

NameTypeMandatoryDescription
callbackAsyncCallback<DoNotDisturbDate>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let getDoNotDisturbDateCallback = (err: Base.BusinessError, data: Notification.DoNotDisturbDate) => {
  if (err) {
    console.info("getDoNotDisturbDate failed " + JSON.stringify(err));
  } else {
    console.info("getDoNotDisturbDate success");
  }
}

Notification.getDoNotDisturbDate(getDoNotDisturbDateCallback);

Notification.getDoNotDisturbDate8+

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

TypeDescription
Promise<DoNotDisturbDate>Promise used to return the result.

Example

import Base from '@ohos.base';

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

Notification.getDoNotDisturbDate8+

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

NameTypeMandatoryDescription
callbackAsyncCallback<DoNotDisturbDate>YesCallback used to return the result.
userIdnumberYesUser ID.

Example

import Base from '@ohos.base';

let getDoNotDisturbDateCallback = (err: Base.BusinessError, data: Notification.DoNotDisturbDate) => {
  if (err) {
    console.info("getDoNotDisturbDate failed " + JSON.stringify(err));
  } else {
    console.info("getDoNotDisturbDate success");
  }
}

let userId: number = 1;

Notification.getDoNotDisturbDate(userId, getDoNotDisturbDateCallback);

Notification.getDoNotDisturbDate8+

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

NameTypeMandatoryDescription
userIdnumberYesUser ID.

Return value

TypeDescription
Promise<DoNotDisturbDate>Promise used to return the result.

Example

import Base from '@ohos.base';

let userId: number = 1;

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

Notification.supportDoNotDisturbMode8+

supportDoNotDisturbMode(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

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let supportDoNotDisturbModeCallback = (err: Base.BusinessError, data: boolean) => {
  if (err) {
    console.info("supportDoNotDisturbMode failed " + JSON.stringify(err));
  } else {
    console.info("supportDoNotDisturbMode success");
  }
}

Notification.supportDoNotDisturbMode(supportDoNotDisturbModeCallback);

Notification.supportDoNotDisturbMode8+

supportDoNotDisturbMode(): 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

TypeDescription
Promise<boolean>Promise used to return the result.

Example

import Base from '@ohos.base';

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

Notification.isSupportTemplate8+

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

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

System capability: SystemCapability.Notification.Notification

Parameters

NameTypeMandatoryDescription
templateNamestringYesTemplate name.
callbackAsyncCallback<boolean>YesCallback used to return the result.

Example

import Base from '@ohos.base';

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

Notification.isSupportTemplate(templateName, isSupportTemplateCallback);

Notification.isSupportTemplate8+

isSupportTemplate(templateName: string): Promise<boolean>

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

System capability: SystemCapability.Notification.Notification

Parameters

NameTypeMandatoryDescription
templateNamestringYesTemplate name.

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Example

import Base from '@ohos.base';

let templateName: string = 'process';
Notification.isSupportTemplate(templateName).then((data: boolean) => {
  console.info("isSupportTemplate success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
  console.error(`isSupportTemplate failed, code is ${err}`);
});

Notification.requestEnableNotification8+

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

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let requestEnableNotificationCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("requestEnableNotification failed " + JSON.stringify(err));
  } else {
    console.info("requestEnableNotification success");
  }
};

Notification.requestEnableNotification(requestEnableNotificationCallback);

Notification.requestEnableNotification8+

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

Return value

TypeDescription
Promise<void>Promise that returns no value.

Example

import Base from '@ohos.base';

Notification.requestEnableNotification().then(() => {
  console.info("requestEnableNotification success");
}).catch((err: Base.BusinessError) => {
  console.error(`requestEnableNotification failed, code is ${err}`);
});

Notification.enableDistributed8+

enableDistributed(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

NameTypeMandatoryDescription
enablebooleanYesWhether the device supports distributed notifications.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let enabledNotificationCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("enableDistributed failed " + JSON.stringify(err));
  } else {
    console.info("enableDistributed success");
  }
};

let enable: boolean = true;

Notification.enableDistributed(enable, enabledNotificationCallback);

Notification.enableDistributed8+

enableDistributed(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

NameTypeMandatoryDescription
enablebooleanYesWhether the device supports distributed notifications.

Example

import Base from '@ohos.base';

let enable: boolean = true;
Notification.enableDistributed(enable).then(() => {
  console.info("enableDistributed success");
}).catch((err: Base.BusinessError) => {
  console.error(`enableDistributed failed, code is ${err}`);
});

Notification.isDistributedEnabled8+

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

NameTypeMandatoryDescription
callbackAsyncCallback<boolean>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let isDistributedEnabledCallback = (err: Base.BusinessError, data: boolean) => {
  if (err) {
    console.info("isDistributedEnabled failed " + JSON.stringify(err));
  } else {
    console.info("isDistributedEnabled success " + JSON.stringify(data));
  }
};

Notification.isDistributedEnabled(isDistributedEnabledCallback);

Notification.isDistributedEnabled8+

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

TypeDescription
Promise<boolean>Promise used to return the result.

Example

import Base from '@ohos.base';

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

Notification.enableDistributedByBundle8+

enableDistributedByBundle(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

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
enablebooleanYesWhether the device supports distributed notifications.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let enableDistributedByBundleCallback = (err: Base.BusinessError) => {
  if (err) {
    console.info("enableDistributedByBundle failed " + JSON.stringify(err));
  } else {
    console.info("enableDistributedByBundle success");
  }
};

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};

let enable: boolean = true;

Notification.enableDistributedByBundle(bundle, enable, enableDistributedByBundleCallback);

Notification.enableDistributedByBundle8+

enableDistributedByBundle(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

NameTypeMandatoryDescription
bundleBundleOptionYesApplication bundle.
enablebooleanYesWhether the device supports distributed notifications.

Example

import Base from '@ohos.base';

let enable: boolean = true;

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};

Notification.enableDistributedByBundle(bundle, enable).then(() => {
  console.info("enableDistributedByBundle success");
}).catch((err: Base.BusinessError) => {
  console.error(`enableDistributedByBundle failed, code is ${err}`);
});

Notification.isDistributedEnabledByBundle8+

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

Obtains whether an application supports distributed notifications based on the bundle. 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

NameTypeMandatoryDescription
bundleBundleOptionYesApplication bundle.
callbackAsyncCallback<boolean>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let isDistributedEnabledByBundleCallback = (err: Base.BusinessError, data: boolean) => {
  if (err) {
    console.info("isDistributedEnabledByBundle failed " + JSON.stringify(err));
  } else {
    console.info("isDistributedEnabledByBundle success" + JSON.stringify(data));
  }
};

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};

Notification.isDistributedEnabledByBundle(bundle, isDistributedEnabledByBundleCallback);

Notification.isDistributedEnabledByBundle8+

isDistributedEnabledByBundle(bundle: BundleOption): Promise<boolean>

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

NameTypeMandatoryDescription
bundleBundleOptionYesApplication bundle.

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Example

import Base from '@ohos.base';

let bundle: Notification.BundleOption = {
  bundle: "bundleName1",
};

Notification.isDistributedEnabledByBundle(bundle).then((data: boolean) => {
  console.info("isDistributedEnabledByBundle success, data: " + JSON.stringify(data));
}).catch((err: Base.BusinessError) => {
  console.error(`isDistributedEnabledByBundle failed, code is ${err}`);
});

Notification.getDeviceRemindType8+

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

NameTypeMandatoryDescription
callbackAsyncCallback<DeviceRemindType>YesCallback used to return the result.

Example

import Base from '@ohos.base';

let getDeviceRemindTypeCallback = (err: Base.BusinessError, data: Notification.DeviceRemindType) => {
  if (err) {
    console.info("getDeviceRemindType failed " + JSON.stringify(err));
  } else {
    console.info("getDeviceRemindType success");
  }
};

Notification.getDeviceRemindType(getDeviceRemindTypeCallback);

Notification.getDeviceRemindType8+

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

TypeDescription
Promise<DeviceRemindType>Promise used to return the result.

Example

import Base from '@ohos.base';

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

SubscribeCallbackData

System capability: SystemCapability.Notification.Notification

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

NameTypeReadableWritableDescription
requestNotificationRequestYesNoNotification content.
sortingMapNotificationSortingMapYesNoNotification sorting information.
reasonnumberYesNoReason for deletion.
soundstringYesNoSound used for notification.
vibrationValuesArray<number>YesNoVibration used for notification.

EnabledNotificationCallbackData8+

System capability: SystemCapability.Notification.Notification

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

NameTypeReadableWritableDescription
bundlestringYesNoBundle name of the application.
uidnumberYesNoUID of the application.
enablebooleanYesNoNotification enabled status of the application.

DoNotDisturbDate8+ deprecated

System capability: SystemCapability.Notification.Notification

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use notificationManager.DoNotDisturbDate instead.

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

NameTypeReadableWritableDescription
typeDoNotDisturbTypeYesYesDND time type.
beginDateYesYesDND start time.
endDateYesYesDND end time.

DoNotDisturbType8+ deprecated

System capability: SystemCapability.Notification.Notification

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use notificationManager.DoNotDisturbType instead.

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

NameValueDescription
TYPE_NONE0Non-DND.
TYPE_ONCE1One-shot DND at the specified time segment (only considering the hour and minute).
TYPE_DAILY2Daily DND at the specified time segment (only considering the hour and minute).
TYPE_CLEARLY3DND at the specified time segment (considering the year, month, day, hour, and minute).

ContentType

System capability: SystemCapability.Notification.Notification

NameValueDescription
NOTIFICATION_CONTENT_BASIC_TEXTNOTIFICATION_CONTENT_BASIC_TEXTNormal text notification.
NOTIFICATION_CONTENT_LONG_TEXTNOTIFICATION_CONTENT_LONG_TEXTLong text notification.
NOTIFICATION_CONTENT_PICTURENOTIFICATION_CONTENT_PICTUREPicture-attached notification.
NOTIFICATION_CONTENT_CONVERSATIONNOTIFICATION_CONTENT_CONVERSATIONConversation notification.
NOTIFICATION_CONTENT_MULTILINENOTIFICATION_CONTENT_MULTILINEMulti-line text notification.

SlotLevel

System capability: SystemCapability.Notification.Notification

NameValueDescription
LEVEL_NONE0The notification function is disabled.
LEVEL_MIN1The notification function is enabled, but the notification icon is not displayed in the status bar, with no banner or alert tone.
LEVEL_LOW2The notification function is enabled, and the notification icon is displayed in the status bar, with no banner or alert tone.
LEVEL_DEFAULT3The notification feature is enabled, and the notification icon is displayed in the status bar, with an alert tone but no banner.
LEVEL_HIGH4The notification feature is enabled, and the notification icon is displayed in the status bar, with an alert tone and banner.

BundleOptiondeprecated

System capability: SystemCapability.Notification.Notification

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use notificationManager.BundleOption instead.

NameTypeMandatoryDescription
bundlestringYesBundle information of the application.
uidnumberNoUser ID. The default value is 0.

NotificationKeydeprecated

System capability: SystemCapability.Notification.Notification

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use notificationManager.NotificationKey instead.

NameTypeReadableWritableDescription
idnumberYesYesNotification ID.
labelstringYesYesNotification label.

SlotType

System capability: SystemCapability.Notification.Notification

NameValueDescription
UNKNOWN_TYPE0Unknown type.
SOCIAL_COMMUNICATION1Notification slot for social communication.
SERVICE_INFORMATION2Notification slot for service information.
CONTENT_INFORMATION3Notification slot for content consultation.
OTHER_TYPES0xFFFFNotification slot for other purposes.

NotificationActionButton

Describes the button displayed in the notification.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
titlestringYesYesButton title.
wantAgentWantAgentYesYesWantAgent of the button.
extras{ [key: string]: any }YesYesExtra information of the button.
userInput8+NotificationUserInputYesYesUser input object.

NotificationBasicContent

Describes the normal text notification.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
titlestringYesYesNotification title.
textstringYesYesNotification content.
additionalTextstringYesYesAdditional information of the notification.

NotificationLongTextContent

Describes the long text notification.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
titlestringYesYesNotification title.
textstringYesYesNotification content.
additionalTextstringYesYesAdditional information of the notification.
longTextstringYesYesLong text of the notification.
briefTextstringYesYesBrief text of the notification.
expandedTitlestringYesYesTitle of the notification in the expanded state.

NotificationMultiLineContent

Describes the multi-line text notification.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
titlestringYesYesNotification title.
textstringYesYesNotification content.
additionalTextstringYesYesAdditional information of the notification.
briefTextstringYesYesBrief text of the notification.
longTitlestringYesYesTitle of the notification in the expanded state.
linesArray<string>YesYesMulti-line text of the notification.

NotificationPictureContent

Describes the picture-attached notification.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
titlestringYesYesNotification title.
textstringYesYesNotification content.
additionalTextstringYesYesAdditional information of the notification.
briefTextstringYesYesBrief text of the notification.
expandedTitlestringYesYesTitle of the notification in the expanded state.
pictureimage.PixelMapYesYesPicture attached to the notification.

NotificationContent

Describes the notification content.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
contentTypeContentTypeYesYesNotification content type.
normalNotificationBasicContentYesYesNormal text.
longTextNotificationLongTextContentYesYesLong text.
multiLineNotificationMultiLineContentYesYesMulti-line text.
pictureNotificationPictureContentYesYesPicture-attached.

NotificationFlagStatus8+

Describes the notification flag status.

System capability: SystemCapability.Notification.Notification

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

NameValueDescription
TYPE_NONE0The default flag is used.
TYPE_OPEN1The notification flag is enabled.
TYPE_CLOSE2The notification flag is disabled.

NotificationFlags8+

Enumerates notification flags.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
soundEnabledNotificationFlagStatusYesNoWhether to enable the sound alert for the notification.
vibrationEnabledNotificationFlagStatusYesNoWhether to enable vibration for the notification.

NotificationRequest

Describes the notification request.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
contentNotificationContentYesYesNotification content.
idnumberYesYesNotification ID.
slotTypeSlotTypeYesYesSlot type.
isOngoingbooleanYesYesWhether the notification is an ongoing notification.
isUnremovablebooleanYesYesWhether the notification can be removed.
deliveryTimenumberYesYesTime when the notification is sent.
tapDismissedbooleanYesYesWhether the notification is automatically cleared.
autoDeletedTimenumberYesYesTime when the notification is automatically cleared.
wantAgentWantAgentYesYesWantAgent instance to which the notification will be redirected after being clicked.
extraInfo{[key: string]: any}YesYesExtended parameters.
colornumberYesYesBackground color of the notification. Not supported currently.
colorEnabledbooleanYesYesWhether the notification background color is enabled. Not supported currently.
isAlertOncebooleanYesYesWhether the notification triggers an alert only once.
isStopwatchbooleanYesYesWhether to display the stopwatch.
isCountDownbooleanYesYesWhether to display the countdown time.
isFloatingIconbooleanYesYesWhether the notification is displayed as a floating icon in the status bar.
labelstringYesYesNotification label.
badgeIconStylenumberYesYesNotification badge type.
showDeliveryTimebooleanYesYesWhether to display the time when the notification is delivered.
actionButtonsArray<NotificationActionButton>YesYesButtons in the notification. Up to two buttons are allowed.
smallIconimage.PixelMapYesYesSmall notification icon. This field is optional, and the icon size cannot exceed 30 KB.
largeIconimage.PixelMapYesYesLarge notification icon. This field is optional, and the icon size cannot exceed 30 KB.
creatorBundleNamestringYesNoName of the bundle that creates the notification.
creatorUidnumberYesNoUID used for creating the notification.
creatorPidnumberYesNoPID used for creating the notification.
creatorUserId8+numberYesNoID of the user who creates the notification.
hashCodestringYesNoUnique ID of the notification.
classificationstringYesYesNotification category.
System API: This is a system API and cannot be called by third-party applications.
groupName8+stringYesYesNotification group name.
template8+NotificationTemplateYesYesNotification template.
isRemoveAllowed8+booleanYesNoWhether the notification can be removed.
System API: This is a system API and cannot be called by third-party applications.
source8+numberYesNoNotification source.
System API: This is a system API and cannot be called by third-party applications.
distributedOption8+DistributedOptionsYesYesDistributed notification options.
deviceId8+stringYesNoDevice ID of the notification source.
System API: This is a system API and cannot be called by third-party applications.
notificationFlags8+NotificationFlagsYesNoNotification flags.
removalWantAgent9+WantAgentYesYesWantAgent instance to which the notification will be redirected when it is removed.
badgeNumber9+numberYesYesNumber of notifications displayed on the application icon.

DistributedOptions8+

Describes distributed notifications options.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
isDistributedbooleanYesYesWhether the notification is a distributed notification.
supportDisplayDevicesArray<string>YesYesList of the devices to which the notification can be synchronized.
supportOperateDevicesArray<string>YesYesList of the devices on which the notification can be opened.
remindTypenumberYesNoNotification reminder type.
System API: This is a system API and cannot be called by third-party applications.

NotificationSlot

Describes the notification slot.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
typeSlotTypeYesYesSlot type.
levelnumberYesYesNotification level. If this parameter is not set, the default value is used based on the notification slot type.
descstringYesYesNotification slot description.
badgeFlagbooleanYesYesWhether to display the badge.
bypassDndbooleanYesYesWhether to bypass DND mode in the system.
lockscreenVisibilitynumberYesYesMode for displaying the notification on the lock screen.
vibrationEnabledbooleanYesYesWhether vibration is enabled for the notification.
soundstringYesYesNotification alert tone.
lightEnabledbooleanYesYesWhether the indicator blinks for the notification.
lightColornumberYesYesIndicator color of the notification.
vibrationValuesArray<number>YesYesVibration mode of the notification.
enabled9+booleanYesNoWhether the notification slot is enabled.

NotificationSorting

Provides sorting information of active notifications.

System capability: SystemCapability.Notification.Notification

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

NameTypeReadableWritableDescription
slotNotificationSlotYesNoNotification slot content.
hashCodestringYesNoUnique ID of the notification.
rankingnumberYesNoNotification sequence number.

NotificationSortingMap

Provides sorting information of active notifications in all subscribed notifications.

System capability: SystemCapability.Notification.Notification

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

NameTypeReadableWritableDescription
sortings{[key: string]: NotificationSorting}YesNoArray of notification sorting information.
sortedHashCodeArray<string>YesNoArray of unique notification IDs.

NotificationSubscribeInfo

Provides the information about the publisher for notification subscription.

System capability: SystemCapability.Notification.Notification

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

NameTypeReadableWritableDescription
bundleNamesArray<string>YesYesBundle names of the applications whose notifications are to be subscribed to.
userIdnumberYesYesUser whose notifications are to be subscribed to. You can obtain the user ID through getCurrentOsAccount.

NotificationTemplate8+

Describes the notification template.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
namestringYesYesTemplate name.
data{[key:string]: Object}YesYesTemplate data.

NotificationUserInput8+

Provides the notification user input.

System capability: SystemCapability.Notification.Notification

NameTypeReadableWritableDescription
inputKeystringYesYesKey to identify the user input.

DeviceRemindType8+ deprecated

System capability: SystemCapability.Notification.Notification

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

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use notificationManager.DeviceRemindType instead.

NameValueDescription
IDLE_DONOT_REMIND0The device is not in use. No notification is required.
IDLE_REMIND1The device is not in use.
ACTIVE_DONOT_REMIND2The device is in use. No notification is required.
ACTIVE_REMIND3The device is in use.

SourceType8+ deprecated

System capability: SystemCapability.Notification.Notification

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

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use notificationManager.SourceType instead.

NameValueDescription
TYPE_NORMAL0Normal notification.
TYPE_CONTINUOUS1Continuous notification.
TYPE_TIMER2Timed notification.

RemoveReason deprecated

System capability: SystemCapability.Notification.Notification

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

NOTE

This API is supported since API version 7 and deprecated since API version 9. You are advised to use notificationManager.RemoveReason instead.

NameValueDescription
CLICK_REASON_REMOVE1The notification is removed after a click on it.
CANCEL_REASON_REMOVE2The notification is removed by the user.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

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