1# Subscribing to Notifications (for System Applications Only)
2
3
4To 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.
5
6
7You can use the [NotificationSubscriber](../reference/apis-notification-kit/js-apis-inner-notification-notificationSubscriber-sys.md) object to provide callbacks for subscription events, such as subscription success, notification reception, notification cancellation, and subscription cancellation.
8
9## Principle of Notification Subscription
10
11The 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)](../ipc/ipc-rpc-overview.md). The notification subsystem then distributes the notification to the notification subscriber.
12
13* Notification sender: It can be a third-party application or a system application. Pay special attention to this role.
14
15* 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.
16
17**Figure 1** Notification service process
18
19![notification_internal_principle](figures/notification_internal_principle.png)
20
21
22## Available APIs
23
24The major APIs for notification subscription are described as follows. For details about the APIs, see [@ohos.notificationSubscribe (NotificationSubscribe)](../reference/apis-notification-kit/js-apis-notificationSubscribe-sys.md).
25
26**Table 1** Major APIs for notification subscription
27
28| API| Description|
29| -------- | -------- |
30| subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback<void>): void | Subscribes to notifications from a specific application.|
31| subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback<void>): void | Subscribes to notifications from all applications.    |
32
33**Table 2** Callbacks for notification subscription
34
35For details about the API, see [NotificationSubscriber](../reference/apis-notification-kit/js-apis-inner-notification-notificationSubscriber-sys.md).
36
37| API| Description|
38| -------- | -------- |
39| onConsume?: (data: SubscribeCallbackData) => void  | Callback for receiving notifications.              |
40| onCancel?: (data: SubscribeCallbackData) => void   | Callback for canceling notifications.          |
41| onUpdate?: (data: NotificationSortingMap) => void  | Callback for notification sorting updates.      |
42| onConnect?: () => void;                                 | Callback for subscription.          |
43| onDisconnect?: () => void;                              | Callback for unsubscription.          |
44| onDestroy?: () => void                                  | Callback for disconnecting from the notification subsystem.  |
45| onDoNotDisturbDateChange<sup>deprecated</sup>?: (mode:&nbsp;notification.DoNotDisturbDate<sup>deprecated</sup>)&nbsp;=&gt;&nbsp;void | Callback for the Do Not Disturb (DNT) time changes. This API is deprecated since API version 11.|
46| onDoNotDisturbChanged?: (mode:&nbsp;notificationManager.DoNotDisturbDate)&nbsp;=&gt;&nbsp;void           | Callback for the Do Not Disturb (DNT) time changes.|
47| onEnabledNotificationChanged?: (callbackData:&nbsp;EnabledNotificationCallbackData)&nbsp;=&gt;&nbsp;void | Callback for notification switch changes.      |
48| onBadgeChanged?: (data:&nbsp;BadgeNumberCallbackData)&nbsp;=&gt;&nbsp;void                               | Callback for notification badge number changes.  |
49
50
51## How to Develop
52
531. Request the **ohos.permission.NOTIFICATION_CONTROLLER** permission. For details, see [Requesting Application Permissions](https://gitee.com/openharmony/docs/blob/master/en/application-dev/security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications).
54
552. Import the **notificationSubscribe** module.
56
57   ```ts
58   import { notificationSubscribe } from '@kit.NotificationKit';
59   import { BusinessError } from '@kit.BasicServicesKit';
60   import { hilog } from '@kit.PerformanceAnalysisKit';
61
62   const TAG: string = '[SubscribeOperations]';
63   const DOMAIN_NUMBER: number = 0xFF00;
64   ```
65
663. Create a **subscriber** object.
67
68   ```ts
69   let subscriber:notificationSubscribe.NotificationSubscriber = {
70     onConsume: (data:notificationSubscribe.SubscribeCallbackData) => {
71       let req: notificationManager.NotificationRequest = data.request;
72       hilog.info(DOMAIN_NUMBER, TAG, `onConsume callback. req.id: ${req.id}`);
73     },
74     onCancel: (data:notificationSubscribe.SubscribeCallbackData) => {
75       let req: notificationManager.NotificationRequest = data.request;
76       hilog.info(DOMAIN_NUMBER, TAG, `onCancel callback. req.id: ${req.id}`);
77     },
78     onUpdate: (data) => {
79       hilog.info(DOMAIN_NUMBER, TAG, `onUpdate callback. req.id: ${data.sortedHashCode}`);
80     },
81     onConnect: () => {
82       hilog.info(DOMAIN_NUMBER, TAG, `onConnect callback.`);
83     },
84     onDisconnect: () => {
85       hilog.info(DOMAIN_NUMBER, TAG, `onDisconnect callback.`);
86     },
87     onDestroy: () => {
88       hilog.info(DOMAIN_NUMBER, TAG, `onDestroy callback.`);
89     },
90   };
91   ```
92
934. Initiate notification subscription.
94
95   ```ts
96   notificationSubscribe.subscribe(subscriber, (err: BusinessError) => { // This API uses an asynchronous callback to return the result.
97     if (err) {
98       hilog.error(DOMAIN_NUMBER, TAG, `Failed to subscribe notification. Code is ${err.code}, message is ${err.message}`);
99       return;
100     }
101   });
102   ```
103