• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Notification Subscription (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** object to provide callbacks for subscription events, such as subscription success, notification reception, notification cancellation, and subscription cancellation.
8
9
10## Available APIs
11
12The major APIs for notification subscription are described as follows. For details about the APIs, see [@ohos.notificationSubscribe](../reference/apis/js-apis-notificationSubscribe.md).
13
14**Table 1** Major APIs for notification subscription
15
16| Name | Description|
17| -------- | -------- |
18| subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback<void>): void | Subscribes to notifications from a specific application.|
19| subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback<void>): void                                  | Subscribes to notifications from all applications.    |
20
21**Table 2** Callbacks for notification subscription
22
23| Name | Description|
24| -------- | -------- |
25| onConsume?:(data: SubscribeCallbackData) => void                                      | Callback for receiving notifications.              |
26| onCancel?:(data: SubscribeCallbackData) => void                                       | Callback for canceling notifications.          |
27| onUpdate?:(data: NotificationSortingMap) => void                                      | Callback for notification sorting updates.      |
28| onConnect?:() => void;                                                                | Callback for subscription.          |
29| onDisconnect?:() => void;                                                             | Callback for unsubscription.          |
30| onDestroy?:() => void                                                                 | Callback for disconnecting from the notification subsystem.  |
31| onDoNotDisturbDateChange?:(mode: notification.DoNotDisturbDate) => void               | Callback for the Do Not Disturb (DNT) time changes.|
32| onEnabledNotificationChanged?:(callbackData: EnabledNotificationCallbackData) => void | Callback for notification switch changes.      |
33
34
35## How to Develop
36
371. Request the **ohos.permission.NOTIFICATION_CONTROLLER** permission. For details, see [Permission Application Guide](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file).
38
392. Import the **notificationSubscribe** module.
40
41   ```ts
42   import notificationSubscribe from '@ohos.notificationSubscribe';
43   ```
44
453. Create a **subscriber** object.
46
47   ```ts
48   let subscriber = {
49       onConsume: function (data) {
50           let req = data.request;
51           console.info('[ANS] onConsume callback req.id: ' + req.id);
52       },
53       onCancel: function (data) {
54           let req = data.request;
55           console.info('[ANS] onCancel callback req.id: : ' + req.id);
56       },
57       onUpdate: function (data) {
58           console.info('[ANS] onUpdate in test');
59       },
60       onConnect: function () {
61           console.info('[ANS] onConnect in test');
62       },
63       onDisconnect: function () {
64           console.info('[ANS] onDisConnect in test');
65       },
66       onDestroy: function () {
67           console.info('[ANS] onDestroy in test');
68       },
69   };
70   ```
71
724. Initiate notification subscription.
73
74   ```ts
75   notificationSubscribe.subscribe(subscriber, (err, data) => { // This API uses an asynchronous callback to return the result.
76     if (err) {
77       console.error(`[ANS] failed to subscribe, error[${err}]`);
78       return;
79     }
80     console.info(`[ANS] subscribeTest success : + ${data}`);
81   });
82   ```
83