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 value of **hashCode** in the [NotificationRequest](js-apis-inner-notification-notificationRequest.md#notificationrequest) object of [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) in the [onConsume](#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## NotificationSubscriber 664 665Provides callbacks for receiving or removing notifications and serves as the input parameter of [subscribe](#notificationsubscribe). 666 667**System API**: This is a system API and cannot be called by third-party applications. 668 669### onConsume 670 671onConsume?: (data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void 672 673Callback for receiving notifications. 674 675**System capability**: SystemCapability.Notification.Notification 676 677**System API**: This is a system API and cannot be called by third-party applications. 678 679**Parameters** 680 681| Name| Type| Mandatory| Description| 682| ------------ | ------------------------ | ---- | -------------------------- | 683| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification received.| 684 685**Example** 686 687```javascript 688function subscribeCallback(err) { 689 if (err) { 690 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 691 } else { 692 console.info("subscribeCallback"); 693 } 694}; 695 696function onConsumeCallback(data) { 697 console.info('===> onConsume in test'); 698 let req = data.request; 699 console.info('===> onConsume callback req.id:' + req.id); 700}; 701 702let subscriber = { 703 onConsume: onConsumeCallback 704}; 705 706notificationSubscribe.subscribe(subscriber, subscribeCallback); 707``` 708 709### onCancel 710 711onCancel?:(data: [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata)) => void 712 713Callback for canceling notifications. 714 715**System capability**: SystemCapability.Notification.Notification 716 717**System API**: This is a system API and cannot be called by third-party applications. 718 719**Parameters** 720 721| Name| Type| Mandatory| Description| 722| ------------ | ------------------------ | ---- | -------------------------- | 723| data | [SubscribeCallbackData](js-apis-notification.md#subscribecallbackdata) | Yes| Information about the notification to cancel.| 724 725**Example** 726 727```javascript 728function subscribeCallback(err) { 729 if (err) { 730 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 731 } else { 732 console.info("subscribeCallback"); 733 } 734}; 735 736function onCancelCallback(data) { 737 console.info('===> onCancel in test'); 738 let req = data.request; 739 console.info('===> onCancel callback req.id:' + req.id); 740} 741 742let subscriber = { 743 onCancel: onCancelCallback 744}; 745 746notificationSubscribe.subscribe(subscriber, subscribeCallback); 747``` 748 749### onUpdate 750 751onUpdate?:(data: [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) => void 752 753Callback for notification sorting updates. 754 755**System capability**: SystemCapability.Notification.Notification 756 757**System API**: This is a system API and cannot be called by third-party applications. 758 759**Parameters** 760 761| Name| Type| Mandatory| Description| 762| ------------ | ------------------------ | ---- | -------------------------- | 763| data | [NotificationSortingMap](js-apis-notification.md#notificationsortingmap)) | Yes| Latest notification sorting list.| 764 765**Example** 766 767```javascript 768function subscribeCallback(err) { 769 if (err) { 770 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 771 } else { 772 console.info("subscribeCallback"); 773 } 774}; 775 776function onUpdateCallback(map) { 777 console.info('===> onUpdateCallback map:' + JSON.stringify(map)); 778} 779 780let subscriber = { 781 onUpdate: onUpdateCallback 782}; 783 784notificationSubscribe.subscribe(subscriber, subscribeCallback); 785``` 786 787### onConnect 788 789onConnect?:() => void 790 791Callback for subscription. 792 793**System capability**: SystemCapability.Notification.Notification 794 795**System API**: This is a system API and cannot be called by third-party applications. 796 797**Example** 798 799```javascript 800function subscribeCallback(err) { 801 if (err) { 802 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 803 } else { 804 console.info("subscribeCallback"); 805 } 806}; 807 808function onConnectCallback() { 809 console.info('===> onConnect in test'); 810} 811 812let subscriber = { 813 onConnect: onConnectCallback 814}; 815 816notificationSubscribe.subscribe(subscriber, subscribeCallback); 817``` 818 819### onDisconnect 820 821onDisconnect?:() => void 822 823Callback for unsubscription. 824 825**System capability**: SystemCapability.Notification.Notification 826 827**System API**: This is a system API and cannot be called by third-party applications. 828 829**Example** 830 831```javascript 832function subscribeCallback(err) { 833 if (err) { 834 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 835 } else { 836 console.info("subscribeCallback"); 837 } 838}; 839function unsubscribeCallback(err) { 840 if (err.code) { 841 console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`); 842 } else { 843 console.info("unsubscribeCallback"); 844 } 845}; 846 847function onConnectCallback() { 848 console.info('===> onConnect in test'); 849} 850function onDisconnectCallback() { 851 console.info('===> onDisconnect in test'); 852} 853 854let subscriber = { 855 onConnect: onConnectCallback, 856 onDisconnect: onDisconnectCallback 857}; 858 859// The onConnect callback is invoked when subscription to the notification is complete. 860notificationSubscribe.subscribe(subscriber, subscribeCallback); 861// The onDisconnect callback is invoked when unsubscription to the notification is complete. 862notificationSubscribe.unsubscribe(subscriber, unsubscribeCallback); 863``` 864 865### onDestroy 866 867onDestroy?:() => void 868 869Callback for service disconnection. 870 871**System capability**: SystemCapability.Notification.Notification 872 873**System API**: This is a system API and cannot be called by third-party applications. 874 875**Example** 876 877```javascript 878function subscribeCallback(err) { 879 if (err) { 880 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 881 } else { 882 console.info("subscribeCallback"); 883 } 884}; 885 886function onDestroyCallback() { 887 console.info('===> onDestroy in test'); 888} 889 890let subscriber = { 891 onDestroy: onDestroyCallback 892}; 893 894notificationSubscribe.subscribe(subscriber, subscribeCallback); 895``` 896 897### onDoNotDisturbDateChange 898 899onDoNotDisturbDateChange?:(mode: notification.[DoNotDisturbDate](js-apis-notificationManager.md#donotdisturbdate)) => void 900 901Callback for DND time setting updates. 902 903**System capability**: SystemCapability.Notification.Notification 904 905**System API**: This is a system API and cannot be called by third-party applications. 906 907**Parameters** 908 909| Name| Type| Mandatory| Description| 910| ------------ | ------------------------ | ---- | -------------------------- | 911| mode | notification.[DoNotDisturbDate](js-apis-notificationManager.md#DoNotDisturbDate) | Yes| DND time setting updates.| 912 913**Example** 914 915```javascript 916function subscribeCallback(err) { 917 if (err) { 918 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 919 } else { 920 console.info("subscribeCallback"); 921 } 922}; 923 924function onDoNotDisturbDateChangeCallback(mode) { 925 console.info('===> onDoNotDisturbDateChange:' + mode); 926} 927 928let subscriber = { 929 onDoNotDisturbDateChange: onDoNotDisturbDateChangeCallback 930}; 931 932notificationSubscribe.subscribe(subscriber, subscribeCallback); 933``` 934 935 936### onEnabledNotificationChanged 937 938onEnabledNotificationChanged?:(callbackData: [EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata)) => void 939 940Listens for the notification enabled status changes. This API uses an asynchronous callback to return the result. 941 942**System capability**: SystemCapability.Notification.Notification 943 944**System API**: This is a system API and cannot be called by third-party applications. 945 946**Parameters** 947 948| Name| Type| Mandatory| Description| 949| ------------ | ------------------------ | ---- | -------------------------- | 950| callback | AsyncCallback\<[EnabledNotificationCallbackData](js-apis-notification.md#enablednotificationcallbackdata)\> | Yes| Callback used to return the result.| 951 952**Example** 953 954```javascript 955function subscribeCallback(err) { 956 if (err) { 957 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 958 } else { 959 console.info("subscribeCallback"); 960 } 961}; 962 963function onEnabledNotificationChangedCallback(callbackData) { 964 console.info("bundle: ", callbackData.bundle); 965 console.info("uid: ", callbackData.uid); 966 console.info("enable: ", callbackData.enable); 967}; 968 969let subscriber = { 970 onEnabledNotificationChanged: onEnabledNotificationChangedCallback 971}; 972 973notificationSubscribe.subscribe(subscriber, subscribeCallback); 974``` 975 976### onBadgeChanged<sup>10+</sup> 977 978 onBadgeChanged?:(data: [BadgeNumberCallbackData](#badgenumbercallbackdata)) => void 979 980Listens for the change of the notification badge number. 981 982**System capability**: SystemCapability.Notification.Notification 983 984**System API**: This is a system API and cannot be called by third-party applications. 985 986**Parameters** 987 988| Name | Type | Mandatory| Description | 989| -------- | ------------------------------------------------------------ | ---- | -------------------------- | 990| callback | AsyncCallback\<[BadgeNumberCallbackData](#badgenumbercallbackdata)\> | Yes | Callback used to return the result.| 991 992**Example** 993 994```javascript 995function subscribeCallback(err) { 996 if (err) { 997 console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`); 998 } else { 999 console.info("subscribeCallback"); 1000 } 1001}; 1002 1003function onBadgeChangedCallback(data) { 1004 console.info("bundle: ", data.bundle); 1005 console.info("uid: ", data.uid); 1006 console.info("badgeNumber: ", data.badgeNumber); 1007}; 1008 1009let subscriber = { 1010 onBadgeChanged: onBadgeChangedCallback 1011}; 1012 1013notificationSubscribe.subscribe(subscriber, subscribeCallback); 1014``` 1015 1016 1017## RemoveReason 1018 1019**System capability**: SystemCapability.Notification.Notification 1020 1021**System API**: This is a system API and cannot be called by third-party applications. 1022 1023| Name | Value | Description | 1024| -------------------- | --- | -------------------- | 1025| CLICK_REASON_REMOVE | 1 | The notification is removed after a click on it. | 1026| CANCEL_REASON_REMOVE | 2 | The notification is removed by the user. | 1027 1028## BadgeNumberCallbackData<sup>10+</sup> 1029 1030**System capability**: SystemCapability.Notification.Notification 1031 1032**System API**: This is a system API and cannot be called by third-party applications. 1033 1034| Name | Type | Readable| Writable| Description | 1035| ----------- | ------ | ---- | ---- | ------------ | 1036| bundle | string | Yes | No | Bundle name of the application.| 1037| uid | number | Yes | No | UID of the application. | 1038| badgeNumber | number | Yes | No | Notification badge number. | 1039