1# @ohos.notificationSubscribe (NotificationSubscribe模块)(系统接口)
2
3本模块提供通知订阅、取消订阅、通知移除等,一般情况下,只有系统应用具有这些操作权限。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块接口均为系统接口。
10
11## 导入模块
12
13```ts
14import { notificationSubscribe } from '@kit.NotificationKit';
15```
16
17## notificationSubscribe.subscribe
18
19subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback\<void\>): void
20
21订阅通知并指定订阅信息。使用callback异步回调。
22
23**系统能力**:SystemCapability.Notification.Notification
24
25**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
26
27**系统接口**: 此接口为系统接口。
28
29**参数:**
30
31| 参数名       | 类型                      | 必填 | 说明             |
32| ---------- | ------------------------- | ---- | ---------------- |
33| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber)    | 是   | 通知订阅对象。     |
34| info       | [NotificationSubscribeInfo](js-apis-inner-notification-notificationSubscribeInfo-sys.md#notificationsubscribeinfo) | 是   | 通知订阅信息。 |
35| callback   | AsyncCallback\<void\>     | 是   | 订阅动作回调函数。 |
36
37**错误码:**
38
39以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
40
41| 错误码ID | 错误信息                             |
42| -------- | ----------------------------------- |
43| 201      | Permission denied.     |
44| 202      | Not system application to call the interface.                                      |
45| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
46| 1600001  | Internal error.                     |
47| 1600002  | Marshalling or unmarshalling error. |
48| 1600003  | Failed to connect to the service.          |
49| 1600012  | No memory space.                    |
50
51**示例:**
52
53```ts
54import { BusinessError } from '@kit.BasicServicesKit';
55
56//subscribe回调
57let subscribeCallback = (err: BusinessError) => {
58  if (err) {
59    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
60  } else {
61    console.info("subscribe success");
62  }
63}
64let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
65  console.info("Consume callback: " + JSON.stringify(data));
66}
67let subscriber: notificationSubscribe.NotificationSubscriber = {
68  onConsume: onConsumeCallback
69};
70//不会对bundleNames进行校验,开发者自己确定需要订阅哪些bundleName
71let info: notificationSubscribe.NotificationSubscribeInfo = {
72  bundleNames: ["bundleName1","bundleName2"]
73};
74notificationSubscribe.subscribe(subscriber, info, subscribeCallback);
75```
76
77## notificationSubscribe.subscribe
78
79subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void
80
81订阅当前用户下所有应用的通知。使用callback异步回调。
82
83**系统能力**:SystemCapability.Notification.Notification
84
85**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
86
87**系统接口**: 此接口为系统接口。
88
89**参数:**
90
91| 参数名       | 类型                   | 必填 | 说明             |
92| ---------- | ---------------------- | ---- | ---------------- |
93| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是   | 通知订阅对象。     |
94| callback   | AsyncCallback\<void\>  | 是   | 订阅动作回调函数。 |
95
96**错误码:**
97
98以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
99
100| 错误码ID | 错误信息                            |
101| -------- | ----------------------------------- |
102| 201      | Permission denied.     |
103| 202      | Not system application to call the interface.                                      |
104| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
105| 1600001  | Internal error.                     |
106| 1600002  | Marshalling or unmarshalling error. |
107| 1600003  | Failed to connect to the service.          |
108| 1600012  | No memory space.                    |
109
110**示例:**
111
112```ts
113import { BusinessError } from '@kit.BasicServicesKit';
114
115let subscribeCallback = (err: BusinessError) => {
116  if (err) {
117    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
118  } else {
119    console.info("subscribe success");
120  }
121}
122let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
123  console.info("Consume callback: " + JSON.stringify(data));
124}
125let subscriber: notificationSubscribe.NotificationSubscriber = {
126  onConsume: onConsumeCallback
127};
128notificationSubscribe.subscribe(subscriber, subscribeCallback);
129```
130
131
132
133## notificationSubscribe.subscribe
134
135subscribe(subscriber: NotificationSubscriber, info?: NotificationSubscribeInfo): Promise\<void\>
136
137订阅通知并指定订阅信息。使用Promise异步回调。
138
139**系统能力**:SystemCapability.Notification.Notification
140
141**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
142
143**系统接口**: 此接口为系统接口。
144
145**参数:**
146
147| 参数名       | 类型                      | 必填 | 说明         |
148| ---------- | ------------------------- | ---- | ------------ |
149| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber)    | 是   | 通知订阅对象。 |
150| info       | [NotificationSubscribeInfo](js-apis-inner-notification-notificationSubscribeInfo-sys.md#notificationsubscribeinfo) | 否   | 通知订阅信息,默认为空(当为空时,表示订阅当前用户下所有应用的通知,否则表示订阅通知并指定订阅信息)。   |
151
152**返回值:**
153
154| 类型     | 说明               |
155| ------- |------------------|
156| Promise\<void\> | 无返回结果的Promise对象。 |
157
158**错误码:**
159
160以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
161
162| 错误码ID | 错误信息                            |
163| -------- | ----------------------------------- |
164| 201      | Permission denied.     |
165| 202      | Not system application to call the interface.                                      |
166| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
167| 1600001  | Internal error.                     |
168| 1600002  | Marshalling or unmarshalling error. |
169| 1600003  | Failed to connect to the service.          |
170| 1600012  | No memory space.                    |
171
172**示例:**
173
174```ts
175import { BusinessError } from '@kit.BasicServicesKit';
176
177let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
178  console.info("Consume callback: " + JSON.stringify(data));
179}
180let subscriber: notificationSubscribe.NotificationSubscriber = {
181  onConsume: onConsumeCallback
182};
183notificationSubscribe.subscribe(subscriber).then(() => {
184  console.info("subscribe success");
185}).catch((err: BusinessError) => {
186  console.error("subscribe fail: " + JSON.stringify(err));
187});
188```
189
190
191## notificationSubscribe.subscribeSelf<sup>11+</sup>
192
193subscribeSelf(subscriber: NotificationSubscriber): Promise\<void\>
194
195订阅本应用的通知并指定订阅信息。使用Promise异步回调。
196
197**系统能力**:SystemCapability.Notification.Notification
198
199**系统接口**: 此接口为系统接口。
200
201**参数:**
202
203| 参数名       | 类型                      | 必填 | 说明         |
204| ---------- | ------------------------- | ---- | ------------ |
205| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber)    | 是   | 通知订阅对象。 |
206
207**返回值:**
208
209| 类型     | 说明               |
210| ------- |------------------|
211| Promise\<void\> | 无返回结果的Promise对象。 |
212
213**错误码:**
214
215以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
216
217| 错误码ID | 错误信息                            |
218| -------- | ----------------------------------- |
219| 202      | Not system application to call the interface.                                      |
220| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
221| 1600001  | Internal error.                     |
222| 1600002  | Marshalling or unmarshalling error. |
223| 1600003  | Failed to connect to the service.          |
224| 1600012  | No memory space.                    |
225
226**示例:**
227
228```ts
229import { BusinessError } from '@kit.BasicServicesKit';
230
231let onConsumeCallback = (data: notificationSubscribe.SubscribeCallbackData) => {
232  console.info("Consume callback: " + JSON.stringify(data));
233}
234let subscriber: notificationSubscribe.NotificationSubscriber = {
235  onConsume: onConsumeCallback
236};
237notificationSubscribe.subscribeSelf(subscriber).then(() => {
238  console.info("subscribeSelf success");
239}).catch((err: BusinessError) => {
240  console.error("subscribeSelf fail: " + JSON.stringify(err));
241});
242```
243
244
245
246## notificationSubscribe.unsubscribe
247
248unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void
249
250取消订阅。使用callback异步回调。
251
252**系统能力**:SystemCapability.Notification.Notification
253
254**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
255
256**系统接口**: 此接口为系统接口。
257
258**参数:**
259
260| 参数名       | 类型                   | 必填 | 说明                 |
261| ---------- | ---------------------- | ---- | -------------------- |
262| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是   | 通知订阅对象。         |
263| callback   | AsyncCallback\<void\>  | 是   | 取消订阅动作回调函数。 |
264
265**错误码:**
266
267以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
268
269| 错误码ID | 错误信息                            |
270| -------- | ----------------------------------- |
271| 201      | Permission denied.     |
272| 202      | Not system application to call the interface.                                      |
273| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
274| 1600001  | Internal error.                     |
275| 1600002  | Marshalling or unmarshalling error. |
276| 1600003  | Failed to connect to the service.          |
277
278**示例:**
279
280```ts
281import { BusinessError } from '@kit.BasicServicesKit';
282
283let unsubscribeCallback = (err: BusinessError) => {
284  if (err) {
285    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
286  } else {
287    console.info("unsubscribe success");
288  }
289}
290let onDisconnectCallback = () => {
291  console.info("subscribe disconnect");
292}
293let subscriber: notificationSubscribe.NotificationSubscriber = {
294  onDisconnect: onDisconnectCallback
295};
296notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
297```
298
299## notificationSubscribe.unsubscribe
300
301unsubscribe(subscriber: NotificationSubscriber): Promise\<void\>
302
303取消订阅。使用Promise异步回调。
304
305**系统能力**:SystemCapability.Notification.Notification
306
307**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
308
309**系统接口**: 此接口为系统接口。
310
311**参数:**
312
313| 参数名       | 类型                   | 必填 | 说明         |
314| ---------- | ---------------------- | ---- | ------------ |
315| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | 是   | 通知订阅对象。 |
316
317**返回值:**
318
319| 类型     | 说明         |
320| ------- |------------|
321| Promise\<void\> | 无返回结果的Promise对象。 |
322
323**错误码:**
324
325以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
326
327| 错误码ID | 错误信息                            |
328| -------- | ----------------------------------- |
329| 201      | Permission denied.     |
330| 202      | Not system application to call the interface.                                      |
331| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
332| 1600001  | Internal error.                     |
333| 1600002  | Marshalling or unmarshalling error. |
334| 1600003  | Failed to connect to the service.          |
335
336**示例:**
337
338```ts
339import { BusinessError } from '@kit.BasicServicesKit';
340
341let onDisconnectCallback = () => {
342  console.info("subscribe disconnect");
343}
344let subscriber: notificationSubscribe.NotificationSubscriber = {
345  onDisconnect: onDisconnectCallback
346};
347notificationSubscribe.unsubscribe(subscriber).then(() => {
348  console.info("unsubscribe success");
349}).catch((err: BusinessError) => {
350  console.error("unsubscribe fail: " + JSON.stringify(err));
351});
352```
353
354## notificationSubscribe.remove
355
356remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason, callback: AsyncCallback\<void\>): void
357
358根据应用的包信息和通知键值,删除指定通知。使用callback异步回调。
359
360**系统能力**:SystemCapability.Notification.Notification
361
362**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
363
364**系统接口**: 此接口为系统接口。
365
366**参数:**
367
368| 参数名            | 类型                                | 必填 | 说明                 |
369| --------------- |   ----------------------------------| ---- | -------------------- |
370| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)       | 是   | 指定应用的包信息。           |
371| notificationKey | [NotificationKey](#notificationkey) | 是   | 通知键值。             |
372| reason          | [RemoveReason](#removereason)      | 是   | 通知删除原因。         |
373| callback        | AsyncCallback\<void\>               | 是   | 删除指定通知回调函数。 |
374
375**错误码:**
376
377以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
378
379| 错误码ID | 错误信息                                 |
380| -------- | ---------------------------------------- |
381| 201      | Permission denied.     |
382| 202      | Not system application to call the interface.                                      |
383| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
384| 1600001  | Internal error.                          |
385| 1600002  | Marshalling or unmarshalling error.      |
386| 1600003  | Failed to connect to the service.               |
387| 1600007  | The notification is not exist.           |
388| 17700001 | The specified bundle name was not found. |
389
390**示例:**
391
392```ts
393import { BusinessError } from '@kit.BasicServicesKit';
394import { notificationManager } from '@kit.NotificationKit';
395
396let removeCallback = (err: BusinessError) => {
397  if (err) {
398    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
399  } else {
400    console.info("remove success");
401  }
402}
403let bundle: notificationManager.BundleOption = {
404  bundle: "bundleName1",
405};
406let notificationKey: notificationSubscribe.NotificationKey = {
407  id: 0,
408  label: "label",
409};
410let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
411notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback);
412```
413
414
415
416## notificationSubscribe.remove
417
418remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason): Promise\<void\>
419
420根据应用的包信息和通知键值,删除指定通知。使用Promise异步回调。
421
422**系统能力**:SystemCapability.Notification.Notification
423
424**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
425
426**系统接口**: 此接口为系统接口。
427
428**参数:**
429
430| 参数名            | 类型            | 必填 | 说明       |
431| --------------- | --------------- | ---- | ---------- |
432| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)    | 是   | 指定应用的包信息。 |
433| notificationKey | [NotificationKey](#notificationkey) | 是   | 通知键值。   |
434| reason          | [RemoveReason](#removereason) | 是   | 通知删除原因。         |
435
436**返回值:**
437
438| 类型     | 说明         |
439| ------- |------------|
440| Promise\<void\> | 无返回结果的Promise对象。 |
441
442**错误码:**
443
444以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
445
446| 错误码ID | 错误信息                                 |
447| -------- | ---------------------------------------- |
448| 201      | Permission denied.     |
449| 202      | Not system application to call the interface.                                      |
450| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
451| 1600001  | Internal error.                          |
452| 1600002  | Marshalling or unmarshalling error.      |
453| 1600003  | Failed to connect to the service.               |
454| 1600007  | The notification is not exist.           |
455| 17700001 | The specified bundle name was not found. |
456
457**示例:**
458
459```ts
460import { BusinessError } from '@kit.BasicServicesKit';
461import { notificationManager } from '@kit.NotificationKit';
462
463let bundle: notificationManager.BundleOption = {
464  bundle: "bundleName1",
465};
466let notificationKey: notificationSubscribe.NotificationKey = {
467  id: 0,
468  label: "label",
469};
470let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
471notificationSubscribe.remove(bundle, notificationKey, reason).then(() => {
472  console.info("remove success");
473}).catch((err: BusinessError) => {
474  console.error("remove fail: " + JSON.stringify(err));
475});
476```
477
478## notificationSubscribe.remove
479
480remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback\<void\>): void
481
482通过通知的唯一ID,删除指定通知。使用callback异步回调。
483
484**系统能力**:SystemCapability.Notification.Notification
485
486**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
487
488**系统接口**: 此接口为系统接口。
489
490**参数:**
491
492| 参数名     | 类型                  | 必填 | 说明                 |
493| -------- | --------------------- | ---- | -------------------- |
494| hashCode | string                | 是   | 通知唯一ID。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume)回调的入参[SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 |
495| reason   | [RemoveReason](#removereason) | 是   | 通知删除原因。         |
496| callback | AsyncCallback\<void\> | 是   | 删除指定通知回调函数。 |
497
498**错误码:**
499
500以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
501
502| 错误码ID | 错误信息                            |
503| -------- | ----------------------------------- |
504| 201      | Permission denied.     |
505| 202      | Not system application to call the interface.                                      |
506| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
507| 1600001  | Internal error.                     |
508| 1600002  | Marshalling or unmarshalling error. |
509| 1600003  | Failed to connect to the service.          |
510| 1600007  | The notification is not exist.      |
511
512**示例:**
513
514```ts
515import { BusinessError } from '@kit.BasicServicesKit';
516
517let hashCode: string = 'hashCode';
518let removeCallback = (err: BusinessError) => {
519  if (err) {
520    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
521  } else {
522    console.info("remove success");
523  }
524}
525let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
526notificationSubscribe.remove(hashCode, reason, removeCallback);
527```
528
529## notificationSubscribe.remove
530
531remove(hashCode: string, reason: RemoveReason): Promise\<void\>
532
533通过通知的唯一ID,删除指定通知。使用Promise异步回调。
534
535**系统能力**:SystemCapability.Notification.Notification
536
537**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
538
539**系统接口**: 此接口为系统接口。
540
541**参数:**
542
543| 参数名     | 类型       | 必填 | 说明       |
544| -------- | ---------- | ---- | ---------- |
545| hashCode | string | 是   | 通知唯一ID。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume)回调的入参[SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 |
546| reason   | [RemoveReason](#removereason) | 是   | 通知删除原因。         |
547
548**返回值:**
549
550| 类型     | 说明 |
551| ------- |--|
552| Promise\<void\> | 无返回结果的Promise对象。 |
553
554**错误码:**
555
556以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
557
558| 错误码ID | 错误信息                            |
559| -------- | ----------------------------------- |
560| 201      | Permission denied.     |
561| 202      | Not system application to call the interface.                                      |
562| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
563| 1600001  | Internal error.                     |
564| 1600002  | Marshalling or unmarshalling error. |
565| 1600003  | Failed to connect to the service.          |
566| 1600007  | The notification is not exist.      |
567
568**示例:**
569
570```ts
571import { BusinessError } from '@kit.BasicServicesKit';
572
573let hashCode: string = 'hashCode';
574let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
575notificationSubscribe.remove(hashCode, reason).then(() => {
576	console.info("remove success");
577}).catch((err: BusinessError) => {
578  console.error("remove fail: " + JSON.stringify(err));
579});
580```
581
582## notificationSubscribe.remove<sup>10+</sup>
583
584remove(hashCodes: Array\<String\>, reason: RemoveReason, callback: AsyncCallback\<void\>): void
585
586批量删除指定通知。使用callback异步回调。
587
588**系统能力**:SystemCapability.Notification.Notification
589
590**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
591
592**系统接口**: 此接口为系统接口。
593
594**参数:**
595
596| 参数名       | 类型                            | 必填 | 说明                                                                                                                                                                                                                                                                                  |
597|-----------|-------------------------------| ---- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
598| hashCodes | Array\<String\>               | 是   | 通知唯一ID数组集合。可以通过[onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume)回调的入参[SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata)获取其内部[NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest)对象中的hashCode。 |
599| reason    | [RemoveReason](#removereason) | 是   | 通知删除原因。                                                                                                                                                                                                                                                                             |
600| callback  | AsyncCallback\<void\>         | 是   | 删除指定通知回调函数。                                                                                                                                                                                                                                                                         |
601
602**错误码:**
603
604以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
605
606| 错误码ID | 错误信息                            |
607| -------- | ----------------------------------- |
608| 201      | Permission denied.     |
609| 202      | Not system application to call the interface.                                      |
610| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
611| 1600001  | Internal error.                     |
612| 1600002  | Marshalling or unmarshalling error. |
613| 1600003  | Failed to connect to the service.          |
614
615**示例:**
616
617```ts
618import { BusinessError } from '@kit.BasicServicesKit';
619
620let hashCodes: string[] = ['hashCode1', 'hashCode2'];
621let removeCallback = (err: BusinessError) => {
622  if (err) {
623    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
624  } else {
625    console.info("remove success");
626  }
627}
628let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
629notificationSubscribe.remove(hashCodes, reason, removeCallback);
630```
631
632## notificationSubscribe.remove<sup>10+</sup>
633
634remove(hashCodes: Array\<String\>, reason: RemoveReason): Promise\<void\>
635
636批量删除指定通知。使用Promise异步回调。
637
638**系统能力**:SystemCapability.Notification.Notification
639
640**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
641
642**系统接口**: 此接口为系统接口。
643
644**参数:**
645
646| 参数名       | 类型                            | 必填 | 说明          |
647|-----------|-------------------------------| ---- |-------------|
648| hashCodes | Array\<String\>               | 是   | 通知唯一ID数组集合。 |
649| reason    | [RemoveReason](#removereason) | 是   | 通知删除原因。     |
650
651**返回值:**
652
653| 类型     | 说明               |
654| ------- |------------------|
655| Promise\<void\> | 无返回结果的Promise对象。 |
656
657**错误码:**
658
659以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
660
661| 错误码ID | 错误信息                            |
662| -------- | ----------------------------------- |
663| 201      | Permission denied.     |
664| 202      | Not system application to call the interface.                                      |
665| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
666| 1600001  | Internal error.                     |
667| 1600002  | Marshalling or unmarshalling error. |
668| 1600003  | Failed to connect to the service.          |
669
670**示例:**
671
672```ts
673import { BusinessError } from '@kit.BasicServicesKit';
674
675let hashCodes: string[] = ['hashCode1','hashCode2'];
676let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
677notificationSubscribe.remove(hashCodes, reason).then(() => {
678  console.info("remove success");
679}).catch((err: BusinessError) => {
680  console.error("remove fail: " + JSON.stringify(err));
681});
682```
683
684## notificationSubscribe.removeAll
685
686removeAll(bundle: BundleOption, callback: AsyncCallback\<void\>): void
687
688删除指定应用的所有通知。使用callback异步回调。
689
690**系统能力**:SystemCapability.Notification.Notification
691
692**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
693
694**系统接口**:此接口为系统接口。
695
696**参数:**
697
698| 参数名     | 类型                  | 必填 | 说明                         |
699| -------- | --------------------- | ---- | ---------------------------- |
700| bundle   | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)        | 是   | 指定应用的包信息。                   |
701| callback | AsyncCallback\<void\> | 是   | 删除指定应用的所有通知回调函数。 |
702
703**错误码:**
704
705以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
706
707| 错误码ID | 错误信息                                 |
708| -------- | ---------------------------------------- |
709| 201      | Permission denied.     |
710| 202      | Not system application to call the interface.                                      |
711| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
712| 1600001  | Internal error.                          |
713| 1600002  | Marshalling or unmarshalling error.      |
714| 1600003  | Failed to connect to the service.               |
715| 17700001 | The specified bundle name was not found. |
716
717**示例:**
718
719```ts
720import { BusinessError } from '@kit.BasicServicesKit';
721
722let removeAllCallback = (err: BusinessError) => {
723  if (err) {
724    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
725  } else {
726    console.info("removeAll success");
727  }
728}
729let bundle: notificationSubscribe.BundleOption = {
730  bundle: "bundleName1",
731};
732notificationSubscribe.removeAll(bundle, removeAllCallback);
733```
734
735## notificationSubscribe.removeAll
736
737removeAll(callback: AsyncCallback\<void\>): void
738
739删除所有通知。使用callback异步回调。
740
741**系统能力**:SystemCapability.Notification.Notification
742
743**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
744
745**系统接口**: 此接口为系统接口。
746
747**参数:**
748
749| 参数名     | 类型                  | 必填 | 说明                 |
750| -------- | --------------------- | ---- | -------------------- |
751| callback | AsyncCallback\<void\> | 是   | 删除所有通知回调函数。 |
752
753**错误码:**
754
755以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
756
757| 错误码ID | 错误信息                            |
758| -------- | ----------------------------------- |
759| 201      | Permission denied.     |
760| 202      | Not system application to call the interface.                                      |
761| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
762| 1600001  | Internal error.                     |
763| 1600002  | Marshalling or unmarshalling error. |
764| 1600003  | Failed to connect to the service.          |
765
766**示例:**
767
768```ts
769import { BusinessError } from '@kit.BasicServicesKit';
770
771let removeAllCallback = (err: BusinessError) => {
772    if (err) {
773        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
774    } else {
775        console.info("removeAll success");
776    }
777}
778notificationSubscribe.removeAll(removeAllCallback);
779```
780
781## notificationSubscribe.removeAll
782
783removeAll(bundle?: BundleOption): Promise\<void\>
784
785删除指定应用的所有通知。使用Promise异步回调。
786
787**系统能力**:SystemCapability.Notification.Notification
788
789**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
790
791**系统接口**: 此接口为系统接口。
792
793**参数:**
794
795| 参数名   | 类型         | 必填 | 说明       |
796| ------ | ------------ | ---- | ---------- |
797| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | 否   | 指定应用的包信息。默认为空,表示删除所有通知。 |
798
799**返回值:**
800
801| 类型     | 说明         |
802| ------- |------------|
803| Promise\<void\> | 无返回结果的Promise对象。 |
804
805**错误码:**
806
807以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
808
809| 错误码ID | 错误信息                                 |
810| -------- | ---------------------------------------- |
811| 201      | Permission denied.     |
812| 202      | Not system application to call the interface.                                      |
813| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
814| 1600001  | Internal error.                          |
815| 1600002  | Marshalling or unmarshalling error.      |
816| 1600003  | Failed to connect to the service.               |
817| 17700001 | The specified bundle name was not found. |
818
819**示例:**
820
821```ts
822import { BusinessError } from '@kit.BasicServicesKit';
823
824// 不指定应用时,删除所有通知
825notificationSubscribe.removeAll().then(() => {
826	console.info("removeAll success");
827}).catch((err: BusinessError) => {
828  console.error("removeAll fail: " + JSON.stringify(err));
829});
830```
831
832## notificationSubscribe.removeAll
833
834removeAll(userId: number, callback: AsyncCallback\<void>): void
835
836删除指定用户下的所有通知。使用callback异步回调。
837
838**系统能力**:SystemCapability.Notification.Notification
839
840**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
841
842**系统接口**: 此接口为系统接口。
843
844**参数:**
845
846| 参数名   | 类型         | 必填 | 说明       |
847| ------ | ------------ | ---- | ---------- |
848| userId | number | 是   | 用户ID。 |
849| callback | AsyncCallback\<void\> | 是   | 删除指定用户所有通知回调函数。 |
850
851**错误码:**
852
853以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
854
855| 错误码ID | 错误信息                            |
856| -------- | ----------------------------------- |
857| 201      | Permission denied.     |
858| 202      | Not system application to call the interface.                                      |
859| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
860| 1600001  | Internal error.                     |
861| 1600002  | Marshalling or unmarshalling error. |
862| 1600003  | Failed to connect to the service.          |
863| 1600008  | The user does not exist.              |
864
865**示例:**
866
867```ts
868import { BusinessError } from '@kit.BasicServicesKit';
869
870let removeAllCallback = (err: BusinessError) => {
871  if (err) {
872    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
873  } else {
874    console.info("removeAll success");
875  }
876}
877// 用户ID,使用时需替换为真实的userId。
878let userId: number = 1;
879notificationSubscribe.removeAll(userId, removeAllCallback);
880```
881
882## notificationSubscribe.removeAll
883
884removeAll(userId: number): Promise\<void>
885
886删除指定用户下的所有通知。使用Promise异步回调。
887
888**系统能力**:SystemCapability.Notification.Notification
889
890**需要权限**: ohos.permission.NOTIFICATION_CONTROLLER
891
892**系统接口**: 此接口为系统接口。
893
894**参数:**
895
896| 参数名   | 类型         | 必填 | 说明       |
897| ------ | ------------ | ---- | ---------- |
898| userId | number | 是   | 用户ID。 |
899
900**错误码:**
901
902以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[通知错误码](./errorcode-notification.md)。
903
904| 错误码ID | 错误信息                            |
905| -------- | ----------------------------------- |
906| 201      | Permission denied.     |
907| 202      | Not system application to call the interface.                                      |
908| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
909| 1600001  | Internal error.                     |
910| 1600002  | Marshalling or unmarshalling error. |
911| 1600003  | Failed to connect to the service.          |
912| 1600008  | The user does not exist.              |
913
914**示例:**
915
916```ts
917import { BusinessError } from '@kit.BasicServicesKit';
918
919let userId: number = 1;
920notificationSubscribe.removeAll(userId).then(() => {
921	console.info("removeAll success");
922}).catch((err: BusinessError) => {
923  console.error("removeAll fail: " + JSON.stringify(err));
924});
925```
926
927## NotificationKey
928
929**系统能力**:以下各项对应的系统能力均为SystemCapability.Notification.Notification
930
931**系统接口**: 此接口为系统接口。
932
933| 名称  | 类型   | 必填 | 说明     |
934| ----- | ------ | --- | -------- |
935| id    | number | 是  | 通知ID。   |
936| label | string | 否  | 通知标签,默认为空。 |
937
938## RemoveReason
939
940**系统能力**:SystemCapability.Notification.Notification
941
942**系统接口**: 此接口为系统接口。
943
944| 名称                 | 值  | 说明                  |
945| -------------------- | --- | -------------------- |
946| CLICK_REASON_REMOVE  | 1   | 点击通知后删除通知。    |
947| CANCEL_REASON_REMOVE | 2   | 用户删除通知。         |
948