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