NotificationSubscriber

The NotificationSubscriber module provides callbacks for receiving or removing notifications and serves as the input parameter of subscribe.

NOTE

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.

Modules to Import

import notificationSubscribe from '@ohos.notificationSubscribe';

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

onConsume

onConsume?: (data: SubscribeCallbackData) => void

Called when a new notification is received.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onConsume (data: SubscribeCallbackData) => void Yes Information about the notification received.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info('===> onConsume in test');
  let req = data.request;
  console.info('===> onConsume callback req.id:' + req.id);
};

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onCancel

onCancel?:(data: SubscribeCallbackData) => void

Called when a notification is canceled.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onCancel (data: SubscribeCallbackData) => void Yes Information about the notification to cancel.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let onCancelCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info('===> onCancel in test');
  let req = data.request;
  console.info('===> onCancel callback req.id:' + req.id);
}

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onCancel: onCancelCallback
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onUpdate

onUpdate?:(data: NotificationSortingMap) => void

Called when notification sorting is updated.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onUpdate (data: NotificationSortingMap) => void Yes Latest notification sorting list.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onUpdate: (map) => {
    console.info('===> onUpdateCallback map:' + JSON.stringify(map));
  }
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onConnect

onConnect?:() => void

Called when the subscription is complete.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onConnect () => void Yes Callback invoked when the subscription is complete.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let onConnectCallback = () => {
  console.info('===> onConnect in test');
}

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConnect: onConnectCallback
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onDisconnect

onDisconnect?:() => void

Called when unsubscription is complete.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onDisconnect () => void Yes Callback invoked when unsubscription is complete.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};
let unsubscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("unsubscribeCallback");
  }
};

let onConnectCallback = () => {
  console.info('===> onConnect in test');
}
let onDisconnectCallback = () => {
  console.info('===> onDisconnect in test');
}

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConnect: onConnectCallback,
  onDisconnect: onDisconnectCallback
};

// The onConnect callback is invoked when subscription to the notification is complete.
notificationSubscribe.subscribe(subscriber, subscribeCallback);
// The onDisconnect callback is invoked when unsubscription to the notification is complete.
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);

onDestroy

onDestroy?:() => void

Called when the service is disconnected.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onDestroy () => void Yes Callback invoked when the service is disconnected.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let onDestroyCallback = () => {
  console.info('===> onDestroy in test');
}

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onDestroy: onDestroyCallback
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onDoNotDisturbDateChange8+

onDoNotDisturbDateChange?:(mode: notificationManager.DoNotDisturbDate) => void

Called when the DND time settings are changed.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onDoNotDisturbDateChange (mode: notificationManager.DoNotDisturbDate) => void Yes DND time setting updates.

Example

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

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let onDoNotDisturbDateChangeCallback = (mode: NotificationManager.DoNotDisturbDate) => {
  console.info('===> onDoNotDisturbDateChange:' + mode);
}

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onEnabledNotificationChanged8+

onEnabledNotificationChanged?:(callbackData: EnabledNotificationCallbackData) => void

Listens for the notification enabled status changes.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onEnabledNotificationChanged (callbackData: EnabledNotificationCallbackData) => void Yes Callback used to return the result.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let onEnabledNotificationChangedCallback = (callbackData: notificationSubscribe.EnabledNotificationCallbackData) => {
  console.info("bundle: ", callbackData.bundle);
  console.info("uid: ", callbackData.uid);
  console.info("enable: ", callbackData.enable);
};

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onEnabledNotificationChanged: onEnabledNotificationChangedCallback
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onBadgeChanged10+

onBadgeChanged?:(data: BadgeNumberCallbackData) => void

Listens for the change of the notification badge number.

System capability: SystemCapability.Notification.Notification

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

Parameters

Name Type Mandatory Description
onBadgeChanged (data: BadgeNumberCallbackData) => void Yes Callback used to return the result.

Example

import Base from '@ohos.base';

let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribeCallback");
  }
};

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onBadgeChanged: (data) => {
    console.info("bundle: ", data.bundle);
    console.info("uid: ", data.uid);
    console.info("badgeNumber: ", data.badgeNumber);
  }
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

SubscribeCallbackData

System capability: SystemCapability.Notification.Notification

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

Name Type Readable Writable Description
request NotificationRequest Yes No Notification content.
sortingMap NotificationSortingMap Yes No Notification sorting information.
reason number Yes No Reason for deletion. The options are as follows:
1: The notification is deleted after being clicked.
2: The notification is deleted by the user.
sound string Yes No Sound used for notification.
vibrationValues Array<number> Yes No Vibration used for notification.

EnabledNotificationCallbackData8+

System capability: SystemCapability.Notification.Notification

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

Name Type Readable Writable Description
bundle string Yes No Bundle name of the application.
uid number Yes No UID of the application.
enable boolean Yes No Notification enabled status of the application.

BadgeNumberCallbackData10+

System capability: SystemCapability.Notification.Notification

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

Name Type Readable Writable Description
bundle string Yes No Bundle name of the application.
uid number Yes No UID of the application.
badgeNumber number Yes No Number of notifications displayed on the application icon.