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