• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# NotificationSubscriber
2
3The **NotificationSubscriber** module provides callbacks for receiving or removing notifications and serves as the input parameter of [subscribe](js-apis-notificationSubscribe.md).
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```js
12import notificationSubscribe from '@ohos.notificationSubscribe';
13```
14
15**System API**: This is a system API and cannot be called by third-party applications.
16
17## onConsume
18
19onConsume?: (data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void
20
21Called when a new notification is received.
22
23**System capability**: SystemCapability.Notification.Notification
24
25**System API**: This is a system API and cannot be called by third-party applications.
26
27**Parameters**
28
29| Name| Type| Mandatory| Description|
30| ------------ | ------------------------ | ---- | -------------------------- |
31| onConsume | (data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void | Yes| Information about the notification received.|
32
33**Example**
34
35```ts
36import Base from '@ohos.base';
37
38let subscribeCallback = (err: Base.BusinessError) => {
39  if (err) {
40    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
41  } else {
42    console.info("subscribeCallback");
43  }
44};
45
46let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
47  console.info('===> onConsume in test');
48  let req = data.request;
49  console.info('===> onConsume callback req.id:' + req.id);
50};
51
52let subscriber: notificationSubscribe.NotificationSubscriber = {
53  onConsume: onConsumeCallback
54};
55
56notificationSubscribe.subscribe(subscriber, subscribeCallback);
57```
58
59## onCancel
60
61onCancel?:(data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void
62
63Called when a notification is canceled.
64
65**System capability**: SystemCapability.Notification.Notification
66
67**System API**: This is a system API and cannot be called by third-party applications.
68
69**Parameters**
70
71| Name| Type| Mandatory| Description|
72| ------------ | ------------------------ | ---- | -------------------------- |
73| onCancel | (data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void | Yes| Information about the notification to cancel.|
74
75**Example**
76
77```ts
78import Base from '@ohos.base';
79
80let subscribeCallback = (err: Base.BusinessError) => {
81  if (err) {
82    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
83  } else {
84    console.info("subscribeCallback");
85  }
86};
87
88let onCancelCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
89  console.info('===> onCancel in test');
90  let req = data.request;
91  console.info('===> onCancel callback req.id:' + req.id);
92}
93
94let subscriber: notificationSubscribe.NotificationSubscriber = {
95  onCancel: onCancelCallback
96};
97
98notificationSubscribe.subscribe(subscriber, subscribeCallback);
99```
100
101## onUpdate
102
103onUpdate?:(data: [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) => void
104
105Called when notification sorting is updated.
106
107**System capability**: SystemCapability.Notification.Notification
108
109**System API**: This is a system API and cannot be called by third-party applications.
110
111**Parameters**
112
113| Name| Type| Mandatory| Description|
114| ------------ | ------------------------ | ---- | -------------------------- |
115| onUpdate | (data: [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) => void | Yes| Latest notification sorting list.|
116
117**Example**
118
119```ts
120import Base from '@ohos.base';
121
122let subscribeCallback = (err: Base.BusinessError) => {
123  if (err) {
124    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
125  } else {
126    console.info("subscribeCallback");
127  }
128};
129
130let subscriber: notificationSubscribe.NotificationSubscriber = {
131  onUpdate: (map) => {
132    console.info('===> onUpdateCallback map:' + JSON.stringify(map));
133  }
134};
135
136notificationSubscribe.subscribe(subscriber, subscribeCallback);
137```
138
139## onConnect
140
141onConnect?:() => void
142
143Called when the subscription is complete.
144
145**System capability**: SystemCapability.Notification.Notification
146
147**System API**: This is a system API and cannot be called by third-party applications.
148
149**Parameters**
150
151| Name| Type| Mandatory| Description|
152| ------------ | ------------------------ | ---- | -------------------------- |
153| onConnect | () => void | Yes| Callback invoked when the subscription is complete.|
154
155**Example**
156
157```ts
158import Base from '@ohos.base';
159
160let subscribeCallback = (err: Base.BusinessError) => {
161  if (err) {
162    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
163  } else {
164    console.info("subscribeCallback");
165  }
166};
167
168let onConnectCallback = () => {
169  console.info('===> onConnect in test');
170}
171
172let subscriber: notificationSubscribe.NotificationSubscriber = {
173  onConnect: onConnectCallback
174};
175
176notificationSubscribe.subscribe(subscriber, subscribeCallback);
177```
178
179## onDisconnect
180
181onDisconnect?:() => void
182
183Called when unsubscription is complete.
184
185**System capability**: SystemCapability.Notification.Notification
186
187**System API**: This is a system API and cannot be called by third-party applications.
188
189**Parameters**
190
191| Name| Type| Mandatory| Description|
192| ------------ | ------------------------ | ---- | -------------------------- |
193| onDisconnect | () => void | Yes| Callback invoked when unsubscription is complete.|
194
195**Example**
196
197```ts
198import Base from '@ohos.base';
199
200let subscribeCallback = (err: Base.BusinessError) => {
201  if (err) {
202    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
203  } else {
204    console.info("subscribeCallback");
205  }
206};
207let unsubscribeCallback = (err: Base.BusinessError) => {
208  if (err) {
209    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
210  } else {
211    console.info("unsubscribeCallback");
212  }
213};
214
215let onConnectCallback = () => {
216  console.info('===> onConnect in test');
217}
218let onDisconnectCallback = () => {
219  console.info('===> onDisconnect in test');
220}
221
222let subscriber: notificationSubscribe.NotificationSubscriber = {
223  onConnect: onConnectCallback,
224  onDisconnect: onDisconnectCallback
225};
226
227// The onConnect callback is invoked when subscription to the notification is complete.
228notificationSubscribe.subscribe(subscriber, subscribeCallback);
229// The onDisconnect callback is invoked when unsubscription to the notification is complete.
230notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
231```
232
233## onDestroy
234
235onDestroy?:() => void
236
237Called when the service is disconnected.
238
239**System capability**: SystemCapability.Notification.Notification
240
241**System API**: This is a system API and cannot be called by third-party applications.
242
243**Parameters**
244
245| Name| Type| Mandatory| Description|
246| ------------ | ------------------------ | ---- | -------------------------- |
247| onDestroy | () => void | Yes| Callback invoked when the service is disconnected.|
248**Example**
249
250```ts
251import Base from '@ohos.base';
252
253let subscribeCallback = (err: Base.BusinessError) => {
254  if (err) {
255    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
256  } else {
257    console.info("subscribeCallback");
258  }
259};
260
261let onDestroyCallback = () => {
262  console.info('===> onDestroy in test');
263}
264
265let subscriber: notificationSubscribe.NotificationSubscriber = {
266  onDestroy: onDestroyCallback
267};
268
269notificationSubscribe.subscribe(subscriber, subscribeCallback);
270```
271
272## onDoNotDisturbDateChange<sup>8+</sup>
273
274onDoNotDisturbDateChange?:(mode: notificationManager.[DoNotDisturbDate](js-apis-notificationManager.md#donotdisturbdate)) => void
275
276Called when the DND time settings are changed.
277
278**System capability**: SystemCapability.Notification.Notification
279
280**System API**: This is a system API and cannot be called by third-party applications.
281
282**Parameters**
283
284| Name| Type| Mandatory| Description|
285| ------------ | ------------------------ | ---- | -------------------------- |
286| onDoNotDisturbDateChange | (mode: notificationManager.[DoNotDisturbDate](js-apis-notificationManager.md#donotdisturbdate)) => void | Yes| DND time setting updates.|
287
288**Example**
289
290```ts
291import Base from '@ohos.base';
292import NotificationManager from '@ohos.notificationManager';
293
294let subscribeCallback = (err: Base.BusinessError) => {
295  if (err) {
296    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
297  } else {
298    console.info("subscribeCallback");
299  }
300};
301
302let onDoNotDisturbDateChangeCallback = (mode: NotificationManager.DoNotDisturbDate) => {
303  console.info('===> onDoNotDisturbDateChange:' + mode);
304}
305
306let subscriber: notificationSubscribe.NotificationSubscriber = {
307  onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback
308};
309
310notificationSubscribe.subscribe(subscriber, subscribeCallback);
311```
312
313
314## onEnabledNotificationChanged<sup>8+</sup>
315
316onEnabledNotificationChanged?:(callbackData: [EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata8)) => void
317
318Listens for the notification enabled status changes.
319
320**System capability**: SystemCapability.Notification.Notification
321
322**System API**: This is a system API and cannot be called by third-party applications.
323
324**Parameters**
325
326| Name| Type                                                                                                          | Mandatory| Description|
327| ------------ |--------------------------------------------------------------------------------------------------------------| ---- | -------------------------- |
328| onEnabledNotificationChanged | (callbackData: [EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata8)) => void | Yes| Callback used to return the result.|
329
330**Example**
331
332```ts
333import Base from '@ohos.base';
334
335let subscribeCallback = (err: Base.BusinessError) => {
336  if (err) {
337    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
338  } else {
339    console.info("subscribeCallback");
340  }
341};
342
343let onEnabledNotificationChangedCallback = (callbackData: notificationSubscribe.EnabledNotificationCallbackData) => {
344  console.info("bundle: ", callbackData.bundle);
345  console.info("uid: ", callbackData.uid);
346  console.info("enable: ", callbackData.enable);
347};
348
349let subscriber: notificationSubscribe.NotificationSubscriber = {
350  onEnabledNotificationChanged: onEnabledNotificationChangedCallback
351};
352
353notificationSubscribe.subscribe(subscriber, subscribeCallback);
354```
355
356## onBadgeChanged<sup>10+</sup>
357
358 onBadgeChanged?:(data: [BadgeNumberCallbackData](#badgenumbercallbackdata10)) => void
359
360Listens for the change of the notification badge number.
361
362**System capability**: SystemCapability.Notification.Notification
363
364**System API**: This is a system API and cannot be called by third-party applications.
365
366**Parameters**
367
368| Name  | Type                                                        | Mandatory| Description                      |
369| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
370| onBadgeChanged | (data: [BadgeNumberCallbackData](#badgenumbercallbackdata10)) => void | Yes  | Callback used to return the result.|
371
372**Example**
373
374```ts
375import Base from '@ohos.base';
376
377let subscribeCallback = (err: Base.BusinessError) => {
378  if (err) {
379    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
380  } else {
381    console.info("subscribeCallback");
382  }
383};
384
385let subscriber: notificationSubscribe.NotificationSubscriber = {
386  onBadgeChanged: (data) => {
387    console.info("bundle: ", data.bundle);
388    console.info("uid: ", data.uid);
389    console.info("badgeNumber: ", data.badgeNumber);
390  }
391};
392
393notificationSubscribe.subscribe(subscriber, subscribeCallback);
394```
395
396## SubscribeCallbackData
397
398**System capability**: SystemCapability.Notification.Notification
399
400**System API**: This is a system API and cannot be called by third-party applications.
401
402| Name           | Type                                                                | Readable| Writable| Description    |
403| --------------- |--------------------------------------------------------------------| ---- | --- | -------- |
404| request         | [NotificationRequest](js-apis-notification.md#notificationrequest) | Yes | No | Notification content.|
405| sortingMap      | [NotificationSortingMap](js-apis-inner-notification-notificationSortingMap.md) | Yes | No | Notification sorting information.|
406| reason          | number                                                             | Yes | No | Reason for deletion. The options are as follows:<br>**1**: The notification is deleted after being clicked.<br>**2**: The notification is deleted by the user.|
407| sound           | string                                                             | Yes | No | Sound used for notification.|
408| vibrationValues | Array\<number\>                                                    | Yes | No | Vibration used for notification.|
409
410
411## EnabledNotificationCallbackData<sup>8+</sup>
412
413**System capability**: SystemCapability.Notification.Notification
414
415**System API**: This is a system API and cannot be called by third-party applications.
416
417| Name  | Type   | Readable| Writable| Description            |
418| ------ | ------- | ---- | --- | ---------------- |
419| bundle | string  | Yes | No | Bundle name of the application.      |
420| uid    | number  | Yes | No | UID of the application.       |
421| enable | boolean | Yes | No | Notification enabled status of the application.|
422
423
424## BadgeNumberCallbackData<sup>10+</sup>
425
426**System capability**: SystemCapability.Notification.Notification
427
428**System API**: This is a system API and cannot be called by third-party applications.
429
430| Name       | Type  | Readable| Writable| Description        |
431| ----------- | ------ | ---- | ---- | ------------ |
432| bundle      | string | Yes  | No  | Bundle name of the application.|
433| uid         | number | Yes  | No  | UID of the application. |
434| badgeNumber | number | Yes  | No  | Number of notifications displayed on the application icon.  |
435