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