1# @ohos.notificationSubscribe (NotificationSubscribe) 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## Modules to Import 10 11```js 12import notificationSubscribe from '@ohos.notificationSubscribe'; 13``` 14 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 and cannot be called by third-party applications. 28 29**Parameters** 30 31| Name | Type | Mandatory| Description | 32| ---------- | ------------------------- | ---- | ---------------- | 33| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes | Notification subscriber. | 34| info | [NotificationSubscribeInfo](js-apis-notification.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 [Notification Error Codes](../errorcodes/errorcode-notification.md). 40 41| ID| Error Message | 42| -------- | ----------------------------------- | 43| 1600001 | Internal error. | 44| 1600002 | Marshalling or unmarshalling error. | 45| 1600003 | Failed to connect service. | 46 47**Example** 48 49```js 50// subscribe callback 51function subscribeCallback(err) { 52 if (err) { 53 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 54 } else { 55 console.info("subscribe success"); 56 } 57} 58function onConsumeCallback(data) { 59 console.info("Consume callback: " + JSON.stringify(data)); 60} 61let subscriber = { 62 onConsume: onConsumeCallback 63}; 64let info = { 65 bundleNames: ["bundleName1","bundleName2"] 66}; 67notificationSubscribe.subscribe(subscriber, info, subscribeCallback); 68``` 69 70## NotificationSubscribe.subscribe 71 72subscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void 73 74Subscribes to notifications of all applications under this user. This API uses an asynchronous callback to return the result. 75 76**System capability**: SystemCapability.Notification.Notification 77 78**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 79 80**System API**: This is a system API and cannot be called by third-party applications. 81 82**Parameters** 83 84| Name | Type | Mandatory| Description | 85| ---------- | ---------------------- | ---- | ---------------- | 86| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes | Notification subscriber. | 87| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 88 89**Error codes** 90 91For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 92 93| ID| Error Message | 94| -------- | ----------------------------------- | 95| 1600001 | Internal error. | 96| 1600002 | Marshalling or unmarshalling error. | 97| 1600003 | Failed to connect service. | 98 99**Example** 100 101```js 102function subscribeCallback(err) { 103 if (err) { 104 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 105 } else { 106 console.info("subscribe success"); 107 } 108} 109function onConsumeCallback(data) { 110 console.info("Consume callback: " + JSON.stringify(data)); 111} 112let subscriber = { 113 onConsume: onConsumeCallback 114}; 115notificationSubscribe.subscribe(subscriber, subscribeCallback); 116``` 117 118 119 120## NotificationSubscribe.subscribe 121 122subscribe(subscriber: NotificationSubscriber, info?: NotificationSubscribeInfo): Promise\<void\> 123 124Subscribes to a notification with the subscription information specified. This API uses a promise to return the result. 125 126**System capability**: SystemCapability.Notification.Notification 127 128**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 129 130**System API**: This is a system API and cannot be called by third-party applications. 131 132**Parameters** 133 134| Name | Type | Mandatory| Description | 135| ---------- | ------------------------- | ---- | ------------ | 136| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes | Notification subscriber.| 137| info | [NotificationSubscribeInfo](js-apis-notification.md#notificationsubscribeinfo) | No | Notification subscription information. | 138 139**Error codes** 140 141For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 142 143| ID| Error Message | 144| -------- | ----------------------------------- | 145| 1600001 | Internal error. | 146| 1600002 | Marshalling or unmarshalling error. | 147| 1600003 | Failed to connect service. | 148 149**Example** 150 151```js 152function onConsumeCallback(data) { 153 console.info("Consume callback: " + JSON.stringify(data)); 154} 155let subscriber = { 156 onConsume: onConsumeCallback 157}; 158notificationSubscribe.subscribe(subscriber).then(() => { 159 console.info("subscribe success"); 160}); 161``` 162 163 164 165## NotificationSubscribe.unsubscribe 166 167unsubscribe(subscriber: NotificationSubscriber, callback: AsyncCallback\<void\>): void 168 169Unsubscribes from a notification. This API uses an asynchronous callback to return the result. 170 171**System capability**: SystemCapability.Notification.Notification 172 173**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 174 175**System API**: This is a system API and cannot be called by third-party applications. 176 177**Parameters** 178 179| Name | Type | Mandatory| Description | 180| ---------- | ---------------------- | ---- | -------------------- | 181| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes | Notification subscriber. | 182| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 183 184**Error codes** 185 186For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 187 188| ID| Error Message | 189| -------- | ----------------------------------- | 190| 1600001 | Internal error. | 191| 1600002 | Marshalling or unmarshalling error. | 192| 1600003 | Failed to connect service. | 193 194**Example** 195 196```js 197function unsubscribeCallback(err) { 198 if (err) { 199 console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`); 200 } else { 201 console.info("unsubscribe success"); 202 } 203} 204function onDisconnectCallback() { 205 console.info("subscribe disconnect"); 206} 207let subscriber = { 208 onDisconnect: onDisconnectCallback 209}; 210notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback); 211``` 212 213## NotificationSubscribe.unsubscribe 214 215unsubscribe(subscriber: NotificationSubscriber): Promise\<void\> 216 217Unsubscribes from a notification. This API uses a promise to return the result. 218 219**System capability**: SystemCapability.Notification.Notification 220 221**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 222 223**System API**: This is a system API and cannot be called by third-party applications. 224 225**Parameters** 226 227| Name | Type | Mandatory| Description | 228| ---------- | ---------------------- | ---- | ------------ | 229| subscriber | [NotificationSubscriber](js-apis-notification.md#notificationsubscriber) | Yes | Notification subscriber.| 230 231**Error codes** 232 233For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 234 235| ID| Error Message | 236| -------- | ----------------------------------- | 237| 1600001 | Internal error. | 238| 1600002 | Marshalling or unmarshalling error. | 239| 1600003 | Failed to connect service. | 240 241**Example** 242 243```js 244function onDisconnectCallback() { 245 console.info("subscribe disconnect"); 246} 247let subscriber = { 248 onDisconnect: onDisconnectCallback 249}; 250notificationSubscribe.unsubscribe(subscriber).then(() => { 251 console.info("unsubscribe success"); 252}); 253``` 254 255## NotificationSubscribe.remove 256 257remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason, callback: AsyncCallback\<void\>): void 258 259Removes a notification for a specified application. This API uses an asynchronous callback to return the result. 260 261**System capability**: SystemCapability.Notification.Notification 262 263**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 264 265**System API**: This is a system API and cannot be called by third-party applications. 266 267**Parameters** 268 269| Name | Type | Mandatory| Description | 270| --------------- | ----------------------------------| ---- | -------------------- | 271| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | Yes | Bundle information of the application. | 272| notificationKey | [NotificationKey](js-apis-notification.md#notificationkey) | Yes | Notification key. | 273| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 274| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 275 276**Error codes** 277 278For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 279 280| ID| Error Message | 281| -------- | ---------------------------------------- | 282| 1600001 | Internal error. | 283| 1600002 | Marshalling or unmarshalling error. | 284| 1600003 | Failed to connect service. | 285| 1600007 | The notification is not exist. | 286| 17700001 | The specified bundle name was not found. | 287 288**Example** 289 290```js 291function removeCallback(err) { 292 if (err) { 293 console.error(`remove failed, code is ${err.code}, message is ${err.message}`); 294 } else { 295 console.info("remove success"); 296 } 297} 298let bundle = { 299 bundle: "bundleName1", 300}; 301let notificationKey = { 302 id: 0, 303 label: "label", 304}; 305let reason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE; 306notificationSubscribe.remove(bundle, notificationKey, reason, removeCallback); 307``` 308 309 310 311## NotificationSubscribe.remove 312 313remove(bundle: BundleOption, notificationKey: NotificationKey, reason: RemoveReason): Promise\<void\> 314 315Removes a notification for a specified application. This API uses a promise to return the result. 316 317**System capability**: SystemCapability.Notification.Notification 318 319**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 320 321**System API**: This is a system API and cannot be called by third-party applications. 322 323**Parameters** 324 325| Name | Type | Mandatory| Description | 326| --------------- | --------------- | ---- | ---------- | 327| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | Yes | Bundle information of the application.| 328| notificationKey | [NotificationKey](js-apis-notification.md#notificationkey) | Yes | Notification key. | 329| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 330 331**Error codes** 332 333For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 334 335| ID| Error Message | 336| -------- | ---------------------------------------- | 337| 1600001 | Internal error. | 338| 1600002 | Marshalling or unmarshalling error. | 339| 1600003 | Failed to connect service. | 340| 1600007 | The notification is not exist. | 341| 17700001 | The specified bundle name was not found. | 342 343**Example** 344 345```js 346let bundle = { 347 bundle: "bundleName1", 348}; 349let notificationKey = { 350 id: 0, 351 label: "label", 352}; 353let reason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE; 354notificationSubscribe.remove(bundle, notificationKey, reason).then(() => { 355 console.info("remove success"); 356}); 357``` 358 359## NotificationSubscribe.remove 360 361remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback\<void\>): void 362 363Removes a specified notification. This API uses an asynchronous callback to return the result. 364 365**System capability**: SystemCapability.Notification.Notification 366 367**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 368 369**System API**: This is a system API and cannot be called by third-party applications. 370 371**Parameters** 372 373| Name | Type | Mandatory| Description | 374| -------- | --------------------- | ---- | -------------------- | 375| hashCode | string | Yes | Unique notification ID. It is the **hashCode** in the [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) object of [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) of the [onConsume](js-apis-notification.md#onconsume) callback.| 376| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 377| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 378 379**Error codes** 380 381For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 382 383| ID| Error Message | 384| -------- | ----------------------------------- | 385| 1600001 | Internal error. | 386| 1600002 | Marshalling or unmarshalling error. | 387| 1600003 | Failed to connect service. | 388| 1600007 | The notification is not exist. | 389 390**Example** 391 392```js 393let hashCode = 'hashCode'; 394 395function removeCallback(err) { 396 if (err) { 397 console.error(`remove failed, code is ${err.code}, message is ${err.message}`); 398 } else { 399 console.info("remove success"); 400 } 401} 402let reason = notificationSubscribe.RemoveReason.CANCEL_REASON_REMOVE; 403notificationSubscribe.remove(hashCode, reason, removeCallback); 404``` 405 406## NotificationSubscribe.remove 407 408remove(hashCode: string, reason: RemoveReason): Promise\<void\> 409 410Removes a specified notification. This API uses a promise to return the result. 411 412**System capability**: SystemCapability.Notification.Notification 413 414**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 415 416**System API**: This is a system API and cannot be called by third-party applications. 417 418**Parameters** 419 420| Name | Type | Mandatory| Description | 421| -------- | ---------- | ---- | ---------- | 422| hashCode | string | Yes | Unique notification ID.| 423| reason | [RemoveReason](#removereason) | Yes | Reason for removing the notification. | 424 425**Error codes** 426 427For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 428 429| ID| Error Message | 430| -------- | ----------------------------------- | 431| 1600001 | Internal error. | 432| 1600002 | Marshalling or unmarshalling error. | 433| 1600003 | Failed to connect service. | 434| 1600007 | The notification is not exist. | 435 436**Example** 437 438```js 439let hashCode = 'hashCode'; 440let reason = notificationSubscribe.RemoveReason.CLICK_REASON_REMOVE; 441notificationSubscribe.remove(hashCode, reason).then(() => { 442 console.info("remove success"); 443}); 444``` 445 446## NotificationSubscribe.removeAll 447 448removeAll(bundle: BundleOption, callback: AsyncCallback\<void\>): void 449 450Removes all notifications for a specified application. This API uses an asynchronous callback to return the result. 451 452**System capability**: SystemCapability.Notification.Notification 453 454**System API**: This is a system API and cannot be called by third-party applications. 455 456**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 457 458**Parameters** 459 460| Name | Type | Mandatory| Description | 461| -------- | --------------------- | ---- | ---------------------------- | 462| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | Yes | Bundle information of the application. | 463| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 464 465**Error codes** 466 467For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 468 469| ID| Error Message | 470| -------- | ---------------------------------------- | 471| 1600001 | Internal error. | 472| 1600002 | Marshalling or unmarshalling error. | 473| 1600003 | Failed to connect service. | 474| 17700001 | The specified bundle name was not found. | 475 476**Example** 477 478```js 479function removeAllCallback(err) { 480 if (err) { 481 console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`); 482 } else { 483 console.info("removeAll success"); 484 } 485} 486let bundle = { 487 bundle: "bundleName1", 488}; 489notificationSubscribe.removeAll(bundle, removeAllCallback); 490``` 491 492## NotificationSubscribe.removeAll 493 494removeAll(callback: AsyncCallback\<void\>): void 495 496Removes all notifications. This API uses an asynchronous callback to return the result. 497 498**System capability**: SystemCapability.Notification.Notification 499 500**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 501 502**System API**: This is a system API and cannot be called by third-party applications. 503 504**Parameters** 505 506| Name | Type | Mandatory| Description | 507| -------- | --------------------- | ---- | -------------------- | 508| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 509 510**Error codes** 511 512For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 513 514| ID| Error Message | 515| -------- | ----------------------------------- | 516| 1600001 | Internal error. | 517| 1600002 | Marshalling or unmarshalling error. | 518| 1600003 | Failed to connect service. | 519 520**Example** 521 522```js 523function removeAllCallback(err) { 524 if (err) { 525 console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`); 526 } else { 527 console.info("removeAll success"); 528 } 529} 530 531notificationSubscribe.removeAll(removeAllCallback); 532``` 533 534## NotificationSubscribe.removeAll 535 536removeAll(bundle?: BundleOption): Promise\<void\> 537 538Removes all notifications for a specified application. This API uses a promise to return the result. 539 540**System capability**: SystemCapability.Notification.Notification 541 542**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 543 544**System API**: This is a system API and cannot be called by third-party applications. 545 546**Parameters** 547 548| Name | Type | Mandatory| Description | 549| ------ | ------------ | ---- | ---------- | 550| bundle | [BundleOption](js-apis-inner-notification-notificationCommonDef.md#bundleoption) | No | Bundle information of the application.| 551 552**Error codes** 553 554For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 555 556| ID| Error Message | 557| -------- | ---------------------------------------- | 558| 1600001 | Internal error. | 559| 1600002 | Marshalling or unmarshalling error. | 560| 1600003 | Failed to connect service. | 561| 17700001 | The specified bundle name was not found. | 562 563**Example** 564 565```js 566// If no application is specified, notifications of all applications are deleted. 567notificationSubscribe.removeAll().then(() => { 568 console.info("removeAll success"); 569}); 570``` 571 572## NotificationSubscribe.removeAll 573 574removeAll(userId: number, callback: AsyncCallback\<void>): void 575 576Removes all notifications for a specified user. This API uses an asynchronous callback to return the result. 577 578**System capability**: SystemCapability.Notification.Notification 579 580**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 581 582**System API**: This is a system API and cannot be called by third-party applications. 583 584**Parameters** 585 586| Name | Type | Mandatory| Description | 587| ------ | ------------ | ---- | ---------- | 588| userId | number | Yes | User ID.| 589| callback | AsyncCallback\<void\> | Yes | Callback used to return the result.| 590 591**Error codes** 592 593For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 594 595| ID| Error Message | 596| -------- | ----------------------------------- | 597| 1600001 | Internal error. | 598| 1600002 | Marshalling or unmarshalling error. | 599| 1600003 | Failed to connect service. | 600| 1600008 | The user is not exist. | 601 602**Example** 603 604```js 605function removeAllCallback(err) { 606 if (err) { 607 console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`); 608 } else { 609 console.info("removeAll success"); 610 } 611} 612 613let userId = 1; 614 615notificationSubscribe.removeAll(userId, removeAllCallback); 616``` 617 618## Notification.removeAll 619 620removeAll(userId: number): Promise\<void> 621 622Removes all notifications for a specified user. This API uses a promise to return the result. 623 624**System capability**: SystemCapability.Notification.Notification 625 626**Required permissions**: ohos.permission.NOTIFICATION_CONTROLLER 627 628**System API**: This is a system API and cannot be called by third-party applications. 629 630**Parameters** 631 632| Name | Type | Mandatory| Description | 633| ------ | ------------ | ---- | ---------- | 634| userId | number | Yes | User ID.| 635 636**Error codes** 637 638For details about the error codes, see [Notification Error Codes](../errorcodes/errorcode-notification.md). 639 640| ID| Error Message | 641| -------- | ----------------------------------- | 642| 1600001 | Internal error. | 643| 1600002 | Marshalling or unmarshalling error. | 644| 1600003 | Failed to connect service. | 645| 1600008 | The user is not exist. | 646 647**Example** 648 649```js 650function removeAllCallback(err) { 651 if (err) { 652 console.error(`removeAll failed, code is ${err.code}, message is ${err.message}`); 653 } else { 654 console.info("removeAll success"); 655 } 656} 657 658let userId = 1; 659 660notificationSubscribe.removeAll(userId, removeAllCallback); 661``` 662 663## RemoveReason 664 665**System capability**: SystemCapability.Notification.Notification 666 667**System API**: This is a system API and cannot be called by third-party applications. 668 669| Name | Value | Description | 670| -------------------- | --- | -------------------- | 671| CLICK_REASON_REMOVE | 1 | The notification is removed after a click on it. | 672| CANCEL_REASON_REMOVE | 2 | The notification is removed by the user. | 673