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