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