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