harmony 鸿蒙Subscribing to Notifications (for System Applications Only)

  • 2023-02-03
  • 浏览 (741)

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

Table 1 Major APIs for notification subscription

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

Table 2 Callbacks for notification subscription

For details about the API, see NotificationSubscriber.

API Description
onConsume?: (data: SubscribeCallbackData) => void Callback for receiving notifications.
onCancel?: (data: SubscribeCallbackData) => void Callback for canceling notifications.
onUpdate?: (data: NotificationSortingMap) => void Callback for notification sorting updates.
onConnect?: () => void; Callback for subscription.
onDisconnect?: () => void; Callback for unsubscription.
onDestroy?: () => void Callback for disconnecting from the notification subsystem.
onDoNotDisturbDateChangedeprecated?: (mode: notification.DoNotDisturbDatedeprecated) => void Callback for the Do Not Disturb (DNT) time changes. This API is deprecated since API version 11.
onDoNotDisturbChanged?: (mode: notificationManager.DoNotDisturbDate) => void Callback for the Do Not Disturb (DNT) time changes.
onEnabledNotificationChanged?: (callbackData: EnabledNotificationCallbackData) => void Callback for notification switch changes.
onBadgeChanged?: (data: BadgeNumberCallbackData) => void Callback 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;
  1. 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.`);
     },
   };
  1. 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;
     }
   });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Notification Kit (User Notification Service)

harmony 鸿蒙Publishing a Live View Notification (for System Applications Only)

harmony 鸿蒙Managing the Notification Badge

harmony 鸿蒙Canceling a Notification

harmony 鸿蒙Clearing Repeated Notifications Across Devices

harmony 鸿蒙Cross-Device Notification Management (for System Applications Only)

harmony 鸿蒙Cross-Device Notification Overview

harmony 鸿蒙Requesting Notification Authorization

harmony 鸿蒙Introduction to Notification Kit

harmony 鸿蒙Enabling Quick Reply for Cross-device Notifications

0  赞