harmony 鸿蒙NotificationSubscriber

  • 2023-06-24
  • 浏览 (453)

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
data SubscribeCallbackData 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
data SubscribeCallbackData 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");
  }
};

function 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
data NotificationSortingMap 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.

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.

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.

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: notification.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
mode notification.DoNotDisturbDate 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
callback AsyncCallback<EnabledNotificationCallbackData> 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
callback AsyncCallback<BadgeNumberCallbackData> 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.
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.

你可能感兴趣的鸿蒙文章

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)

0  赞