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