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 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 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 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 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 245 246## notificationSubscribe.unsubscribe 247 248unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void 249 250Unsubscribes from a notification. This API uses an asynchronous callback to return the result. 251 252**System capability**: SystemCapability.Notification.Notification 253 254**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 255 256**System API**: This is a system API. 257 258**Parameters** 259 260| Name | Type | Mandatory| Description | 261| ---------- | ---------------------- | ---- | -------------------- | 262| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | Yes | Notification subscriber. | 263| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 264 265**Error codes** 266 267For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 268 269| ID| Error Message | 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 service. | 277 278**Example** 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 303Unsubscribes from a notification. This API uses a promise to return the result. 304 305**System capability**: SystemCapability.Notification.Notification 306 307**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 308 309**System API**: This is a system API. 310 311**Parameters** 312 313| Name | Type | Mandatory| Description | 314| ---------- | ---------------------- | ---- | ------------ | 315| subscriber | [NotificationSubscriber](js-apis-inner-notification-notificationSubscriber-sys.md#notificationsubscriber) | Yes | Notification subscriber.| 316 317**Return value** 318 319| Type | Description | 320| ------- |------------| 321| Promise\<void\> | Promise that returns no value.| 322 323**Error codes** 324 325For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 326 327| ID| Error Message | 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 service. | 335 336**Example** 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 358Removes a notification based on the bundle information and notification key. This API uses an asynchronous callback to return the result. 359 360**System capability**: SystemCapability.Notification.Notification 361 362**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 363 364**System API**: This is a system API. 365 366**Parameters** 367 368| Name | Type | Mandatory| Description | 369| --------------- | ----------------------------------| ---- | -------------------- | 370| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | Yes | Bundle information of the application. | 371| notificationKey | [NotificationKey](#notificationkey) | Yes | Notification key. | 372| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 373| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 374 375**Error codes** 376 377For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 378 379| ID| Error Message | 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 service. | 387| 1600007 | The notification is not exist. | 388| 17700001 | The specified bundle name was not found. | 389 390**Example** 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 420Removes a notification based on the bundle information and notification key. This API uses a promise to return the result. 421 422**System capability**: SystemCapability.Notification.Notification 423 424**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 425 426**System API**: This is a system API. 427 428**Parameters** 429 430| Name | Type | Mandatory| Description | 431| --------------- | --------------- | ---- | ---------- | 432| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | Yes | Bundle information of the application.| 433| notificationKey | [NotificationKey](#notificationkey) | Yes | Notification key. | 434| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 435 436**Return value** 437 438| Type | Description | 439| ------- |------------| 440| Promise\<void\> | Promise that returns no value.| 441 442**Error codes** 443 444For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 445 446| ID| Error Message | 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 service. | 454| 1600007 | The notification is not exist. | 455| 17700001 | The specified bundle name was not found. | 456 457**Example** 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 482Removes a notification based on the specified unique notification ID. This API uses an asynchronous callback to return the result. 483 484**System capability**: SystemCapability.Notification.Notification 485 486**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 487 488**System API**: This is a system API. 489 490**Parameters** 491 492| Name | Type | Mandatory| Description | 493| -------- | --------------------- | ---- | -------------------- | 494| 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.| 495| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 496| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 497 498**Error codes** 499 500For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 501 502| ID| Error Message | 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 service. | 510| 1600007 | The notification is not exist. | 511 512**Example** 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 533Removes a notification based on the specified unique notification ID. This API uses a promise to return the result. 534 535**System capability**: SystemCapability.Notification.Notification 536 537**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 538 539**System API**: This is a system API. 540 541**Parameters** 542 543| Name | Type | Mandatory| Description | 544| -------- | ---------- | ---- | ---------- | 545| 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.| 546| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 547 548**Return value** 549 550| Type | Description| 551| ------- |--| 552| Promise\<void\> | Promise that returns no value.| 553 554**Error codes** 555 556For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 557 558| ID| Error Message | 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 service. | 566| 1600007 | The notification is not exist. | 567 568**Example** 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 586Removes specified notifications. This API uses an asynchronous callback to return the result. 587 588**System capability**: SystemCapability.Notification.Notification 589 590**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 591 592**System API**: This is a system API. 593 594**Parameters** 595 596| Name | Type | Mandatory| Description | 597|-----------|-------------------------------| ---- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 598| 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.| 599| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 600| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. | 601 602**Error codes** 603 604For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 605 606| ID| Error Message | 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 service. | 614 615**Example** 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 636Removes specified notifications. This API uses a promise to return the result. 637 638**System capability**: SystemCapability.Notification.Notification 639 640**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 641 642**System API**: This is a system API. 643 644**Parameters** 645 646| Name | Type | Mandatory| Description | 647|-----------|-------------------------------| ---- |-------------| 648| hashCodes | Array\<String\> | Yes | Array of unique notification IDs.| 649| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 650 651**Return value** 652 653| Type | Description | 654| ------- |------------------| 655| Promise\<void\> | Promise that returns no value.| 656 657**Error codes** 658 659For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 660 661| ID| Error Message | 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 service. | 669 670**Example** 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 688Removes all notifications for a specified application. This API uses an asynchronous callback to return the result. 689 690**System capability**: SystemCapability.Notification.Notification 691 692**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 693 694**System API**: This is a system API. 695 696**Parameters** 697 698| Name | Type | Mandatory| Description | 699| -------- | --------------------- | ---- | ---------------------------- | 700| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | Yes | Bundle information of the application. | 701| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 702 703**Error codes** 704 705For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 706 707| ID| Error Message | 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 service. | 715| 17700001 | The specified bundle name was not found. | 716 717**Example** 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 739Removes all notifications. This API uses an asynchronous callback to return the result. 740 741**System capability**: SystemCapability.Notification.Notification 742 743**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 744 745**System API**: This is a system API. 746 747**Parameters** 748 749| Name | Type | Mandatory| Description | 750| -------- | --------------------- | ---- | -------------------- | 751| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 752 753**Error codes** 754 755For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 756 757| ID| Error Message | 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 service. | 765 766**Example** 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 785Removes all notifications for a specified application. This API uses a promise to return the result. 786 787**System capability**: SystemCapability.Notification.Notification 788 789**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 790 791**System API**: This is a system API. 792 793**Parameters** 794 795| Name | Type | Mandatory| Description | 796| ------ | ------------ | ---- | ---------- | 797| 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.| 798 799**Return value** 800 801| Type | Description | 802| ------- |------------| 803| Promise\<void\> | Promise that returns no value.| 804 805**Error codes** 806 807For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 808 809| ID| Error Message | 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 service. | 817| 17700001 | The specified bundle name was not found. | 818 819**Example** 820 821```ts 822import { BusinessError } from '@kit.BasicServicesKit'; 823 824// If no application is specified, notifications of all applications are deleted. 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 836Removes all notifications for a specified user. This API uses an asynchronous callback to return the result. 837 838**System capability**: SystemCapability.Notification.Notification 839 840**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 841 842**System API**: This is a system API. 843 844**Parameters** 845 846| Name | Type | Mandatory| Description | 847| ------ | ------------ | ---- | ---------- | 848| userId | number | Yes | User ID.| 849| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 850 851**Error codes** 852 853For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 854 855| ID| Error Message | 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 service. | 863| 1600008 | The user does not exist. | 864 865**Example** 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// Use the actual user ID when calling the API. 878let userId: number = 1; 879notificationSubscribe.removeAll(userId, removeAllCallback); 880``` 881 882## notificationSubscribe.removeAll 883 884removeAll(userId: number): Promise\<void> 885 886Removes all notifications for a specified user. This API uses a promise to return the result. 887 888**System capability**: SystemCapability.Notification.Notification 889 890**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 891 892**System API**: This is a system API. 893 894**Parameters** 895 896| Name | Type | Mandatory| Description | 897| ------ | ------------ | ---- | ---------- | 898| userId | number | Yes | User ID.| 899 900**Error codes** 901 902For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Notification Error Codes](./errorcode-notification.md). 903 904| ID| Error Message | 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 service. | 912| 1600008 | The user does not exist. | 913 914**Example** 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**System capability**: SystemCapability.Notification.Notification 930 931**System API**: This is a system API. 932 933| Name | Type | Mandatory| Description | 934| ----- | ------ | --- | -------- | 935| id | number | Yes | Notification ID. | 936| label | string | No | Notification label. This parameter is left empty by default.| 937 938## RemoveReason 939 940**System capability**: SystemCapability.Notification.Notification 941 942**System API**: This is a system API. 943 944| Name | Value | Description | 945| -------------------- | --- | -------------------- | 946| CLICK_REASON_REMOVE | 1 | The notification is removed after a click on it. | 947| CANCEL_REASON_REMOVE | 2 | The notification is removed by the user. | 948