NotificationSubscriber

作为订阅通知接口subscribe的入参,提供订阅者接收到新通知、取消通知等的回调方法。

说明:

本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口均为系统接口.

导入模块

import notificationSubscribe from '@ohos.notificationSubscribe';

onConsume

onConsume?: (data: SubscribeCallbackData) => void

接收到新通知的回调函数。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onConsume (data: SubscribeCallbackData) => void 新接收到的通知信息。

示例:

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

取消通知的回调函数。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onCancel (data: SubscribeCallbackData) => void 需要取消的通知信息。

示例:

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

更新通知排序的回调函数。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onUpdate (data: NotificationSortingMap) => void 最新的通知排序列表。

示例:

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

订阅完成的回调函数。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onConnect () => void 订阅完成的回调。

示例:

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

取消订阅的回调函数。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onDisconnect () => void 取消订阅的回调。

示例:

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

// 订阅通知后会收到onConnect回调
notificationSubscribe.subscribe(subscriber, subscribeCallback);
// 取消订阅后会收到onDisconnect回调
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);

onDestroy

onDestroy?:() => void

服务失联回调函数。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onDestroy () => void 服务失联的回调。

示例:

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

免打扰时间选项发生变更时的回调函数。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onDoNotDisturbDateChange (mode: notification.DoNotDisturbDate) => void 回调返回免打扰时间选项变更。

示例:

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

onEnabledNotificationChanged8+

onEnabledNotificationChanged?:(callbackData: EnabledNotificationCallbackData) => void

监听应用通知使能变化。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onEnabledNotificationChanged (callbackData: EnabledNotificationCallbackData) => void 回调返回监听到的应用信息。

示例:

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

监听应用角标个数变化。

系统能力:SystemCapability.Notification.Notification

系统接口: 此接口为系统接口。

参数:

参数名 类型 必填 说明
onBadgeChanged (data: BadgeNumberCallbackData) => void 回调返回监听到的应用信息。

示例:

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

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

系统接口:此接口为系统接口。

名称 类型 可读 可写 说明
request NotificationRequest 通知内容。
sortingMap NotificationSortingMap 通知排序信息。
reason number 删除原因(1:点击通知后删除通知,2:用户删除通知) 。
sound string 通知声音。
vibrationValues Array<number> 通知震动。

EnabledNotificationCallbackData8+

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

系统接口:此接口为系统接口。

名称 类型 可读 可写 说明
bundle string 应用的包名。
uid number 应用的uid。
enable boolean 应用通知使能状态。

BadgeNumberCallbackData10+

系统能力:以下各项对应的系统能力均为SystemCapability.Notification.Notification

系统接口:此接口为系统接口。

名称 类型 可读 可写 说明
bundle string 应用的包名。
uid number 应用的uid。
badgeNumber number 角标个数。