• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.notificationSubscribe (NotificationSubscribe) (System API)
2
3The **notificationSubscribe** module provides APIs for notification subscription, notification unsubscription, subscription removal, and more. In general cases, only system applications can call these APIs.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> The APIs provided by this module are system APIs.
10
11## Modules to Import
12
13```ts
14import { notificationSubscribe } from '@kit.NotificationKit';
15```
16
17## notificationSubscribe.subscribe
18
19subscribe(subscriber: NotificationSubscriber, info: NotificationSubscribeInfo, callback: AsyncCallback\<void\>): void
20
21Subscribes to a notification with the subscription information specified. This API uses an asynchronous callback to return the result.
22
23**System capability**: SystemCapability.Notification.Notification
24
25**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
26
27**System API**: This is a system API.
28
29**Parameters**
30
31| Name      | Type                     | Mandatory| Description            |
32| ---------- | ------------------------- | ---- | ---------------- |
33| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber)    | Yes  | Notification subscriber.    |
34| info       | [NotificationSubscribeInfo](js-apis-inner-notification-notificationSubscribeInfo-sys.md#notificationsubscribeinfo) | Yes  | Notification subscription information.|
35| callback   | AsyncCallback\<void\>     | Yes  | Callback used to return the result.|
36
37**Error codes**
38
39For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
40
41| ID| Error Message                            |
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**Example**
52
53```ts
54import { BusinessError } from '@kit.BasicServicesKit';
55
56// subscribe callback
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// Bundle names are not verified. You need to determine the target bundle names.
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
81Subscribes to notifications of all applications under this user. This API uses an asynchronous callback to return the result.
82
83**System capability**: SystemCapability.Notification.Notification
84
85**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
86
87**System API**: This is a system API.
88
89**Parameters**
90
91| Name      | Type                  | Mandatory| Description            |
92| ---------- | ---------------------- | ---- | ---------------- |
93| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | Yes  | Notification subscriber.    |
94| callback   | AsyncCallback\<void\>  | Yes  | Callback used to return the result.|
95
96**Error codes**
97
98For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
99
100| ID| Error Message                           |
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**Example**
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
137Subscribes to a notification with the subscription information specified. This API uses a promise to return the result.
138
139**System capability**: SystemCapability.Notification.Notification
140
141**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
142
143**System API**: This is a system API.
144
145**Parameters**
146
147| Name      | Type                     | Mandatory| Description        |
148| ---------- | ------------------------- | ---- | ------------ |
149| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber)    | Yes  | Notification subscriber.|
150| info       | [NotificationSubscribeInfo](js-apis-inner-notification-notificationSubscribeInfo-sys.md#notificationsubscribeinfo) | No  | Notification subscription information. By default, this parameter is left empty, which means to subscribe to notifications of all applications under this user.  |
151
152**Return value**
153
154| Type    | Description              |
155| ------- |------------------|
156| Promise\<void\> | Promise that returns no value.|
157
158**Error codes**
159
160For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
161
162| ID| Error Message                           |
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**Example**
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
195Subscribes to notifications of the application and specifies subscription information. This API uses a promise to return the result.
196
197**System capability**: SystemCapability.Notification.Notification
198
199**System API**: This is a system API.
200
201**Parameters**
202
203| Name      | Type                     | Mandatory| Description        |
204| ---------- | ------------------------- | ---- | ------------ |
205| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber)    | Yes  | Notification subscriber.|
206
207**Return value**
208
209| Type    | Description              |
210| ------- |------------------|
211| Promise\<void\> | Promise that returns no value.|
212
213**Error codes**
214
215For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
216
217| ID| Error Message                           |
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**Example**
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## notificationSubscribe.unsubscribe
245
246unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void
247
248Unsubscribes from a notification. This API uses an asynchronous callback to return the result.
249
250**System capability**: SystemCapability.Notification.Notification
251
252**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
253
254**System API**: This is a system API.
255
256**Parameters**
257
258| Name      | Type                  | Mandatory| Description                |
259| ---------- | ---------------------- | ---- | -------------------- |
260| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | Yes  | Notification subscriber.        |
261| callback   | AsyncCallback\<void\>  | Yes  | Callback used to return the result.|
262
263**Error codes**
264
265For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
266
267| ID| Error Message                           |
268| -------- | ----------------------------------- |
269| 201      | Permission denied.     |
270| 202      | Not system application to call the interface.                                      |
271| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
272| 1600001  | Internal error.                     |
273| 1600002  | Marshalling or unmarshalling error. |
274| 1600003  | Failed to connect to the service.          |
275
276**Example**
277
278```ts
279import { BusinessError } from '@kit.BasicServicesKit';
280
281let unsubscribeCallback = (err: BusinessError) => {
282  if (err) {
283    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
284  } else {
285    console.info("unsubscribe success");
286  }
287}
288let onDisconnectCallback = () => {
289  console.info("subscribe disconnect");
290}
291let subscriber: notificationSubscribe.NotificationSubscriber = {
292  onDisconnect: onDisconnectCallback
293};
294notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback);
295```
296
297## notificationSubscribe.unsubscribe
298
299unsubscribe(subscriber: NotificationSubscriber): Promise\<void\>
300
301Unsubscribes from a notification. This API uses a promise to return the result.
302
303**System capability**: SystemCapability.Notification.Notification
304
305**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
306
307**System API**: This is a system API.
308
309**Parameters**
310
311| Name      | Type                  | Mandatory| Description        |
312| ---------- | ---------------------- | ---- | ------------ |
313| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | Yes  | Notification subscriber.|
314
315**Return value**
316
317| Type    | Description        |
318| ------- |------------|
319| Promise\<void\> | Promise that returns no value.|
320
321**Error codes**
322
323For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
324
325| ID| Error Message                           |
326| -------- | ----------------------------------- |
327| 201      | Permission denied.     |
328| 202      | Not system application to call the interface.                                      |
329| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
330| 1600001  | Internal error.                     |
331| 1600002  | Marshalling or unmarshalling error. |
332| 1600003  | Failed to connect to the service.          |
333
334**Example**
335
336```ts
337import { BusinessError } from '@kit.BasicServicesKit';
338
339let onDisconnectCallback = () => {
340  console.info("subscribe disconnect");
341}
342let subscriber: notificationSubscribe.NotificationSubscriber = {
343  onDisconnect: onDisconnectCallback
344};
345notificationSubscribe.unsubscribe(subscriber).then(() => {
346  console.info("unsubscribe success");
347}).catch((err: BusinessError) => {
348  console.error("unsubscribe fail: " + JSON.stringify(err));
349});
350```
351
352## notificationSubscribe.remove
353
354remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason, callback: AsyncCallback\<void\>): void
355
356Removes a notification based on the bundle information and notification key. This API uses an asynchronous callback to return the result.
357
358**System capability**: SystemCapability.Notification.Notification
359
360**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
361
362**System API**: This is a system API.
363
364**Parameters**
365
366| Name           | Type                               | Mandatory| Description                |
367| --------------- |   ----------------------------------| ---- | -------------------- |
368| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)       | Yes  | Bundle information of the application.          |
369| notificationKey | [NotificationKey](#notificationkey) | Yes  | Notification key.            |
370| reason          | [RemoveReason](#removereason)      | Yes  | Reason for removing the notification.        |
371| callback        | AsyncCallback\<void\>               | Yes  | Callback used to return the result.|
372
373**Error codes**
374
375For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
376
377| ID| Error Message                                |
378| -------- | ---------------------------------------- |
379| 201      | Permission denied.     |
380| 202      | Not system application to call the interface.                                      |
381| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
382| 1600001  | Internal error.                          |
383| 1600002  | Marshalling or unmarshalling error.      |
384| 1600003  | Failed to connect to the service.               |
385| 1600007  | The notification is not exist.           |
386| 17700001 | The specified bundle name was not found. |
387
388**Example**
389
390```ts
391import { BusinessError } from '@kit.BasicServicesKit';
392import { notificationManager } from '@kit.NotificationKit';
393
394let removeCallback = (err: BusinessError) => {
395  if (err) {
396    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
397  } else {
398    console.info("remove success");
399  }
400}
401let bundle: notificationManager.BundleOption = {
402  bundle: "bundleName1",
403};
404let notificationKey: notificationSubscribe.NotificationKey = {
405  id: 0,
406  label: "label",
407};
408let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
409notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback);
410```
411
412## notificationSubscribe.remove
413
414remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason): Promise\<void\>
415
416Removes a notification based on the bundle information and notification key. This API uses a promise to return the result.
417
418**System capability**: SystemCapability.Notification.Notification
419
420**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
421
422**System API**: This is a system API.
423
424**Parameters**
425
426| Name           | Type           | Mandatory| Description      |
427| --------------- | --------------- | ---- | ---------- |
428| bundle          | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)    | Yes  | Bundle information of the application.|
429| notificationKey | [NotificationKey](#notificationkey) | Yes  | Notification key.  |
430| reason          | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |
431
432**Return value**
433
434| Type    | Description        |
435| ------- |------------|
436| Promise\<void\> | Promise that returns no value.|
437
438**Error codes**
439
440For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
441
442| ID| Error Message                                |
443| -------- | ---------------------------------------- |
444| 201      | Permission denied.     |
445| 202      | Not system application to call the interface.                                      |
446| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
447| 1600001  | Internal error.                          |
448| 1600002  | Marshalling or unmarshalling error.      |
449| 1600003  | Failed to connect to the service.               |
450| 1600007  | The notification is not exist.           |
451| 17700001 | The specified bundle name was not found. |
452
453**Example**
454
455```ts
456import { BusinessError } from '@kit.BasicServicesKit';
457import { notificationManager } from '@kit.NotificationKit';
458
459let bundle: notificationManager.BundleOption = {
460  bundle: "bundleName1",
461};
462let notificationKey: notificationSubscribe.NotificationKey = {
463  id: 0,
464  label: "label",
465};
466let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
467notificationSubscribe.remove(bundle, notificationKey, reason).then(() => {
468  console.info("remove success");
469}).catch((err: BusinessError) => {
470  console.error("remove fail: " + JSON.stringify(err));
471});
472```
473
474## notificationSubscribe.remove
475
476remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback\<void\>): void
477
478Removes a notification based on the specified unique notification ID. This API uses an asynchronous callback to return the result.
479
480**System capability**: SystemCapability.Notification.Notification
481
482**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
483
484**System API**: This is a system API.
485
486**Parameters**
487
488| Name    | Type                 | Mandatory| Description                |
489| -------- | --------------------- | ---- | -------------------- |
490| hashCode | string                | Yes  | Unique notification ID. It is the value of **hashCode** in the [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) object of [SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata) in the [onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume) callback.|
491| reason   | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |
492| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|
493
494**Error codes**
495
496For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
497
498| ID| Error Message                           |
499| -------- | ----------------------------------- |
500| 201      | Permission denied.     |
501| 202      | Not system application to call the interface.                                      |
502| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
503| 1600001  | Internal error.                     |
504| 1600002  | Marshalling or unmarshalling error. |
505| 1600003  | Failed to connect to the service.          |
506| 1600007  | The notification is not exist.      |
507
508**Example**
509
510```ts
511import { BusinessError } from '@kit.BasicServicesKit';
512
513let hashCode: string = 'hashCode';
514let removeCallback = (err: BusinessError) => {
515  if (err) {
516    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
517  } else {
518    console.info("remove success");
519  }
520}
521let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
522notificationSubscribe.remove(hashCode, reason, removeCallback);
523```
524
525## notificationSubscribe.remove
526
527remove(hashCode: string, reason: RemoveReason): Promise\<void\>
528
529Removes a notification based on the specified unique notification ID. This API uses a promise to return the result.
530
531**System capability**: SystemCapability.Notification.Notification
532
533**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
534
535**System API**: This is a system API.
536
537**Parameters**
538
539| Name    | Type      | Mandatory| Description      |
540| -------- | ---------- | ---- | ---------- |
541| hashCode | string | Yes  | Unique notification ID. It is the value of **hashCode** in the [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) object of [SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata) in the [onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume) callback.|
542| reason   | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.        |
543
544**Return value**
545
546| Type    | Description|
547| ------- |--|
548| Promise\<void\> | Promise that returns no value.|
549
550**Error codes**
551
552For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
553
554| ID| Error Message                           |
555| -------- | ----------------------------------- |
556| 201      | Permission denied.     |
557| 202      | Not system application to call the interface.                                      |
558| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
559| 1600001  | Internal error.                     |
560| 1600002  | Marshalling or unmarshalling error. |
561| 1600003  | Failed to connect to the service.          |
562| 1600007  | The notification is not exist.      |
563
564**Example**
565
566```ts
567import { BusinessError } from '@kit.BasicServicesKit';
568
569let hashCode: string = 'hashCode';
570let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
571notificationSubscribe.remove(hashCode, reason).then(() => {
572	console.info("remove success");
573}).catch((err: BusinessError) => {
574  console.error("remove fail: " + JSON.stringify(err));
575});
576```
577
578## notificationSubscribe.remove<sup>10+</sup>
579
580remove(hashCodes: Array\<String\>, reason: RemoveReason, callback: AsyncCallback\<void\>): void
581
582Removes specified notifications. This API uses an asynchronous callback to return the result.
583
584**System capability**: SystemCapability.Notification.Notification
585
586**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
587
588**System API**: This is a system API.
589
590**Parameters**
591
592| Name      | Type                           | Mandatory| Description                                                                                                                                                                                                                                                                                 |
593|-----------|-------------------------------| ---- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
594| hashCodes | Array\<String\>               | Yes  | Array of unique notification IDs. It is the value of **hashCode** in the [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) object of [SubscribeCallbackData](js-apis-inner-notification-notificationSubscriber-sys.md#subscribecallbackdata) in the [onConsume](js-apis-inner-notification-notificationSubscriber-sys.md#onconsume) callback.|
595| reason    | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.                                                                                                                                                                                                                                                                            |
596| callback  | AsyncCallback\<void\>         | Yes  | Callback used to return the result.                                                                                                                                                                                                                                                                        |
597
598**Error codes**
599
600For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
601
602| ID| Error Message                           |
603| -------- | ----------------------------------- |
604| 201      | Permission denied.     |
605| 202      | Not system application to call the interface.                                      |
606| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
607| 1600001  | Internal error.                     |
608| 1600002  | Marshalling or unmarshalling error. |
609| 1600003  | Failed to connect to the service.          |
610
611**Example**
612
613```ts
614import { BusinessError } from '@kit.BasicServicesKit';
615
616let hashCodes: string[] = ['hashCode1', 'hashCode2'];
617let removeCallback = (err: BusinessError) => {
618  if (err) {
619    console.error(`remove failed, code is ${err.code}, message is ${err.message}`);
620  } else {
621    console.info("remove success");
622  }
623}
624let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE;
625notificationSubscribe.remove(hashCodes, reason, removeCallback);
626```
627
628## notificationSubscribe.remove<sup>10+</sup>
629
630remove(hashCodes: Array\<String\>, reason: RemoveReason): Promise\<void\>
631
632Removes specified notifications. This API uses a promise to return the result.
633
634**System capability**: SystemCapability.Notification.Notification
635
636**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
637
638**System API**: This is a system API.
639
640**Parameters**
641
642| Name      | Type                           | Mandatory| Description         |
643|-----------|-------------------------------| ---- |-------------|
644| hashCodes | Array\<String\>               | Yes  | Array of unique notification IDs.|
645| reason    | [RemoveReason](#removereason) | Yes  | Reason for removing the notification.    |
646
647**Return value**
648
649| Type    | Description              |
650| ------- |------------------|
651| Promise\<void\> | Promise that returns no value.|
652
653**Error codes**
654
655For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
656
657| ID| Error Message                           |
658| -------- | ----------------------------------- |
659| 201      | Permission denied.     |
660| 202      | Not system application to call the interface.                                      |
661| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
662| 1600001  | Internal error.                     |
663| 1600002  | Marshalling or unmarshalling error. |
664| 1600003  | Failed to connect to the service.          |
665
666**Example**
667
668```ts
669import { BusinessError } from '@kit.BasicServicesKit';
670
671let hashCodes: string[] = ['hashCode1','hashCode2'];
672let reason: notificationSubscribe.RemoveReason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE;
673notificationSubscribe.remove(hashCodes, reason).then(() => {
674  console.info("remove success");
675}).catch((err: BusinessError) => {
676  console.error("remove fail: " + JSON.stringify(err));
677});
678```
679
680## notificationSubscribe.removeAll
681
682removeAll(bundle: BundleOption, callback: AsyncCallback\<void\>): void
683
684Removes all notifications for a specified application. This API uses an asynchronous callback to return the result.
685
686**System capability**: SystemCapability.Notification.Notification
687
688**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
689
690**System API**: This is a system API.
691
692**Parameters**
693
694| Name    | Type                 | Mandatory| Description                        |
695| -------- | --------------------- | ---- | ---------------------------- |
696| bundle   | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption)        | Yes  | Bundle information of the application.                  |
697| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|
698
699**Error codes**
700
701For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
702
703| ID| Error Message                                |
704| -------- | ---------------------------------------- |
705| 201      | Permission denied.     |
706| 202      | Not system application to call the interface.                                      |
707| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
708| 1600001  | Internal error.                          |
709| 1600002  | Marshalling or unmarshalling error.      |
710| 1600003  | Failed to connect to the service.               |
711| 17700001 | The specified bundle name was not found. |
712
713**Example**
714
715```ts
716import { BusinessError } from '@kit.BasicServicesKit';
717
718let removeAllCallback = (err: BusinessError) => {
719  if (err) {
720    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
721  } else {
722    console.info("removeAll success");
723  }
724}
725let bundle: notificationSubscribe.BundleOption = {
726  bundle: "bundleName1",
727};
728notificationSubscribe.removeAll(bundle, removeAllCallback);
729```
730
731## notificationSubscribe.removeAll
732
733removeAll(callback: AsyncCallback\<void\>): void
734
735Removes all notifications. This API uses an asynchronous callback to return the result.
736
737**System capability**: SystemCapability.Notification.Notification
738
739**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
740
741**System API**: This is a system API.
742
743**Parameters**
744
745| Name    | Type                 | Mandatory| Description                |
746| -------- | --------------------- | ---- | -------------------- |
747| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|
748
749**Error codes**
750
751For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
752
753| ID| Error Message                           |
754| -------- | ----------------------------------- |
755| 201      | Permission denied.     |
756| 202      | Not system application to call the interface.                                      |
757| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
758| 1600001  | Internal error.                     |
759| 1600002  | Marshalling or unmarshalling error. |
760| 1600003  | Failed to connect to the service.          |
761
762**Example**
763
764```ts
765import { BusinessError } from '@kit.BasicServicesKit';
766
767let removeAllCallback = (err: BusinessError) => {
768    if (err) {
769        console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
770    } else {
771        console.info("removeAll success");
772    }
773}
774notificationSubscribe.removeAll(removeAllCallback);
775```
776
777## notificationSubscribe.removeAll
778
779removeAll(bundle?: BundleOption): Promise\<void\>
780
781Removes all notifications for a specified application. This API uses a promise to return the result.
782
783**System capability**: SystemCapability.Notification.Notification
784
785**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
786
787**System API**: This is a system API.
788
789**Parameters**
790
791| Name  | Type        | Mandatory| Description      |
792| ------ | ------------ | ---- | ---------- |
793| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | No  | Bundle information of the application. By default, this parameter is left empty, indicating that all notifications will be removed.|
794
795**Return value**
796
797| Type    | Description        |
798| ------- |------------|
799| Promise\<void\> | Promise that returns no value.|
800
801**Error codes**
802
803For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
804
805| ID| Error Message                                |
806| -------- | ---------------------------------------- |
807| 201      | Permission denied.     |
808| 202      | Not system application to call the interface.                                      |
809| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
810| 1600001  | Internal error.                          |
811| 1600002  | Marshalling or unmarshalling error.      |
812| 1600003  | Failed to connect to the service.               |
813| 17700001 | The specified bundle name was not found. |
814
815**Example**
816
817```ts
818import { BusinessError } from '@kit.BasicServicesKit';
819
820// If no application is specified, notifications of all applications are deleted.
821notificationSubscribe.removeAll().then(() => {
822	console.info("removeAll success");
823}).catch((err: BusinessError) => {
824  console.error("removeAll fail: " + JSON.stringify(err));
825});
826```
827
828## notificationSubscribe.removeAll
829
830removeAll(userId: number, callback: AsyncCallback\<void>): void
831
832Removes all notifications for a specified user. This API uses an asynchronous callback to return the result.
833
834**System capability**: SystemCapability.Notification.Notification
835
836**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
837
838**System API**: This is a system API.
839
840**Parameters**
841
842| Name  | Type        | Mandatory| Description      |
843| ------ | ------------ | ---- | ---------- |
844| userId | number | Yes  | User ID.|
845| callback | AsyncCallback\<void\> | Yes  | Callback used to return the result.|
846
847**Error codes**
848
849For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
850
851| ID| Error Message                           |
852| -------- | ----------------------------------- |
853| 201      | Permission denied.     |
854| 202      | Not system application to call the interface.                                      |
855| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
856| 1600001  | Internal error.                     |
857| 1600002  | Marshalling or unmarshalling error. |
858| 1600003  | Failed to connect to the service.          |
859| 1600008  | The user does not exist.              |
860
861**Example**
862
863```ts
864import { BusinessError } from '@kit.BasicServicesKit';
865
866let removeAllCallback = (err: BusinessError) => {
867  if (err) {
868    console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`);
869  } else {
870    console.info("removeAll success");
871  }
872}
873// Use the actual user ID when calling the API.
874let userId: number = 1;
875notificationSubscribe.removeAll(userId, removeAllCallback);
876```
877
878## notificationSubscribe.removeAll
879
880removeAll(userId: number): Promise\<void>
881
882Removes all notifications for a specified user. This API uses a promise to return the result.
883
884**System capability**: SystemCapability.Notification.Notification
885
886**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
887
888**System API**: This is a system API.
889
890**Parameters**
891
892| Name  | Type        | Mandatory| Description      |
893| ------ | ------------ | ---- | ---------- |
894| userId | number | Yes  | User ID.|
895
896**Error codes**
897
898For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
899
900| ID| Error Message                           |
901| -------- | ----------------------------------- |
902| 201      | Permission denied.     |
903| 202      | Not system application to call the interface.                                      |
904| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
905| 1600001  | Internal error.                     |
906| 1600002  | Marshalling or unmarshalling error. |
907| 1600003  | Failed to connect to the service.          |
908| 1600008  | The user does not exist.              |
909
910**Example**
911
912```ts
913import { BusinessError } from '@kit.BasicServicesKit';
914
915let userId: number = 1;
916notificationSubscribe.removeAll(userId).then(() => {
917	console.info("removeAll success");
918}).catch((err: BusinessError) => {
919  console.error("removeAll fail: " + JSON.stringify(err));
920});
921```
922
923## notificationSubscribe.distributeOperation<sup>18+</sup>
924
925distributeOperation(hashcode: string, operationInfo?: OperationInfo): Promise\<void>
926
927Triggers a notification for cross-device operations, such as tap-to-redirect and quick reply. This API uses a promise to return the result.
928
929**System capability**: SystemCapability.Notification.Notification
930
931**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER
932
933**System API**: This is a system API.
934
935**Parameters**
936
937| Name  | Type        | Mandatory| Description      |
938| ------ | ------------ | ---- | ---------- |
939| hashcode | string | Yes  | Unique notification ID.|
940| operationInfo | [OperationInfo](#operationinfo18) | No  | Cross-device operation information.|
941
942**Error codes**
943
944For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md).
945
946| ID| Error Message                           |
947| -------- | ----------------------------------- |
948| 201      | Permission denied.     |
949| 202      | Not system application to call the interface.                                      |
950| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      |
951| 1600010  | Distributed operation failed.                     |
952| 1600021  | Distributed operation timed out.                    |
953
954**Example**
955
956```ts
957import { BusinessError } from '@kit.BasicServicesKit';
958
959let hashcode: string = 'hashcode';
960let operationInfo: notificationSubscribe.OperationInfo = {
961  actionName: "actionName",
962  userInput: "userInput",
963};
964notificationSubscribe.distributeOperation(hashcode, operationInfo).then(() => {
965	console.info("distributeOperation success");
966}).catch((err: BusinessError) => {
967  console.error("distributeOperation fail: " + JSON.stringify(err));
968});
969```
970
971## NotificationKey
972
973**System capability**: SystemCapability.Notification.Notification
974
975**System API**: This is a system API.
976
977| Name | Type  | Mandatory| Description    |
978| ----- | ------ | --- | -------- |
979| id    | number | Yes | Notification ID.  |
980| label | string | No | Notification label. This parameter is left empty by default.|
981
982## RemoveReason
983
984**System capability**: SystemCapability.Notification.Notification
985
986**System API**: This is a system API.
987
988| Name                | Value | Description                 |
989| -------------------- | --- | -------------------- |
990| CLICK_REASON_REMOVE  | 1   | The notification is removed after a click on it.   |
991| CANCEL_REASON_REMOVE | 2   | The notification is removed by the user.        |
992
993## OperationInfo<sup>18+</sup>
994
995**System capability**: SystemCapability.Notification.Notification
996
997**System API**: This is a system API.
998
999| Name | Type  | Mandatory| Description    |
1000| ----- | ------ | --- | -------- |
1001| actionName    | string | No | Operation button displayed in the notification. The value must be the same as that of **title** in [NotificationActionButton](js-apis-inner-notification-notificationActionButton.md#notificationactionbutton).  |
1002| userInput | string | No | User input, used to apply quick reply across devices. The value must be the same as that of **inputKey** in [NotificationUserInput](js-apis-inner-notification-notificationUserInput.md#notificationuserinput).|
1003