openharmony 鸿蒙 notification-subscription-sys

2026-08-25 浏览 (1)

Subscribing to Notifications (for System Applications Only)

To receive notifications, an application must subscribe to notifications first. The notification subsystem provides two types of subscription APIs, allowing applications to subscribe to notifications from all applications or notifications from a specific application.

You can use the NotificationSubscriber object to provide callbacks for subscription events, such as subscription success, notification reception, notification cancellation, and subscription cancellation.

Principle of Notification Subscription

The notification service process involves the notification subsystem, notification sender, and notification subscriber. A notification is generated by the notification sender and sent to the notification subsystem through inter-process communication (IPC). The notification subsystem then distributes the notification to the notification subscriber.

  • Notification sender: It can be a third-party application or a system application. Pay special attention to this role.

  • Notification subscriber: It can only be a system application, for example, the notification center. By default, the notification center subscribes to notifications sent by all applications on the current device to the current user. You do not need to pay attention to this role.

Figure 1 Notification service process

notification_internal_principle

Available APIs

The major APIs for notification subscription are described as follows. For details about the APIs, see @ohos.notificationSubscribe (NotificationSubscribe) (System API).

Table 1 Major APIs for notification subscription

APIDescription
subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback<void>): voidSubscribes to notifications from a specific application.
subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback<void>): voidSubscribes to notifications from all applications.

Table 2 Callbacks for notification subscription

For details about the API, see NotificationSubscriber.

APIDescription
onConsume?: (data: SubscribeCallbackData) => voidCallback for receiving notifications.
onCancel?: (data: SubscribeCallbackData) => voidCallback for canceling notifications.
onUpdate?: (data: NotificationSortingMap) => voidCallback for notification sorting updates.
onConnect?: () => void;Callback for subscription.
onDisconnect?: () => void;Callback for unsubscription.
onDestroy?: () => voidCallback for disconnecting from the notification subsystem.
onDoNotDisturbDateChangedeprecated?: (mode: notification.DoNotDisturbDatedeprecated) => voidCallback for the Do Not Disturb (DNT) time changes. This API is deprecated since API version 11.
onDoNotDisturbChanged?: (mode: notificationManager.DoNotDisturbDate) => voidCallback for the Do Not Disturb (DNT) time changes.
onEnabledNotificationChanged?: (callbackData: EnabledNotificationCallbackData) => voidCallback for notification switch changes.
onBadgeChanged?: (data: BadgeNumberCallbackData) => voidCallback for notification badge number changes.

How to Develop

  1. Request the ohos.permission.NOTIFICATION_CONTROLLER permission. For details, see Requesting Application Permissions.

  2. Import the notificationSubscribe module.

    import { notificationSubscribe, notificationManager } from '@kit.NotificationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    const TAG: string = '[SubscribeOperations]';
    const DOMAIN_NUMBER: number = 0xFF00;
    
  3. Create a subscriber object.

    let subscriber:notificationSubscribe.NotificationSubscriber = {
      onConsume: (data:notificationSubscribe.SubscribeCallbackData) => {
        let req: notificationManager.NotificationRequest = data.request;
        hilog.info(DOMAIN_NUMBER, TAG, `onConsume callback. req.id: ${req.id}`);
      },
      onCancel: (data:notificationSubscribe.SubscribeCallbackData) => {
        let req: notificationManager.NotificationRequest = data.request;
        hilog.info(DOMAIN_NUMBER, TAG, `onCancel callback. req.id: ${req.id}`);
      },
      onUpdate: (data) => {
        hilog.info(DOMAIN_NUMBER, TAG, `onUpdate callback. req.id: ${data.sortedHashCode}`);
      },
      onConnect: () => {
        hilog.info(DOMAIN_NUMBER, TAG, `onConnect callback.`);
      },
      onDisconnect: () => {
        hilog.info(DOMAIN_NUMBER, TAG, `onDisconnect callback.`);
      },
      onDestroy: () => {
        hilog.info(DOMAIN_NUMBER, TAG, `onDestroy callback.`);
      },
    };
    
  4. Initiate notification subscription.

    notificationSubscribe.subscribe(subscriber, (err: BusinessError) => { // This API uses an asynchronous callback to return the result.
      if (err) {
        hilog.error(DOMAIN_NUMBER, TAG, `Failed to subscribe notification. Code is ${err.code}, message is ${err.message}`);
        return;
      }
    });
    

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 notification-enable

openharmony 鸿蒙 notification-overview

openharmony 鸿蒙 notification-with-wantagent

openharmony 鸿蒙 notification-customized-ringtone

openharmony 鸿蒙 notification-subscriber-extension-ability-development-steps

openharmony 鸿蒙 notification-cancel

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 notification-update

openharmony 鸿蒙 live-view-notification-sys

openharmony 鸿蒙 notification-slot

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