1# NotificationSubscriber(系统接口)
2
3作为订阅通知接口[subscribe](./js-apis-notificationSubscribe-sys.md)的入参,提供订阅者接收到新通知、取消通知等的回调方法。
4
5> **说明:**
6>
7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块为系统接口。
10
11## 导入模块
12
13```js
14import { notificationSubscribe } from '@kit.NotificationKit';
15```
16
17## onConsume
18
19onConsume?: (data: SubscribeCallbackData) => void
20
21接收到新通知的回调函数。
22
23**系统能力**:SystemCapability.Notification.Notification
24
25**系统接口**: 此接口为系统接口。
26
27**参数:**
28
29| 参数名 | 类型 | 必填 | 说明 |
30| ------------ | ------------------------ | ---- | -------------------------- |
31| onConsume | (data: [SubscribeCallbackData](#subscribecallbackdata)) => void | 否 | 新接收到的通知信息。 |
32
33**示例:**
34
35```ts
36import { BusinessError } from '@kit.BasicServicesKit';
37
38let subscribeCallback = (err: 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) => void
62
63取消通知的回调函数。
64
65**系统能力**:SystemCapability.Notification.Notification
66
67**系统接口**: 此接口为系统接口。
68
69**参数:**
70
71| 参数名 | 类型 | 必填 | 说明 |
72| ------------ | ------------------------ | ---- | -------------------------- |
73| onCancel | (data: [SubscribeCallbackData](#subscribecallbackdata)) => void | 否 | 需要取消的通知信息。 |
74
75**示例:**
76
77```ts
78import { BusinessError } from '@kit.BasicServicesKit';
79
80let subscribeCallback = (err: 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) => void
104
105更新通知排序的回调函数。
106
107**系统能力**:SystemCapability.Notification.Notification
108
109**系统接口**: 此接口为系统接口。
110
111**参数:**
112
113| 参数名 | 类型 | 必填 | 说明 |
114| ------------ | ------------------------ | ---- | -------------------------- |
115| onUpdate | (data: [NotificationSortingMap](js-apis-inner-notification-notificationSortingMap-sys.md)) => void | 否 | 最新的通知排序列表。 |
116
117**示例:**
118
119```ts
120import { BusinessError } from '@kit.BasicServicesKit';
121
122let subscribeCallback = (err: 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
143订阅完成的回调函数。
144
145**系统能力**:SystemCapability.Notification.Notification
146
147**系统接口**: 此接口为系统接口。
148
149**参数:**
150
151| 参数名 | 类型 | 必填 | 说明 |
152| ------------ | ------------------------ | ---- | -------------------------- |
153| onConnect | () => void | 否 | 订阅完成的回调。 |
154
155**示例:**
156
157```ts
158import { BusinessError } from '@kit.BasicServicesKit';
159
160let subscribeCallback = (err: 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
183取消订阅的回调函数。
184
185**系统能力**:SystemCapability.Notification.Notification
186
187**系统接口**: 此接口为系统接口。
188
189**参数:**
190
191| 参数名 | 类型 | 必填 | 说明 |
192| ------------ | ------------------------ | ---- | -------------------------- |
193| onDisconnect | () => void | 否 | 取消订阅的回调。 |
194
195**示例:**
196
197```ts
198import { BusinessError } from '@kit.BasicServicesKit';
199
200let subscribeCallback = (err: 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: 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// 订阅通知后会收到onConnect回调
228notificationSubscribe.subscribe(subscriber, subscribeCallback);
229// 取消订阅后会收到onDisconnect回调
230notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
231```
232
233## onDestroy
234
235onDestroy?: () => void
236
237服务失联回调函数。
238
239**系统能力**:SystemCapability.Notification.Notification
240
241**系统接口**: 此接口为系统接口。
242
243**参数:**
244
245| 参数名 | 类型 | 必填 | 说明 |
246| ------------ | ------------------------ | ---- | -------------------------- |
247| onDestroy | () => void | 否 | 服务失联的回调。 |
248
249**示例:**
250
251```ts
252import { BusinessError } from '@kit.BasicServicesKit';
253
254let subscribeCallback = (err: BusinessError) => {
255  if (err) {
256    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
257  } else {
258    console.info("subscribeCallback");
259  }
260};
261
262let onDestroyCallback = () => {
263  console.info('===> onDestroy in test');
264}
265
266let subscriber: notificationSubscribe.NotificationSubscriber = {
267  onDestroy: onDestroyCallback
268};
269
270notificationSubscribe.subscribe(subscriber, subscribeCallback);
271```
272
273## onDoNotDisturbDateChange<sup>8+</sup>(deprecated)
274
275onDoNotDisturbDateChange?: (mode: notification.DoNotDisturbDate) => void
276
277免打扰时间选项发生变更时的回调函数。
278
279> **说明:**
280>
281> 此接口从API version 8开始支持,从API version 11开始不再维护,建议使用[onDoNotDisturbChanged](js-apis-inner-notification-notificationSubscriber-sys.md#ondonotdisturbchanged11)代替。
282
283**系统能力**:SystemCapability.Notification.Notification
284
285**系统接口**: 此接口为系统接口。
286
287**参数:**
288
289| 参数名 | 类型 | 必填 | 说明 |
290| ------------ | ------------------------ | ---- | -------------------------- |
291| onDoNotDisturbDateChange | (mode: notification.[DoNotDisturbDate](js-apis-notification-sys.md#donotdisturbdate8-deprecated)) => void | 否 | 回调返回免打扰时间选项变更。 |
292
293**示例:**
294
295```ts
296import { BusinessError } from '@kit.BasicServicesKit';
297
298let subscribeCallback = (err: BusinessError) => {
299  if (err) {
300    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
301  } else {
302    console.info("subscribeCallback");
303  }
304};
305
306let onDoNotDisturbDateChangeCallback = (mode: Notification.DoNotDisturbDate) => {
307  console.info('===> onDoNotDisturbDateChange:' + mode);
308}
309
310let subscriber: notificationSubscribe.NotificationSubscriber = {
311  onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback
312};
313
314notificationSubscribe.subscribe(subscriber, subscribeCallback);
315```
316
317## onDoNotDisturbChanged<sup>11+</sup>
318
319onDoNotDisturbChanged?: (mode: notificationManager.DoNotDisturbDate) => void
320
321免打扰时间选项发生变更时的回调函数。
322
323**系统接口**: 此接口为系统接口。
324
325**系统能力**:SystemCapability.Notification.Notification
326
327**参数:**
328
329| 参数名 | 类型 | 必填 | 说明 |
330| ------------ | ------------------------ | ---- | -------------------------- |
331| onDoNotDisturbChanged | (mode: notificationManager.[DoNotDisturbDate](js-apis-notificationManager-sys.md#donotdisturbdate)) => void | 否 | 回调返回免打扰时间选项变更。 |
332
333**示例:**
334
335```ts
336import { BusinessError } from '@kit.BasicServicesKit';
337import { notificationSubscribe, notificationManager } from '@kit.NotificationKit';
338
339let subscribeCallback = (err: BusinessError) => {
340  if (err) {
341    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
342  } else {
343    console.info("subscribeCallback");
344  }
345};
346
347let onDoNotDisturbChangedCallback = (mode: notificationManager.DoNotDisturbDate) => {
348  console.info('===> onDoNotDisturbChanged:' + JSON.stringify(mode));
349}
350
351let subscriber: notificationSubscribe.NotificationSubscriber = {
352  onDoNotDisturbChanged: onDoNotDisturbChangedCallback
353};
354
355notificationSubscribe.subscribe(subscriber, subscribeCallback);
356```
357
358## onEnabledNotificationChanged<sup>8+</sup>
359
360onEnabledNotificationChanged?: (callbackData: EnabledNotificationCallbackData) => void
361
362监听应用通知使能变化。
363
364**系统能力**:SystemCapability.Notification.Notification
365
366**系统接口**: 此接口为系统接口。
367
368**参数:**
369
370| 参数名 | 类型                                                                                                           | 必填 | 说明 |
371| ------------ |--------------------------------------------------------------------------------------------------------------| ---- | -------------------------- |
372| onEnabledNotificationChanged | (callbackData: [EnabledNotificationCallbackData](#enablednotificationcallbackdata8)) => void | 否 | 回调返回监听到的应用信息。 |
373
374**示例:**
375
376```ts
377import { BusinessError } from '@kit.BasicServicesKit';
378
379let subscribeCallback = (err: BusinessError) => {
380  if (err) {
381    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
382  } else {
383    console.info("subscribeCallback");
384  }
385};
386
387let onEnabledNotificationChangedCallback = (callbackData: notificationSubscribe.EnabledNotificationCallbackData) => {
388  console.info("bundle: ", callbackData.bundle);
389  console.info("uid: ", callbackData.uid);
390  console.info("enable: ", callbackData.enable);
391};
392
393let subscriber: notificationSubscribe.NotificationSubscriber = {
394  onEnabledNotificationChanged: onEnabledNotificationChangedCallback
395};
396
397notificationSubscribe.subscribe(subscriber, subscribeCallback);
398```
399
400## onBadgeChanged<sup>10+</sup>
401
402onBadgeChanged?: (data: BadgeNumberCallbackData) => void
403
404监听应用角标个数变化。
405
406**系统能力**:SystemCapability.Notification.Notification
407
408**系统接口**: 此接口为系统接口。
409
410**参数:**
411
412| 参数名   | 类型                                                         | 必填 | 说明                       |
413| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
414| onBadgeChanged | (data: [BadgeNumberCallbackData](#badgenumbercallbackdata10)) => void | 否   | 回调返回监听到的应用信息。 |
415
416**示例:**
417
418```ts
419import { BusinessError } from '@kit.BasicServicesKit';
420
421let subscribeCallback = (err: BusinessError) => {
422  if (err) {
423    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
424  } else {
425    console.info("subscribeCallback");
426  }
427};
428
429let subscriber: notificationSubscribe.NotificationSubscriber = {
430  onBadgeChanged: (data) => {
431    console.info("bundle: ", data.bundle);
432    console.info("uid: ", data.uid);
433    console.info("badgeNumber: ", data.badgeNumber);
434  }
435};
436
437notificationSubscribe.subscribe(subscriber, subscribeCallback);
438```
439
440## onBadgeEnabledChanged<sup>12+</sup>
441
442onBadgeEnabledChanged?: BadgeEnabledChangedCallback
443
444监听应用角标使能状态变化。
445
446**系统能力**:SystemCapability.Notification.Notification
447
448**系统接口**: 此接口为系统接口。
449
450**参数:**
451
452| 参数名   | 类型                                                         | 必填 | 说明                       |
453| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
454| onBadgeEnabledChanged | [BadgeEnabledChangedCallback](#badgeenabledchangedcallback12) | 否   | 回调应用角标使能状态变化。 |
455
456**示例:**
457
458```ts
459import { BusinessError } from '@kit.BasicServicesKit';
460
461let subscribeCallback = (err: BusinessError) => {
462  if (err) {
463    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
464  } else {
465    console.info('subscribeCallback');
466  }
467};
468
469let BadgeEnabledChangedCallback = (data: notificationSubscribe.EnabledNotificationCallbackData) => {
470  console.info('onBadgeEnabledChanged, badge enabled state change to: ', JSON.stringify(data));
471};
472let subscriber: notificationSubscribe.NotificationSubscriber = {
473  onBadgeEnabledChanged: BadgeEnabledChangedCallback
474};
475
476notificationSubscribe.subscribe(subscriber, subscribeCallback);
477```
478
479
480## onBatchCancel<sup>11+</sup>
481
482onBatchCancel?: (data: Array<SubscribeCallbackData\>) => void
483
484批量删除的回调函数。
485
486**系统能力**:SystemCapability.Notification.Notification
487
488**系统接口**: 此接口为系统接口。
489
490**参数:**
491
492| 参数名   | 类型                                                         | 必填 | 说明                       |
493| -------- | ------------------------------------------------------------ | ---- | -------------------------- |
494| onBatchCancel | (data: Array<[SubscribeCallbackData](#subscribecallbackdata)>) => void | 否   | 批量删除的通知信息。 |
495
496**示例:**
497
498```ts
499import { BusinessError } from '@kit.BasicServicesKit';
500
501let subscribeCallback = (err: BusinessError) => {
502  if (err) {
503    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
504  } else {
505    console.info("subscribeCallback");
506  }
507};
508
509let onBatchCancelCallBack = (data: Array<notificationSubscribe.SubscribeCallbackData>) => {
510  console.info('===> onBatchCancel in test');
511  let req = data[0].request;
512  console.info('===> onBatchCancel callback req.id:' + req.id);
513};
514
515let subscriber: notificationSubscribe.NotificationSubscriber = {
516  onBatchCancel: onBatchCancelCallBack
517};
518
519notificationSubscribe.subscribe(subscriber, subscribeCallback);
520```
521## SubscribeCallbackData
522
523**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification
524
525**系统接口**:此接口为系统接口。
526
527| 名称            | 类型                                                                 | 可读 | 可写 | 说明     |
528| --------------- |--------------------------------------------------------------------| ---- | --- | -------- |
529| request         | [NotificationRequest](js-apis-inner-notification-notificationRequest-sys.md#notificationrequest) | 是  | 否  | 通知内容。 |
530| sortingMap      | [NotificationSortingMap](js-apis-inner-notification-notificationSortingMap-sys.md) | 是  | 否  | 通知排序信息。 |
531| reason          | number                                                             | 是  | 否  | 删除原因(1:点击通知后删除通知,2:用户删除通知) 。|
532| sound           | string                                                             | 是  | 否  | 通知声音。 |
533| vibrationValues | Array\<number\>                                                    | 是  | 否  | 通知震动。 |
534
535
536## EnabledNotificationCallbackData<sup>8+</sup>
537
538**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification
539
540**系统接口**:此接口为系统接口。
541
542| 名称   | 类型    | 可读 | 可写 | 说明             |
543| ------ | ------- | ---- | --- | ---------------- |
544| bundle | string  | 是  | 否  | 应用的包名。       |
545| uid    | number  | 是  | 否  | 应用的uid。        |
546| enable | boolean | 是  | 否  | 应用通知使能状态。 |
547
548
549## BadgeNumberCallbackData<sup>10+</sup>
550
551**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification
552
553**系统接口**:此接口为系统接口。
554
555| 名称        | 类型   | 可读 | 可写 | 说明         |
556| ----------- | ------ | ---- | ---- | ------------ |
557| bundle      | string | 是   | 否   | 应用的包名。 |
558| uid         | number | 是   | 否   | 应用的uid。  |
559| badgeNumber | number | 是   | 否   | 角标个数。   |
560| instanceKey  | number | 是   | 否   | 应用实例键值。   |
561
562
563## BadgeEnabledChangedCallback<sup>12+</sup>
564
565**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification
566
567**系统接口**:此接口为系统接口。
568
569| 名称        | 类型   | 只读 | 可选 | 说明     |
570| ----------- | ------ | ---- | ---- |------------ |
571| data        | [EnabledNotificationCallbackData](#enablednotificationcallbackdata8) | 是   | 是    |   回调返回监听到的角标使能状态信息。 |
572
573