NotificationSubscriber (System API)

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.

The APIs provided by this module are system APIs.

Modules to Import

import notificationSubscribe from '@ohos.notificationSubscribe';

onConsume

onConsume?: (data: SubscribeCallbackData) => void

Called when a new notification is received.

System capability: SystemCapability.Notification.Notification

System API: This is a system API.

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.

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.

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.

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.

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.

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+(deprecated)

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

Called when the DND time settings are changed.

NOTE

This API is supported since API version 8 and is deprecated since API version 11. You are advised to use onDoNotDisturbChanged instead.

System capability: SystemCapability.Notification.Notification

System API: This is a system API.

Parameters

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

Example

import Base from '@ohos.base';
import Notification from '@ohos.notification';

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: Notification.DoNotDisturbDate) => {
  console.info('===> onDoNotDisturbDateChange:' + mode);
}

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

notificationSubscribe.subscribe(subscriber, subscribeCallback);

onDoNotDisturbChanged11+

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

Called when the DND time settings are changed.

System API: This is a system API.

System capability: SystemCapability.Notification.Notification

Parameters

Name Type Mandatory Description
onDoNotDisturbChanged (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 onDoNotDisturbChangedCallback = (mode: NotificationManager.DoNotDisturbDate) => {
  console.info('===> onDoNotDisturbChanged:' + mode);
}

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onDoNotDisturbChanged: onDoNotDisturbChangedCallback
};

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.

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.

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);

onBatchCancel11+

onBatchCancel?:(data: Array<SubscribeCallbackData>) => void

Called for batch deletion.

System capability: SystemCapability.Notification.Notification

System API: This is a system API.

Parameters

Name Type Mandatory Description
onBatchCancel (data: Array<SubscribeCallbackData>) => void Yes Notification information of batch deletion.

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 onBatchCancelCallBack = (data: Array<notificationSubscribe.SubscribeCallbackData>) => {
  console.info('===> onBatchCancel in test');
  let req = data[0].request;
  console.info('===> onBatchCancel callback req.id:' + req.id);
};

let subscriber: notificationSubscribe.NotificationSubscriber = {
  onBatchCancel: onBatchCancelCallBack
};

notificationSubscribe.subscribe(subscriber, subscribeCallback);

SubscribeCallbackData

System capability: SystemCapability.Notification.Notification

System API: This is a system API.

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.

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.

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.