harmony 鸿蒙NotificationSubscriber

2023-06-24 浏览 (612)

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

NameTypeMandatoryDescription
dataSubscribeCallbackDataYesInformation 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

NameTypeMandatoryDescription
dataSubscribeCallbackDataYesInformation 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

NameTypeMandatoryDescription
dataNotificationSortingMapYesLatest 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

NameTypeMandatoryDescription
modenotification.DoNotDisturbDateYesDND 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

NameTypeMandatoryDescription
callbackAsyncCallback<EnabledNotificationCallbackData>YesCallback 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

NameTypeMandatoryDescription
callbackAsyncCallback<BadgeNumberCallbackData>YesCallback 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.

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.

BadgeNumberCallbackData10+

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.
badgeNumbernumberYesNoNumber 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)

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