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