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

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

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.

Available APIs

The major APIs for notification subscription are described as follows. For details about the APIs, see @ohos.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

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.
onDoNotDisturbDateChange?:(mode: notification.DoNotDisturbDate) => void Callback for the Do Not Disturb (DNT) time changes.
onEnabledNotificationChanged?:(callbackData: EnabledNotificationCallbackData) => void Callback for notification switch changes.

How to Develop

  1. Request the ohos.permission.NOTIFICATION_CONTROLLER permission. For details, see Permission Application Guide.

  2. Import the notificationSubscribe module.

   import notificationSubscribe from '@ohos.notificationSubscribe';
   import Base from '@ohos.base';
  1. Create a subscriber object.
   let subscriber:notificationSubscribe.NotificationSubscriber = {
     onConsume: (data:notificationSubscribe.SubscribeCallbackData) => {
       let req = data.request;
       console.info(`onConsume callback. req.id: ${req.id}`);
     },
     onCancel: (data:notificationSubscribe.SubscribeCallbackData) => {
       let req = data.request;
       console.info(`onCancel callback. req.id: ${req.id}`);
     },
     onUpdate: (data) => {
       console.info(`onUpdate callback. req.id: ${data.sortedHashCode}`);
     },
     onConnect: () => {
       console.info(`onConnect callback.}`);
     },
     onDisconnect: () => {
       console.info(`onDisconnect callback.}`);
     },
     onDestroy: () => {
       console.info(`onDestroy callback.}`);
     },
   };
  1. Initiate notification subscription.
   notificationSubscribe.subscribe(subscriber, (err:Base.BusinessError) => { // This API uses an asynchronous callback to return the result.
     if (err) {
       console.error(`Failed to subscribe notification. Code is ${err.code}, message is ${err.message}`);
       return;
     }
   });

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Notification

harmony 鸿蒙Managing the Notification Badge

harmony 鸿蒙Enabling Notification

harmony 鸿蒙Notification Overview

harmony 鸿蒙Adding a WantAgent Object to a Notification

harmony 鸿蒙Publishing a Progress Notification

harmony 鸿蒙Publishing a Basic Notification

0  赞