harmony 鸿蒙@ohos.notificationSubscribe (NotificationSubscribe)

2023-02-03 浏览 (671)

@ohos.notificationSubscribe (NotificationSubscribe)

The notificationSubscribe module provides APIs for notification subscription, notification unsubscription, subscription removal, and more. In general cases, only system applications can call these APIs.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import notificationSubscribe from '@ohos.notificationSubscribe';

NotificationSubscribe.subscribe

subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback<void>): void

Subscribes to a notification with the subscription information specified. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
infoNotificationSubscribeInfoYesNotification subscription information.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

Example

import Base from '@ohos.base';

// subscribe callback
let subscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("subscribe success");
  }
}
let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info("Consume callback: " + JSON.stringify(data));
}
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
};
// Bundle names are not verified. You need to determine the target bundle names.
let info: notificationSubscribe.NotificationSubscribeInfo = {
  bundleNames: ["bundleName1","bundleName2"]
};
notificationSubscribe.subscribe(subscriber, info, subscribeCallback);

NotificationSubscribe.subscribe

subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback<void>): void

Subscribes to notifications of all applications under this user. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

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("subscribe success");
  }
}
let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info("Consume callback: " + JSON.stringify(data));
}
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
};
notificationSubscribe.subscribe(subscriber, subscribeCallback);

NotificationSubscribe.subscribe

subscribe(subscriber: NotificationSubscriber, info?: NotificationSubscribeInfo): Promise<void>

Subscribes to a notification with the subscription information specified. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
infoNotificationSubscribeInfoNoNotification subscription information. This parameter is left empty by default.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600012No memory space.

Example

import Base from '@ohos.base';

let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
  console.info("Consume callback: " + JSON.stringify(data));
}
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onConsume: onConsumeCallback
};
notificationSubscribe.subscribe(subscriber).then(() => {
  console.info("subscribe success");
}).catch((err: Base.BusinessError) => {
  console.error("subscribe fail: " + JSON.stringify(err));
});

NotificationSubscribe.unsubscribe

unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback<void>): void

Unsubscribes from a notification. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

Example

import Base from '@ohos.base';

let unsubscribeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("unsubscribe success");
  }
}
let onDisconnectCallback = () => {
  console.info("subscribe disconnect");
}
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onDisconnect: onDisconnectCallback
};
notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);

NotificationSubscribe.unsubscribe

unsubscribe(subscriber: NotificationSubscriber): Promise<void>

Unsubscribes from a notification. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
subscriberNotificationSubscriberYesNotification subscriber.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

Example

import Base from '@ohos.base';

let onDisconnectCallback = () => {
  console.info("subscribe disconnect");
}
let subscriber: notificationSubscribe.NotificationSubscriber = {
  onDisconnect: onDisconnectCallback
};
notificationSubscribe.unsubscribe(subscriber).then(() => {
  console.info("unsubscribe success");
}).catch((err: Base.BusinessError) => {
  console.error("unsubscribe fail: " + JSON.stringify(err));
});

NotificationSubscribe.remove

remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason, callback: AsyncCallback<void>): void

Removes a notification for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
notificationKeyNotificationKeyYesNotification key.
reasonRemoveReasonYesReason for removing the notification.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.
17700001The specified bundle name was not found.

Example

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

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("remove success");
  }
}
let bundle: NotificationManager.BundleOption = {
  bundle: "bundleName1",
};
let notificationKey: notificationSubscribe.NotificationKey = {
  id: 0,
  label: "label",
};
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback);

NotificationSubscribe.remove

remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason): Promise<void>

Removes a notification for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
notificationKeyNotificationKeyYesNotification key.
reasonRemoveReasonYesReason for removing the notification.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.
17700001The specified bundle name was not found.

Example

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

let bundle: NotificationManager.BundleOption = {
  bundle: "bundleName1",
};
let notificationKey: notificationSubscribe.NotificationKey = {
  id: 0,
  label: "label",
};
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(bundle, notificationKey, reason).then(() => {
  console.info("remove success");
}).catch((err: Base.BusinessError) => {
  console.error("remove fail: " + JSON.stringify(err));
});

NotificationSubscribe.remove

remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback<void>): void

Removes a specified notification. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
hashCodestringYesUnique notification ID. It is the hashCode in the NotificationRequest object of SubscribeCallbackData of the onConsume callback.
reasonRemoveReasonYesReason for removing the notification.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.

Example

import Base from '@ohos.base';

let hashCode: string = 'hashCode';

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("remove success");
  }
}
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
notificationSubscribe.remove(hashCode, reason, removeCallback);

NotificationSubscribe.remove

remove(hashCode: string, reason: RemoveReason): Promise<void>

Removes a specified notification. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
hashCodestringYesUnique notification ID.
reasonRemoveReasonYesReason for removing the notification.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600007The notification is not exist.

Example

import Base from '@ohos.base';

let hashCode: string = 'hashCode';
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(hashCode, reason).then(() => {
	console.info("remove success");
}).catch((err: Base.BusinessError) => {
  console.error("remove fail: " + JSON.stringify(err));
});

NotificationSubscribe.remove10+

remove(hashCodes: Array<String>, reason: RemoveReason, callback: AsyncCallback<void>): void

Removes specified notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
hashCodesArray<String>YesArray of unique notification IDs. It is the hashCode in the NotificationRequest object of SubscribeCallbackData of the onConsume callback.
reasonRemoveReasonYesReason for removing the notification.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

Example

import Base from '@ohos.base';

let hashCodes: string[] = ['hashCode1', 'hashCode2'];

let removeCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("remove success");
  }
}
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
notificationSubscribe.remove(hashCodes, reason, removeCallback);

NotificationSubscribe.remove10+

remove(hashCodes: Array<String>, reason: RemoveReason): Promise<void>

Removes specified notifications. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
hashCodesArray<String>YesArray of unique notification IDs.
reasonRemoveReasonYesReason for removing the notification.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

Example

import Base from '@ohos.base';

let hashCodes: string[] = ['hashCode1','hashCode2'];
let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
notificationSubscribe.remove(hashCodes, reason).then(() => {
  console.info("remove success");
}).catch((err: Base.BusinessError) => {
  console.error("remove fail: " + JSON.stringify(err));
});

NotificationSubscribe.removeAll

removeAll(bundle: BundleOption, callback: AsyncCallback<void>): void

Removes all notifications for a specified application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

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

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

Parameters

NameTypeMandatoryDescription
bundleBundleOptionYesBundle information of the application.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

Example

import Base from '@ohos.base';

let removeAllCallback = (err: Base.BusinessError) => {
  if (err) {
    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
  } else {
    console.info("removeAll success");
  }
}
let bundle: notificationSubscribe.BundleOption = {
  bundle: "bundleName1",
};
notificationSubscribe.removeAll(bundle, removeAllCallback);

NotificationSubscribe.removeAll

removeAll(callback: AsyncCallback<void>): void

Removes all notifications. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.

Example

import Base from '@ohos.base';

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

notificationSubscribe.removeAll(removeAllCallback);

NotificationSubscribe.removeAll

removeAll(bundle?: BundleOption): Promise<void>

Removes all notifications for a specified application. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
bundleBundleOptionNoBundle information of the application. By default, this parameter is left empty, indicating that all notifications will be removed.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
17700001The specified bundle name was not found.

Example

import Base from '@ohos.base';

// If no application is specified, notifications of all applications are deleted.
notificationSubscribe.removeAll().then(() => {
	console.info("removeAll success");
}).catch((err: Base.BusinessError) => {
  console.error("removeAll fail: " + JSON.stringify(err));
});

NotificationSubscribe.removeAll

removeAll(userId: number, callback: AsyncCallback<void>): void

Removes all notifications for a specified user. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
userIdnumberYesUser ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.

Example

import Base from '@ohos.base';

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

let userId: number = 1;

notificationSubscribe.removeAll(userId, removeAllCallback);

NotificationSubscribe.removeAll

removeAll(userId: number): Promise<void>

Removes all notifications for a specified user. This API uses a promise to return the result.

System capability: SystemCapability.Notification.Notification

Required permissions: ohos.permission.NOTIFICATION_CONTROLLER

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

Parameters

NameTypeMandatoryDescription
userIdnumberYesUser ID.

Error codes

For details about the error codes, see Notification Error Codes.

IDError Message
1600001Internal error.
1600002Marshalling or unmarshalling error.
1600003Failed to connect service.
1600008The user is not exist.

Example

import Base from '@ohos.base';

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

let userId: number = 1;

notificationSubscribe.removeAll(userId, removeAllCallback);

NotificationKey

System capability: SystemCapability.Notification.Notification

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

NameTypeMandatoryDescription
idnumberYesNotification ID.
labelstringNoNotification label. This parameter is left empty by default.

RemoveReason

System capability: SystemCapability.Notification.Notification

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

NameValueDescription
CLICK_REASON_REMOVE1The notification is removed after a click on it.
CANCEL_REASON_REMOVE2The notification is removed by the user.

你可能感兴趣的鸿蒙文章

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/127147f802824e20b15ba00db101623f