1# CommonEventSubscriber 2 3The **CommonEventSubscriber** module provides APIs for describing the common event subscriber. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Usage 10 11Before using the **CommonEventSubscriber** module, you must obtain a **subscriber** object by calling **commonEventManager.createSubscriber**. 12 13```ts 14import { commonEventManager } from '@kit.BasicServicesKit'; 15import { BusinessError } from '@kit.BasicServicesKit'; 16let subscriber: commonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 17 18// Subscriber information. 19let subscribeInfo:commonEventManager.CommonEventSubscribeInfo = { 20 events: ["event"] 21}; 22 23// Callback for subscriber creation. 24function createCB(err: BusinessError, commonEventSubscriber:commonEventManager.CommonEventSubscriber) { 25 if (err != null) { 26 console.error(`createSubscriber failed, code is ${err.code}`); 27 } else { 28 console.info("createSubscriber success"); 29 subscriber = commonEventSubscriber; 30 } 31} 32 33// Create a subscriber. 34commonEventManager.createSubscriber(subscribeInfo, createCB); 35``` 36 37## getCode 38 39getCode(callback: AsyncCallback\<number>): void 40 41Obtains the result code of an ordered common event. This API uses an asynchronous callback to return the result. 42 43**Atomic service API**: This API can be used in atomic services since API version 11. 44 45**System capability**: SystemCapability.Notification.CommonEvent 46 47**Parameters** 48 49| Name | Type | Mandatory | Description | 50| -------- | ---------------------- | ---- | ------------------ | 51| callback | AsyncCallback\<number\> | Yes | Common event code. | 52 53**Example** 54 55```ts 56// Callback for result code obtaining of an ordered common event. 57function getCodeCallback(err: BusinessError, code:number) { 58 if (err != null) { 59 console.error(`getCode failed, code is ${err.code}, message is ${err.message}`); 60 } else { 61 console.info("getCode " + JSON.stringify(code)); 62 } 63} 64subscriber.getCode(getCodeCallback); 65``` 66 67## getCode 68 69getCode(): Promise\<number> 70 71Obtains the result code of an ordered common event. This API uses a promise to return the result. 72 73**Atomic service API**: This API can be used in atomic services since API version 11. 74 75**System capability**: SystemCapability.Notification.CommonEvent 76 77**Return value** 78 79| Type | Description | 80| ---------------- | -------------------- | 81| Promise\<number> | Common event code. | 82 83**Example** 84 85```ts 86subscriber.getCode().then((code:number) => { 87 console.info("getCode " + JSON.stringify(code)); 88}).catch((err: BusinessError) => { 89 console.error(`getCode failed, code is ${err.code}, message is ${err.message}`); 90}); 91``` 92 93## getCodeSync<sup>10+</sup> 94 95getCodeSync(): number 96 97Obtains the result code of an ordered common event. 98 99**Atomic service API**: This API can be used in atomic services since API version 11. 100 101**System capability**: SystemCapability.Notification.CommonEvent 102 103**Return value** 104 105| Type | Description | 106| ---------------- | -------------------- | 107| number | Common event code. | 108 109**Example** 110 111```ts 112let code = subscriber.getCodeSync(); 113console.info("getCodeSync " + JSON.stringify(code)); 114``` 115 116## setCode 117 118setCode(code: number, callback: AsyncCallback\<void>): void 119 120Sets the result code of an ordered common event. This API uses an asynchronous callback to return the result. 121 122**Atomic service API**: This API can be used in atomic services since API version 11. 123 124**System capability**: SystemCapability.Notification.CommonEvent 125 126**Parameters** 127 128| Name | Type | Mandatory | Description | 129| -------- | -------------------- | ---- | ---------------------- | 130| code | number | Yes | Common event code. | 131| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 132 133**Example** 134 135```ts 136// Callback for result code setting of an ordered common event. 137function setCodeCallback(err: BusinessError) { 138 if (err != null) { 139 console.error(`setCode failed, code is ${err.code}, message is ${err.message}`); 140 } else { 141 console.info("setCode success"); 142 } 143} 144subscriber.setCode(1, setCodeCallback); 145``` 146 147## setCode 148 149setCode(code: number): Promise\<void> 150 151Sets the result code of an ordered common event. This API uses a promise to return the result. 152 153**Atomic service API**: This API can be used in atomic services since API version 11. 154 155**System capability**: SystemCapability.Notification.CommonEvent 156 157**Parameters** 158 159| Name | Type | Mandatory | Description | 160| ------ | ------ | ---- | ------------------ | 161| code | number | Yes | Common event code. | 162 163**Return value** 164 165| Type | Description | 166| ---------------- | -------------------- | 167| Promise\<void> | Promise used to return the result. | 168 169**Example** 170 171```ts 172subscriber.setCode(1).then(() => { 173 console.info("setCode success"); 174}).catch((err: BusinessError) => { 175 console.error(`setCode failed, code is ${err.code}, message is ${err.message}`); 176}); 177``` 178 179## setCodeSync<sup>10+</sup> 180 181setCodeSync(code: number): void 182 183Sets the result code of an ordered common event. 184 185**Atomic service API**: This API can be used in atomic services since API version 11. 186 187**System capability**: SystemCapability.Notification.CommonEvent 188 189**Parameters** 190 191| Name | Type | Mandatory | Description | 192| ------ | ------ | ---- | ------------------ | 193| code | number | Yes | Common event code. | 194 195**Error codes** 196 197For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 198 199| ID | Error Message | 200| -------- | ----------------------------------- | 201| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 202 203**Example** 204 205```ts 206 207try { 208 subscriber.setCodeSync(1); 209} catch (error) { 210 let err: BusinessError = error as BusinessError; 211 console.error(`setCodeSync failed, code is ${err.code}, message is ${err.message}`); 212} 213``` 214 215## getData 216 217getData(callback: AsyncCallback\<string>): void 218 219Obtains the result data of an ordered common event. This API uses an asynchronous callback to return the result. 220 221**Atomic service API**: This API can be used in atomic services since API version 11. 222 223**System capability**: SystemCapability.Notification.CommonEvent 224 225**Parameters** 226 227| Name | Type | Mandatory | Description | 228| -------- | ---------------------- | ---- | -------------------- | 229| callback | AsyncCallback\<string> | Yes | Common event data. | 230 231**Example** 232 233```ts 234// Callback for result data obtaining of an ordered common event. 235function getDataCallback(err: BusinessError, data:string) { 236 if (err != null) { 237 console.error(`getData failed, code is ${err.code}, message is ${err.message}`); 238 } else { 239 console.info("getData " + JSON.stringify(data)); 240 } 241} 242subscriber.getData(getDataCallback); 243``` 244 245## getData 246 247getData(): Promise\<string> 248 249Obtains the result data of an ordered common event. This API uses a promise to return the result. 250 251**Atomic service API**: This API can be used in atomic services since API version 11. 252 253**System capability**: SystemCapability.Notification.CommonEvent 254 255**Return value** 256 257| Type | Description | 258| ---------------- | ------------------ | 259| Promise\<string> | Common event data. | 260 261**Example** 262 263```ts 264subscriber.getData().then((data:string) => { 265 console.info("getData " + JSON.stringify(data)); 266}).catch((err: BusinessError) => { 267 console.error(`getData failed, code is ${err.code}, message is ${err.message}`); 268}); 269``` 270 271## getDataSync<sup>10+</sup> 272 273getDataSync(): string 274 275Obtains the result data of an ordered common event. 276 277**Atomic service API**: This API can be used in atomic services since API version 11. 278 279**System capability**: SystemCapability.Notification.CommonEvent 280 281**Return value** 282 283| Type | Description | 284| ---------------- | ------------------ | 285| string | Common event data. | 286 287**Example** 288 289```ts 290let data = subscriber.getDataSync(); 291console.info("getDataSync " + JSON.stringify(data)); 292``` 293 294## setData 295 296setData(data: string, callback: AsyncCallback\<void>): void 297 298Sets the result data for an ordered common event. This API uses an asynchronous callback to return the result. 299 300**Atomic service API**: This API can be used in atomic services since API version 11. 301 302**System capability**: SystemCapability.Notification.CommonEvent 303 304**Parameters** 305 306| Name | Type | Mandatory | Description | 307| -------- | -------------------- | ---- | -------------------- | 308| data | string | Yes | Common event data. | 309| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 310 311**Example** 312 313```ts 314// Callback for result data setting of an ordered common event 315function setDataCallback(err: BusinessError) { 316 if (err != null) { 317 console.error(`setData failed, code is ${err.code}, message is ${err.message}`); 318 } else { 319 console.info("setData success"); 320 } 321} 322subscriber.setData("publish_data_changed", setDataCallback); 323``` 324 325## setData 326 327setData(data: string): Promise\<void> 328 329Sets the result data for an ordered common event. This API uses a promise to return the result. 330 331**Atomic service API**: This API can be used in atomic services since API version 11. 332 333**System capability**: SystemCapability.Notification.CommonEvent 334 335**Parameters** 336 337| Name | Type | Mandatory | Description | 338| ------ | ------ | ---- | -------------------- | 339| data | string | Yes | Common event data. | 340 341**Return value** 342 343| Type | Description | 344| ---------------- | -------------------- | 345| Promise\<void> | Promise used to return the result. | 346 347**Example** 348 349```ts 350subscriber.setData("publish_data_changed").then(() => { 351 console.info("setData success"); 352}).catch((err: BusinessError) => { 353 console.error(`setData failed, code is ${err.code}, message is ${err.message}`); 354}); 355``` 356 357## setDataSync<sup>10+</sup> 358 359setDataSync(data: string): void 360 361Sets the result data for an ordered common event. 362 363**Atomic service API**: This API can be used in atomic services since API version 11. 364 365**System capability**: SystemCapability.Notification.CommonEvent 366 367**Parameters** 368 369| Name | Type | Mandatory | Description | 370| ------ | ------ | ---- | -------------------- | 371| data | string | Yes | Common event data. | 372 373**Error codes** 374 375For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 376 377| ID | Error Message | 378| -------- | ----------------------------------- | 379| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 380 381**Example** 382 383```ts 384try { 385 subscriber.setDataSync("publish_data_changed"); 386} catch (error) { 387 let err: BusinessError = error as BusinessError; 388 console.error(`setDataSync failed, code is ${err.code}, message is ${err.message}`); 389} 390``` 391 392## setCodeAndData 393 394setCodeAndData(code: number, data: string, callback:AsyncCallback\<void>): void 395 396Sets the result code and data of an ordered common event. This API uses an asynchronous callback to return the result. 397 398**Atomic service API**: This API can be used in atomic services since API version 11. 399 400**System capability**: SystemCapability.Notification.CommonEvent 401 402**Parameters** 403 404| Name | Type | Mandatory | Description | 405| -------- | -------------------- | ---- | ---------------------- | 406| code | number | Yes | Common event code. | 407| data | string | Yes | Common event data. | 408| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 409 410**Example** 411 412```ts 413// Callback for code and data setting of an ordered common event. 414function setCodeAndDataCallback(err: BusinessError) { 415 if (err != null) { 416 console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`); 417 } else { 418 console.info("setCodeAndData success"); 419 } 420} 421subscriber.setCodeAndData(1, "publish_data_changed", setCodeAndDataCallback); 422``` 423 424## setCodeAndData 425 426setCodeAndData(code: number, data: string): Promise\<void> 427 428Sets the result code and data of an ordered common event. This API uses a promise to return the result. 429 430**Atomic service API**: This API can be used in atomic services since API version 11. 431 432**System capability**: SystemCapability.Notification.CommonEvent 433 434**Parameters** 435 436| Name | Type | Mandatory | Description | 437| ------ | ------ | ---- | -------------------- | 438| code | number | Yes | Common event code. | 439| data | string | Yes | Common event data. | 440 441**Return value** 442 443| Type | Description | 444| ---------------- | -------------------- | 445| Promise\<void> | Promise used to return the result. | 446 447**Example** 448 449```ts 450subscriber.setCodeAndData(1, "publish_data_changed").then(() => { 451 console.info("setCodeAndData success"); 452}).catch((err: BusinessError) => { 453 console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`); 454}); 455``` 456 457## setCodeAndDataSync<sup>10+</sup> 458 459setCodeAndDataSync(code: number, data: string): void 460 461**Atomic service API**: This API can be used in atomic services since API version 11. 462 463Sets the result code and data of an ordered common event. 464 465**System capability**: SystemCapability.Notification.CommonEvent 466 467**Parameters** 468 469| Name | Type | Mandatory | Description | 470| ------ | ------ | ---- | -------------------- | 471| code | number | Yes | Common event code. | 472| data | string | Yes | Common event data. | 473 474**Error codes** 475 476For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 477 478| ID | Error Message | 479| -------- | ----------------------------------- | 480| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 481 482**Example** 483 484```ts 485try { 486 subscriber.setCodeAndDataSync(1, "publish_data_changed"); 487} catch (error) { 488 let err: BusinessError = error as BusinessError; 489 console.error(`setCodeAndDataSync failed, code is ${err.code}, message is ${err.message}`); 490} 491 492``` 493 494## isOrderedCommonEvent 495 496isOrderedCommonEvent(callback: AsyncCallback\<boolean>): void 497 498Checks whether the current common event is an ordered common event. This API uses an asynchronous callback to return the result. 499 500Returns **true** if the common event is an ordered one; returns **false** otherwise. 501 502**System capability**: SystemCapability.Notification.CommonEvent 503 504**Parameters** 505 506| Name | Type | Mandatory | Description | 507| -------- | ----------------------- | ---- | ---------------------------------- | 508| callback | AsyncCallback\<boolean> | Yes | Whether the current common event is an ordered one. | 509 510**Example** 511 512```ts 513// Callback for checking whether the current common event is an ordered one. 514function isOrderedCommonEventCallback(err: BusinessError, isOrdered:boolean) { 515 if (err != null) { 516 console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`); 517 } else { 518 console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered)); 519 } 520} 521subscriber.isOrderedCommonEvent(isOrderedCommonEventCallback); 522``` 523 524## isOrderedCommonEvent 525 526isOrderedCommonEvent(): Promise\<boolean> 527 528Checks whether the current common event is an ordered common event. This API uses a promise to return the result. 529 530Returns **true** if the common event is an ordered one; returns **false** otherwise. 531 532**System capability**: SystemCapability.Notification.CommonEvent 533 534**Return value** 535 536| Type | Description | 537| ----------------- | -------------------------------- | 538| Promise\<boolean> | Whether the current common event is an ordered one. | 539 540**Example** 541 542```ts 543subscriber.isOrderedCommonEvent().then((isOrdered:boolean) => { 544 console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered)); 545}).catch((err: BusinessError) => { 546 console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`); 547}); 548``` 549 550## isOrderedCommonEventSync<sup>10+</sup> 551 552isOrderedCommonEventSync(): boolean 553 554Checks whether the current common event is an ordered common event. 555 556Returns **true** if the common event is an ordered one; returns **false** otherwise. 557 558**System capability**: SystemCapability.Notification.CommonEvent 559 560**Return value** 561 562| Type | Description | 563| ----------------- | -------------------------------- | 564| boolean | Whether the current common event is an ordered one. | 565 566**Example** 567 568```ts 569let isOrdered = subscriber.isOrderedCommonEventSync(); 570console.info("isOrderedCommonEventSync " + JSON.stringify(isOrdered)); 571``` 572 573## isStickyCommonEvent 574 575isStickyCommonEvent(callback: AsyncCallback\<boolean>): void 576 577Checks whether a common event is a sticky one. This API uses an asynchronous callback to return the result. 578 579Returns **true** if the common event is a sticky one; returns **false** otherwise. 580 581**System capability**: SystemCapability.Notification.CommonEvent 582 583**Parameters** 584 585| Name | Type | Mandatory | Description | 586| -------- | ----------------------- | ---- | ---------------------------------- | 587| callback | AsyncCallback\<boolean> | Yes | Whether the current common event is a sticky one. | 588 589**Example** 590 591```ts 592// Callback for checking whether the current common event is a sticky one. 593function isStickyCommonEventCallback(err: BusinessError, isSticky:boolean) { 594 if (err != null) { 595 console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`); 596 } else { 597 console.info("isStickyCommonEvent " + JSON.stringify(isSticky)); 598 } 599} 600subscriber.isStickyCommonEvent(isStickyCommonEventCallback); 601``` 602 603## isStickyCommonEvent 604 605isStickyCommonEvent(): Promise\<boolean> 606 607Checks whether a common event is a sticky one. This API uses a promise to return the result. 608 609Returns **true** if the common event is a sticky one; returns **false** otherwise. 610 611**System capability**: SystemCapability.Notification.CommonEvent 612 613**Return value** 614 615| Type | Description | 616| ----------------- | -------------------------------- | 617| Promise\<boolean> | Whether the current common event is a sticky one. | 618 619**Example** 620 621```ts 622subscriber.isStickyCommonEvent().then((isSticky:boolean) => { 623 console.info("isStickyCommonEvent " + JSON.stringify(isSticky)); 624}).catch((err: BusinessError) => { 625 console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`); 626}); 627``` 628 629## isStickyCommonEventSync<sup>10+</sup> 630 631isStickyCommonEventSync(): boolean 632 633Checks whether a common event is a sticky one. 634 635Returns **true** if the common event is a sticky one; returns **false** otherwise. 636 637**System capability**: SystemCapability.Notification.CommonEvent 638 639**Return value** 640 641| Type | Description | 642| ----------------- | -------------------------------- | 643| boolean | Whether the current common event is a sticky one. | 644 645**Example** 646 647```ts 648let isSticky = subscriber.isStickyCommonEventSync(); 649console.info("isStickyCommonEventSync " + JSON.stringify(isSticky)); 650``` 651 652## abortCommonEvent 653 654abortCommonEvent(callback: AsyncCallback\<void>): void 655 656Aborts this common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. This API uses an asynchronous callback to return the result. 657 658**System capability**: SystemCapability.Notification.CommonEvent 659 660**Parameters** 661 662| Name | Type | Mandatory | Description | 663| -------- | -------------------- | ---- | -------------------- | 664| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 665 666**Example** 667 668```ts 669// Callback for common event aborting. 670function abortCommonEventCallback(err: BusinessError) { 671 if (err != null) { 672 console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 673 } else { 674 console.info("abortCommonEvent success"); 675 } 676} 677function finishCommonEventCallback(err: BusinessError) { 678 if (err != null) { 679 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 680 } else { 681 console.info("finishCommonEvent success"); 682 } 683} 684subscriber.abortCommonEvent(abortCommonEventCallback); 685subscriber.finishCommonEvent(finishCommonEventCallback); 686``` 687 688## abortCommonEvent 689 690abortCommonEvent(): Promise\<void> 691 692Aborts this common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. This API uses a promise to return the result. 693 694**System capability**: SystemCapability.Notification.CommonEvent 695 696**Return value** 697 698| Type | Description | 699| ---------------- | -------------------- | 700| Promise\<void> | Promise used to return the result. | 701 702**Example** 703 704```ts 705subscriber.abortCommonEvent().then(() => { 706 console.info("abortCommonEvent success"); 707}).catch((err: BusinessError) => { 708 console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 709}); 710subscriber.finishCommonEvent().then(() => { 711 console.info("finishCommonEvent success"); 712}).catch((err: BusinessError) => { 713 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 714}); 715``` 716 717## abortCommonEventSync<sup>10+</sup> 718 719abortCommonEventSync(): void 720 721Aborts this common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. 722 723**System capability**: SystemCapability.Notification.CommonEvent 724 725**Example** 726 727```ts 728subscriber.abortCommonEventSync(); 729subscriber.finishCommonEvent().then(() => { 730 console.info("finishCommonEvent success"); 731}).catch((err: BusinessError) => { 732 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 733}); 734``` 735 736## clearAbortCommonEvent 737 738clearAbortCommonEvent(callback: AsyncCallback\<void>): void 739 740Clears the aborted state of this common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. This API uses an asynchronous callback to return the result. 741 742**System capability**: SystemCapability.Notification.CommonEvent 743 744**Parameters** 745 746| Name | Type | Mandatory | Description | 747| -------- | -------------------- | ---- | -------------------- | 748| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 749 750**Example** 751 752```ts 753// Callback for clearing the aborted state of the current common event. 754function clearAbortCommonEventCallback(err: BusinessError) { 755 if (err != null) { 756 console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 757 } else { 758 console.info("clearAbortCommonEvent success"); 759 } 760} 761function finishCommonEventCallback(err: BusinessError) { 762 if (err != null) { 763 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 764 } else { 765 console.info("finishCommonEvent success"); 766 } 767} 768subscriber.clearAbortCommonEvent(clearAbortCommonEventCallback); 769subscriber.finishCommonEvent(finishCommonEventCallback); 770``` 771 772## clearAbortCommonEvent 773 774clearAbortCommonEvent(): Promise\<void> 775 776Clears the aborted state of this common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. This API uses a promise to return the result. 777 778**System capability**: SystemCapability.Notification.CommonEvent 779 780**Return value** 781 782| Type | Description | 783| ---------------- | -------------------- | 784| Promise\<void> | Promise used to return the result. | 785 786**Example** 787 788```ts 789subscriber.clearAbortCommonEvent().then(() => { 790 console.info("clearAbortCommonEvent success"); 791}).catch((err: BusinessError) => { 792 console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 793}); 794subscriber.finishCommonEvent().then(() => { 795 console.info("finishCommonEvent success"); 796}).catch((err: BusinessError) => { 797 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 798}); 799``` 800 801## clearAbortCommonEventSync<sup>10+</sup> 802 803clearAbortCommonEventSync(): void 804 805Clears the aborted state of this common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. 806 807**System capability**: SystemCapability.Notification.CommonEvent 808 809**Example** 810 811```ts 812subscriber.clearAbortCommonEventSync(); 813subscriber.finishCommonEvent().then(() => { 814 console.info("finishCommonEvent success"); 815}).catch((err: BusinessError) => { 816 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 817}); 818``` 819 820## getAbortCommonEvent 821 822getAbortCommonEvent(callback: AsyncCallback\<boolean>): void 823 824Checks whether this ordered common event should be aborted. This API uses an asynchronous callback to return the result. 825 826**System capability**: SystemCapability.Notification.CommonEvent 827 828**Parameters** 829 830| Name | Type | Mandatory | Description | 831| -------- | ----------------------- | ---- | ---------------------------------- | 832| callback | AsyncCallback\<boolean> | Yes | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise. | 833 834**Example** 835 836```ts 837// Callback for checking whether the current common event is in the aborted state. 838function getAbortCommonEventCallback(err: BusinessError, abortEvent:boolean) { 839 if (err != null) { 840 console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 841 } else { 842 console.info("getAbortCommonEvent " + JSON.stringify(abortEvent)); 843 } 844} 845subscriber.getAbortCommonEvent(getAbortCommonEventCallback); 846``` 847 848## getAbortCommonEvent 849 850getAbortCommonEvent(): Promise\<boolean> 851 852Checks whether this ordered common event should be aborted. This API uses a promise to return the result. 853 854**System capability**: SystemCapability.Notification.CommonEvent 855 856**Return value** 857 858| Type | Description | 859| ----------------- | ---------------------------------- | 860| Promise\<boolean> | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise. | 861 862**Example** 863 864```ts 865subscriber.getAbortCommonEvent().then((abortEvent:boolean) => { 866 console.info("getAbortCommonEvent " + JSON.stringify(abortEvent)); 867}).catch((err: BusinessError) => { 868 console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 869}); 870``` 871 872## getAbortCommonEventSync<sup>10+</sup> 873 874getAbortCommonEventSync(): boolean 875 876Checks whether this ordered common event should be aborted. 877 878**System capability**: SystemCapability.Notification.CommonEvent 879 880**Return value** 881 882| Type | Description | 883| ----------------- | ---------------------------------- | 884| boolean | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise. | 885 886**Example** 887 888```ts 889let abortEvent = subscriber.getAbortCommonEventSync(); 890console.info("getAbortCommonEventSync " + JSON.stringify(abortEvent)); 891``` 892 893## getSubscribeInfo 894 895getSubscribeInfo(callback: AsyncCallback\<CommonEventSubscribeInfo>): void 896 897Obtains the subscriber information. This API uses an asynchronous callback to return the result. 898 899**Atomic service API**: This API can be used in atomic services since API version 11. 900 901**System capability**: SystemCapability.Notification.CommonEvent 902 903**Parameters** 904 905| Name | Type | Mandatory | Description | 906| -------- | ------------------------------------------------------------ | ---- | ---------------------- | 907| callback | AsyncCallback\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Yes | Returns the subscriber information. | 908 909**Example** 910 911```ts 912// Callback for subscriber information obtaining. 913function getSubscribeInfoCallback(err: BusinessError, subscribeInfo:commonEventManager.CommonEventSubscribeInfo) { 914 if (err != null) { 915 console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`); 916 } else { 917 console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo)); 918 } 919} 920subscriber.getSubscribeInfo(getSubscribeInfoCallback); 921``` 922 923## getSubscribeInfo 924 925getSubscribeInfo(): Promise\<CommonEventSubscribeInfo> 926 927Obtains the subscriber information. This API uses a promise to return the result. 928 929**Atomic service API**: This API can be used in atomic services since API version 11. 930 931**System capability**: SystemCapability.Notification.CommonEvent 932 933**Return value** 934 935| Type | Description | 936| ------------------------------------------------------------ | ---------------------- | 937| Promise\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Returns the subscriber information. | 938 939**Example** 940 941```ts 942subscriber.getSubscribeInfo().then((subscribeInfo:commonEventManager.CommonEventSubscribeInfo) => { 943 console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo)); 944}).catch((err: BusinessError) => { 945 console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`); 946}); 947``` 948 949## getSubscribeInfoSync<sup>10+</sup> 950 951getSubscribeInfoSync(): CommonEventSubscribeInfo 952 953Obtains the subscriber information. 954 955**Atomic service API**: This API can be used in atomic services since API version 11. 956 957**System capability**: SystemCapability.Notification.CommonEvent 958 959**Return value** 960 961| Type | Description | 962| ------------------------------------------------------------ | ---------------------- | 963| [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Returns the subscriber information. | 964 965**Example** 966 967```ts 968let subscribeInfo = subscriber.getSubscribeInfoSync(); 969console.info("getSubscribeInfoSync " + JSON.stringify(subscribeInfo)); 970``` 971 972## finishCommonEvent<sup>9+</sup> 973 974finishCommonEvent(callback: AsyncCallback\<void>): void 975 976Finishes this ordered common event. This API uses an asynchronous callback to return the result. 977 978**System capability**: SystemCapability.Notification.CommonEvent 979 980**Parameters** 981 982| Name | Type | Mandatory | Description | 983| -------- | -------------------- | ---- | -------------------------------- | 984| callback | AsyncCallback\<void> | Yes | Callback returned after the ordered common event is finished. | 985 986**Example** 987 988```ts 989// Callback for ordered common event finishing. 990function finishCommonEventCallback(err: BusinessError) { 991 if (err != null) { 992 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 993 } else { 994 console.info("finishCommonEvent success"); 995 } 996} 997subscriber.finishCommonEvent(finishCommonEventCallback); 998``` 999 1000## finishCommonEvent<sup>9+</sup> 1001 1002finishCommonEvent(): Promise\<void> 1003 1004Finishes this ordered common event. This API uses a promise to return the result. 1005 1006**System capability**: SystemCapability.Notification.CommonEvent 1007 1008**Return value** 1009 1010| Type | Description | 1011| ---------------- | -------------------- | 1012| Promise\<void> | Promise used to return the result. | 1013 1014**Example** 1015 1016```ts 1017subscriber.finishCommonEvent().then(() => { 1018 console.info("finishCommonEvent success"); 1019}).catch((err: BusinessError) => { 1020 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 1021}); 1022``` 1023