1# @ohos.commonEvent (公共事件模块) 2 3本模块提供了公共事件的能力,包括公共事件的权限列表,发布公共事件,订阅或取消订阅公共事件,获取或修改公共事件结果代码、结果数据等。 4 5> **说明:** 6> - 从API Version 9开始,该接口不再维护,推荐使用新接口[@ohos.commonEventManager](js-apis-commonEventManager.md)。 7> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```js 12import CommonEvent from '@ohos.commonEvent'; 13``` 14 15## Support 16 17系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。 18 19全部系统公共事件枚举定义请参见[系统公共事件定义](./commonEvent-definitions.md)。 20 21## CommonEvent.publish 22 23publish(event: string, callback: AsyncCallback\<void>): void 24 25发布公共事件(callback形式)。 26 27**系统能力:** SystemCapability.Notification.CommonEvent 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| -------- | -------------------- | ---- | ---------------------- | 33| event | string | 是 | 表示要发送的公共事件。 | 34| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 35 36**示例:** 37 38```js 39//发布公共事件回调 40function publishCallBack(err) { 41 if (err.code) { 42 console.error("publish failed " + JSON.stringify(err)); 43 } else { 44 console.info("publish"); 45 } 46} 47 48//发布公共事件 49CommonEvent.publish("event", publishCallBack); 50``` 51 52 53 54## CommonEvent.publish 55 56publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 57 58发布公共事件指定发布信息(callback形式)。 59 60**系统能力:** SystemCapability.Notification.CommonEvent 61 62**参数:** 63 64| 参数名 | 类型 | 必填 | 说明 | 65| -------- | ---------------------- | ---- | ---------------------- | 66| event | string | 是 | 表示要发布的公共事件。 | 67| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是 | 表示发布公共事件的属性。 | 68| callback | syncCallback\<void> | 是 | 表示被指定的回调方法。 | 69 70**示例:** 71 72 73```js 74//公共事件相关信息 75let options = { 76 code: 0, //公共事件的初始代码 77 data: "initial data",//公共事件的初始数据 78 isOrdered: true //有序公共事件 79} 80 81//发布公共事件回调 82function publishCallBack(err) { 83 if (err.code) { 84 console.error("publish failed " + JSON.stringify(err)); 85 } else { 86 console.info("publish"); 87 } 88} 89 90//发布公共事件 91CommonEvent.publish("event", options, publishCallBack); 92``` 93 94 95 96## CommonEvent.publishAsUser<sup>8+</sup> 97 98publishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void 99 100向指定用户发布公共事件(callback形式)。 101 102**系统能力:** SystemCapability.Notification.CommonEvent 103 104**系统API**:此接口为系统接口,三方应用不支持调用。 105 106**参数:** 107 108| 参数名 | 类型 | 必填 | 说明 | 109| -------- | -------------------- | ---- | ---------------------------------- | 110| event | string | 是 | 表示要发送的公共事件。 | 111| userId | number | 是 | 表示指定向该用户ID发送此公共事件。 | 112| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 113 114**示例:** 115 116```js 117//发布公共事件回调 118function publishAsUserCallBack(err) { 119 if (err.code) { 120 console.error("publishAsUser failed " + JSON.stringify(err)); 121 } else { 122 console.info("publishAsUser"); 123 } 124} 125 126//指定发送的用户 127let userId = 100; 128 129//发布公共事件 130CommonEvent.publishAsUser("event", userId, publishAsUserCallBack); 131``` 132 133 134 135## CommonEvent.publishAsUser<sup>8+</sup> 136 137publishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 138 139向指定用户发布公共事件并指定发布信息(callback形式)。 140 141**系统能力:** SystemCapability.Notification.CommonEvent 142 143**系统API**:此接口为系统接口,三方应用不支持调用。 144 145**参数:** 146 147| 参数名 | 类型 | 必填 | 说明 | 148| -------- | ---------------------- | ---- | ---------------------- | 149| event | string | 是 | 表示要发布的公共事件。 | 150| userId | number | 是 | 表示指定向该用户ID发送此公共事件。 | 151| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是 | 表示发布公共事件的属性。 | 152| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 153 154**示例:** 155 156 157```js 158//公共事件相关信息 159let options = { 160 code: 0, //公共事件的初始代码 161 data: "initial data",//公共事件的初始数据 162} 163 164//发布公共事件回调 165function publishAsUserCallBack(err) { 166 if (err.code) { 167 console.error("publishAsUser failed " + JSON.stringify(err)); 168 } else { 169 console.info("publishAsUser"); 170 } 171} 172 173//指定发送的用户 174let userId = 100; 175 176//发布公共事件 177CommonEvent.publishAsUser("event", userId, options, publishAsUserCallBack); 178``` 179 180 181 182## CommonEvent.createSubscriber 183 184createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void 185 186创建订阅者(callback形式)。 187 188**系统能力:** SystemCapability.Notification.CommonEvent 189 190**参数:** 191 192| 参数名 | 类型 | 必填 | 说明 | 193| ------------- | ------------------------------------------------------------ | ---- | -------------------------- | 194| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | 195| callback | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是 | 表示创建订阅者的回调方法。 | 196 197**示例:** 198 199 200```js 201let subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 202 203//订阅者信息 204let subscribeInfo = { 205 events: ["event"] 206}; 207 208//创建订阅者回调 209function createSubscriberCallBack(err, commonEventSubscriber) { 210 if (err.code) { 211 console.error("createSubscriber failed " + JSON.stringify(err)); 212 } else { 213 console.info("createSubscriber"); 214 subscriber = commonEventSubscriber; 215 } 216} 217 218//创建订阅者 219CommonEvent.createSubscriber(subscribeInfo, createSubscriberCallBack); 220``` 221 222 223 224## CommonEvent.createSubscriber 225 226createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber> 227 228创建订阅者(Promise形式)。 229 230**系统能力:** SystemCapability.Notification.CommonEvent 231 232**参数:** 233 234| 参数名 | 类型 | 必填 | 说明 | 235| ------------- | ----------------------------------------------------- | ---- | -------------- | 236| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | 237 238**返回值:** 239| 类型 | 说明 | 240| --------------------------------------------------------- | ---------------- | 241| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 返回订阅者对象。 | 242 243**示例:** 244 245```js 246let subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 247 248//订阅者信息 249let subscribeInfo = { 250 events: ["event"] 251}; 252 253//创建订阅者 254CommonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber) => { 255 console.info("createSubscriber"); 256 subscriber = commonEventSubscriber; 257}).catch((err) => { 258 console.error("createSubscriber failed " + JSON.stringify(err)); 259}); 260``` 261 262 263 264## CommonEvent.subscribe 265 266subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void 267 268订阅公共事件(callback形式)。 269 270**系统能力:** SystemCapability.Notification.CommonEvent 271 272**参数:** 273 274| 参数名 | 类型 | 必填 | 说明 | 275| ---------- | ---------------------------------------------------- | ---- | -------------------------------- | 276| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | 277| callback | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是 | 表示接收公共事件数据的回调函数。 | 278 279**示例:** 280 281```js 282let subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 283 284//订阅者信息 285let subscribeInfo = { 286 events: ["event"] 287}; 288 289//订阅公共事件回调 290function subscribeCallBack(err, data) { 291 if (err.code) { 292 console.error("subscribe failed " + JSON.stringify(err)); 293 } else { 294 console.info("subscribe " + JSON.stringify(data)); 295 } 296} 297 298//创建订阅者回调 299function createSubscriberCallBack(err, commonEventSubscriber) { 300 if (err.code) { 301 console.error("createSubscriber failed " + JSON.stringify(err)); 302 } else { 303 console.info("createSubscriber"); 304 subscriber = commonEventSubscriber; 305 //订阅公共事件 306 CommonEvent.subscribe(subscriber, subscribeCallBack); 307 } 308} 309 310//创建订阅者 311CommonEvent.createSubscriber(subscribeInfo, createSubscriberCallBack); 312``` 313 314 315 316## CommonEvent.unsubscribe 317 318unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void 319 320取消订阅公共事件(callback形式)。 321 322**系统能力:** SystemCapability.Notification.CommonEvent 323 324**参数:** 325 326| 参数名 | 类型 | 必填 | 说明 | 327| ---------- | ----------------------------------------------- | ---- | ------------------------ | 328| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | 329| callback | AsyncCallback\<void> | 否 | 表示取消订阅的回调方法。 | 330 331**示例:** 332 333```js 334let subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 335 336//订阅者信息 337let subscribeInfo = { 338 events: ["event"] 339}; 340 341//订阅公共事件回调 342function subscribeCallBack(err, data) { 343 if (err.code) { 344 console.info("subscribe failed " + JSON.stringify(err)); 345 } else { 346 console.info("subscribe " + JSON.stringify(data)); 347 } 348} 349 350//创建订阅者回调 351function createSubscriberCallBack(err, commonEventSubscriber) { 352 if (err.code) { 353 console.info("createSubscriber failed " + JSON.stringify(err)); 354 } else { 355 console.info("createSubscriber"); 356 subscriber = commonEventSubscriber; 357 //订阅公共事件 358 CommonEvent.subscribe(subscriber, subscribeCallBack); 359 } 360} 361 362//取消订阅公共事件回调 363function unsubscribeCallBack(err) { 364 if (err.code) { 365 console.info("unsubscribe failed " + JSON.stringify(err)); 366 } else { 367 console.info("unsubscribe"); 368 } 369} 370 371//创建订阅者 372CommonEvent.createSubscriber(subscribeInfo, createSubscriberCallBack); 373 374//取消订阅公共事件 375CommonEvent.unsubscribe(subscriber, unsubscribeCallBack); 376``` 377 378## CommonEventSubscriber 379 380### getCode 381 382getCode(callback: AsyncCallback\<number>): void 383 384获取公共事件的结果代码(callback形式)。 385 386**系统能力**:SystemCapability.Notification.CommonEvent 387 388**参数:** 389 390| 参数名 | 类型 | 必填 | 说明 | 391| -------- | ---------------------- | ---- | ------------------ | 392| callback | AsyncCallback\<number> | 是 | 公共事件的结果代码。 | 393 394**示例:** 395 396```js 397let subscriber; //创建成功的订阅者对象 398 399//获取有序公共事件的结果代码回调 400function getCodeCallback(err, Code) { 401 if (err.code) { 402 console.error("getCode failed " + JSON.stringify(err)); 403 } else { 404 console.info("getCode " + JSON.stringify(Code)); 405 } 406} 407subscriber.getCode(getCodeCallback); 408``` 409 410### getCode 411 412getCode(): Promise\<number> 413 414获取公共事件的结果代码(Promise形式)。 415 416**系统能力**:SystemCapability.Notification.CommonEvent 417 418**返回值:** 419 420| 类型 | 说明 | 421| ---------------- | -------------------- | 422| Promise\<number> | 公共事件的结果代码。 | 423 424**示例:** 425 426```js 427let subscriber; //创建成功的订阅者对象 428 429subscriber.getCode().then((Code) => { 430 console.info("getCode " + JSON.stringify(Code)); 431}).catch((err) => { 432 console.error("getCode failed " + JSON.stringify(err)); 433}); 434``` 435 436### setCode 437 438setCode(code: number, callback: AsyncCallback\<void>): void 439 440设置公共事件的结果代码(callback形式)。 441 442**系统能力**:SystemCapability.Notification.CommonEvent 443 444**参数:** 445 446| 参数名 | 类型 | 必填 | 说明 | 447| -------- | -------------------- | ---- | ---------------------- | 448| code | number | 是 | 公共事件的结果代码。 | 449| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 450 451**示例:** 452 453```js 454let subscriber; //创建成功的订阅者对象 455 456//设置有序公共事件的结果代码回调 457function setCodeCallback(err) { 458 if (err.code) { 459 console.error("setCode failed " + JSON.stringify(err)); 460 } else { 461 console.info("setCode"); 462 } 463} 464subscriber.setCode(1, setCodeCallback); 465``` 466 467### setCode 468 469setCode(code: number): Promise\<void> 470 471设置公共事件的结果代码(Promise形式)。 472 473**系统能力**:SystemCapability.Notification.CommonEvent 474 475**参数:** 476 477| 参数名 | 类型 | 必填 | 说明 | 478| ------ | ------ | ---- | ------------------ | 479| code | number | 是 | 公共事件的结果代码。 | 480 481**返回值:** 482 483| 类型 | 说明 | 484| ---------------- | -------------------- | 485| Promise\<void> | 返回一个Promise的结果。 | 486 487**示例:** 488 489```js 490let subscriber; //创建成功的订阅者对象 491 492subscriber.setCode(1).then(() => { 493 console.info("setCode"); 494}).catch((err) => { 495 console.error("setCode failed " + JSON.stringify(err)); 496}); 497``` 498 499### getData 500 501getData(callback: AsyncCallback\<string>): void 502 503获取公共事件的结果数据(callback形式)。 504 505**系统能力**:SystemCapability.Notification.CommonEvent 506 507**参数:** 508 509| 参数名 | 类型 | 必填 | 说明 | 510| -------- | ---------------------- | ---- | -------------------- | 511| callback | AsyncCallback\<string> | 是 | 公共事件的结果数据。 | 512 513**示例:** 514 515```js 516let subscriber; //创建成功的订阅者对象 517 518//获取有序公共事件的结果数据回调 519function getDataCallback(err, Data) { 520 if (err.code) { 521 console.error("getData failed " + JSON.stringify(err)); 522 } else { 523 console.info("getData " + JSON.stringify(Data)); 524 } 525} 526subscriber.getData(getDataCallback); 527``` 528 529### getData 530 531getData(): Promise\<string> 532 533获取公共事件的结果数据(Promise形式)。 534 535**系统能力**:SystemCapability.Notification.CommonEvent 536 537**返回值:** 538 539| 类型 | 说明 | 540| ---------------- | ------------------ | 541| Promise\<string> | 公共事件的结果数据。 | 542 543**示例:** 544 545```js 546let subscriber; //创建成功的订阅者对象 547 548subscriber.getData().then((Data) => { 549 console.info("getData " + JSON.stringify(Data)); 550}).catch((err) => { 551 console.error("getData failed " + JSON.stringify(err)); 552}); 553``` 554 555### setData 556 557setData(data: string, callback: AsyncCallback\<void>): void 558 559设置公共事件的结果数据(callback形式)。 560 561**系统能力**:SystemCapability.Notification.CommonEvent 562 563**参数:** 564 565| 参数名 | 类型 | 必填 | 说明 | 566| -------- | -------------------- | ---- | -------------------- | 567| data | string | 是 | 公共事件的结果数据。 | 568| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 569 570**示例:** 571 572```js 573let subscriber; //创建成功的订阅者对象 574 575//设置有序公共事件的结果数据回调 576function setDataCallback(err) { 577 if (err.code) { 578 console.error("setData failed " + JSON.stringify(err)); 579 } else { 580 console.info("setData"); 581 } 582} 583subscriber.setData("publish_data_changed", setDataCallback); 584``` 585 586### setData 587 588setData(data: string): Promise\<void> 589 590设置公共事件的结果数据(Promise形式)。 591 592**系统能力**:SystemCapability.Notification.CommonEvent 593 594**参数:** 595 596| 参数名 | 类型 | 必填 | 说明 | 597| ------ | ------ | ---- | -------------------- | 598| data | string | 是 | 公共事件的结果数据。 | 599 600**返回值:** 601 602| 类型 | 说明 | 603| ---------------- | -------------------- | 604| Promise\<void> | 返回一个Promise的结果。 | 605 606**示例:** 607 608```js 609let subscriber; //创建成功的订阅者对象 610 611subscriber.setData("publish_data_changed").then(() => { 612 console.info("setData"); 613}).catch((err) => { 614 console.error("setData failed " + JSON.stringify(err)); 615}); 616``` 617 618### setCodeAndData 619 620setCodeAndData(code: number, data: string, callback:AsyncCallback\<void>): void 621 622设置公共事件的结果代码和结果数据(callback形式)。 623 624**系统能力**:SystemCapability.Notification.CommonEvent 625 626**参数:** 627 628| 参数名 | 类型 | 必填 | 说明 | 629| -------- | -------------------- | ---- | ---------------------- | 630| code | number | 是 | 公共事件的结果代码。 | 631| data | string | 是 | 公共事件的结果数据。 | 632| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 633 634**示例:** 635 636```js 637let subscriber; //创建成功的订阅者对象 638 639//设置有序公共事件的结果代码和结果数据回调 640function setCodeDataCallback(err) { 641 if (err.code) { 642 console.error("setCodeAndData failed " + JSON.stringify(err)); 643 } else { 644 console.info("setCodeDataCallback"); 645 } 646} 647subscriber.setCodeAndData(1, "publish_data_changed", setCodeDataCallback); 648``` 649 650### setCodeAndData 651 652setCodeAndData(code: number, data: string): Promise\<void> 653 654设置公共事件的结果代码和结果数据(Promise形式)。 655 656**系统能力**:SystemCapability.Notification.CommonEvent 657 658**参数:** 659 660| 参数名 | 类型 | 必填 | 说明 | 661| ------ | ------ | ---- | -------------------- | 662| code | number | 是 | 公共事件的结果代码。 | 663| data | string | 是 | 公共事件的结果数据。 | 664 665**返回值:** 666 667| 类型 | 说明 | 668| ---------------- | -------------------- | 669| Promise\<void> | 返回一个Promise的结果。 | 670 671**示例:** 672 673```js 674let subscriber; //创建成功的订阅者对象 675 676subscriber.setCodeAndData(1, "publish_data_changed").then(() => { 677 console.info("setCodeAndData"); 678}).catch((err) => { 679 console.info("setCodeAndData failed " + JSON.stringify(err)); 680}); 681``` 682 683### isOrderedCommonEvent 684 685isOrderedCommonEvent(callback: AsyncCallback\<boolean>): void 686 687查询当前公共事件的是否为有序公共事件(callback形式)。 688 689返回true代表是有序公共事件,false代表不是有序公共事件。 690 691**系统能力**:SystemCapability.Notification.CommonEvent 692 693**参数:** 694 695| 参数名 | 类型 | 必填 | 说明 | 696| -------- | ----------------------- | ---- | ---------------------------------- | 697| callback | AsyncCallback\<boolean> | 是 | 当前公共事件的是否为有序公共事件。 | 698 699**示例:** 700 701```js 702let subscriber; //创建成功的订阅者对象 703 704//获取当前公共事件是否为有序事件的回调 705function isOrderedCallback(err, isOrdered) { 706 if (err.code) { 707 console.error("isOrderedCommonEvent failed " + JSON.stringify(err)); 708 } else { 709 console.info("isOrdered " + JSON.stringify(isOrdered)); 710 } 711} 712subscriber.isOrderedCommonEvent(isOrderedCallback); 713``` 714 715### isOrderedCommonEvent 716 717isOrderedCommonEvent(): Promise\<boolean> 718 719查询当前公共事件的是否为有序公共事件(Promise形式)。 720 721返回true代表是有序公共事件,false代表不是有序公共事件。 722 723**系统能力**:SystemCapability.Notification.CommonEvent 724 725**返回值:** 726 727| 类型 | 说明 | 728| ----------------- | -------------------------------- | 729| Promise\<boolean> | 当前公共事件的是否为有序公共事件。 | 730 731**示例:** 732 733```js 734let subscriber; //创建成功的订阅者对象 735 736subscriber.isOrderedCommonEvent().then((isOrdered) => { 737 console.info("isOrdered " + JSON.stringify(isOrdered)); 738}).catch((err) => { 739 console.error("isOrdered failed " + JSON.stringify(err)); 740}); 741``` 742 743### isStickyCommonEvent 744 745isStickyCommonEvent(callback: AsyncCallback\<boolean>): void 746 747检查当前公共事件是否为一个粘性事件(callback形式)。 748 749返回true代表是粘性公共事件,false代表不是粘性公共事件。 750 751**系统能力**:SystemCapability.Notification.CommonEvent 752 753**参数:** 754 755| 参数名 | 类型 | 必填 | 说明 | 756| -------- | ----------------------- | ---- | ---------------------------------- | 757| callback | AsyncCallback\<boolean> | 是 | 当前公共事件的是否为粘性公共事件。 | 758 759**示例:** 760 761```js 762let subscriber; //创建成功的订阅者对象 763 764//获取当前公共事件是否为粘性事件的回调 765function isStickyCallback(err, isSticky) { 766 if (err.code) { 767 console.error("isStickyCommonEvent failed " + JSON.stringify(err)); 768 } else { 769 console.info("isSticky " + JSON.stringify(isSticky)); 770 } 771} 772subscriber.isStickyCommonEvent(isStickyCallback); 773``` 774 775### isStickyCommonEvent 776 777isStickyCommonEvent(): Promise\<boolean> 778 779检查当前公共事件是否为一个粘性事件(Promise形式)。 780 781返回true代表是粘性公共事件,false代表不是粘性公共事件。 782 783**系统能力**:SystemCapability.Notification.CommonEvent 784 785**返回值:** 786 787| 类型 | 说明 | 788| ----------------- | -------------------------------- | 789| Promise\<boolean> | 当前公共事件的是否为粘性公共事件。 | 790 791**示例:** 792 793```js 794let subscriber; //创建成功的订阅者对象 795 796subscriber.isStickyCommonEvent().then((isSticky) => { 797 console.info("isSticky " + JSON.stringify(isSticky)); 798}).catch((err) => { 799 console.error("isSticky failed " + JSON.stringify(err)); 800}); 801``` 802 803### abortCommonEvent 804 805abortCommonEvent(callback: AsyncCallback\<void>): void 806 807取消当前的公共事件,仅对有序公共事件有效,取消后,公共事件不再向下一个订阅者传递(callback形式)。 808 809**系统能力**:SystemCapability.Notification.CommonEvent 810 811**参数:** 812 813| 参数名 | 类型 | 必填 | 说明 | 814| -------- | -------------------- | ---- | -------------------- | 815| callback | AsyncCallback\<void> | 是 | 取消当前的公共事件。 | 816 817**示例:** 818 819```js 820let subscriber; //创建成功的订阅者对象 821 822//取消当前有序公共事件的回调 823function abortCallback(err) { 824 if (err.code) { 825 console.error("abortCommonEvent failed " + JSON.stringify(err)); 826 } else { 827 console.info("abortCommonEvent"); 828 } 829} 830subscriber.abortCommonEvent(abortCallback); 831``` 832 833### abortCommonEvent 834 835abortCommonEvent(): Promise\<void> 836 837取消当前的公共事件,仅对有序公共事件有效,取消后,公共事件不再向下一个订阅者传递(Promise形式)。 838 839**系统能力**:SystemCapability.Notification.CommonEvent 840 841**返回值:** 842 843| 类型 | 说明 | 844| ---------------- | -------------------- | 845| Promise\<void> | 返回一个Promise的结果。 | 846 847**示例:** 848 849```js 850let subscriber; //创建成功的订阅者对象 851 852subscriber.abortCommonEvent().then(() => { 853 console.info("abortCommonEvent"); 854}).catch((err) => { 855 console.error("abortCommonEvent failed " + JSON.stringify(err)); 856}); 857``` 858 859### clearAbortCommonEvent 860 861clearAbortCommonEvent(callback: AsyncCallback\<void>): void 862 863清除当前公共事件的取消状态,仅对有序公共事件有效(callback形式)。 864 865**系统能力**:SystemCapability.Notification.CommonEvent 866 867**参数:** 868 869| 参数名 | 类型 | 必填 | 说明 | 870| -------- | -------------------- | ---- | -------------------- | 871| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 872 873**示例:** 874 875```js 876let subscriber; //创建成功的订阅者对象 877 878//清除当前公共事件取消状态的回调 879function clearAbortCallback(err) { 880 if (err.code) { 881 console.error("clearAbortCommonEvent failed " + JSON.stringify(err)); 882 } else { 883 console.info("clearAbortCommonEvent"); 884 } 885} 886subscriber.clearAbortCommonEvent(clearAbortCallback); 887``` 888 889### clearAbortCommonEvent 890 891clearAbortCommonEvent(): Promise\<void> 892 893清除当前公共事件的取消状态,仅对有序公共事件有效(Promise形式)。 894 895**系统能力**:SystemCapability.Notification.CommonEvent 896 897**返回值:** 898 899| 类型 | 说明 | 900| ---------------- | -------------------- | 901| Promise\<void> | 返回一个Promise的结果。 | 902 903**示例:** 904 905```js 906let subscriber; //创建成功的订阅者对象 907 908subscriber.clearAbortCommonEvent().then(() => { 909 console.info("clearAbortCommonEvent"); 910}).catch((err) => { 911 console.error("clearAbortCommonEvent failed " + JSON.stringify(err)); 912}); 913``` 914 915### getAbortCommonEvent 916 917getAbortCommonEvent(callback: AsyncCallback\<boolean>): void 918 919获取当前有序公共事件是否取消的状态(callback形式)。 920 921**系统能力**:SystemCapability.Notification.CommonEvent 922 923**参数:** 924 925| 参数名 | 类型 | 必填 | 说明 | 926| -------- | ----------------------- | ---- | ---------------------------------- | 927| callback | AsyncCallback\<boolean> | 是 | 表示当前有序公共事件是否取消的状态。 | 928 929**示例:** 930 931```js 932let subscriber; //创建成功的订阅者对象 933 934//获取当前有序公共事件是否取消的回调 935function getAbortCallback(err, AbortCommonEvent) { 936 if (err.code) { 937 console.error("getAbortCommonEvent failed " + JSON.stringify(err)); 938 } else { 939 console.info("AbortCommonEvent " + AbortCommonEvent) 940 } 941} 942subscriber.getAbortCommonEvent(getAbortCallback); 943``` 944 945### getAbortCommonEvent 946 947getAbortCommonEvent(): Promise\<boolean> 948 949获取当前有序公共事件是否取消的状态(Promise形式)。 950 951**系统能力**:SystemCapability.Notification.CommonEvent 952 953**返回值:** 954 955| 类型 | 说明 | 956| ----------------- | ---------------------------------- | 957| Promise\<boolean> | 表示当前有序公共事件是否取消的状态。 | 958 959**示例:** 960 961```js 962let subscriber; //创建成功的订阅者对象 963 964subscriber.getAbortCommonEvent().then((AbortCommonEvent) => { 965 console.info("AbortCommonEvent " + JSON.stringify(AbortCommonEvent)); 966}).catch((err) => { 967 console.error("getAbortCommonEvent failed " + JSON.stringify(err)); 968}); 969``` 970 971### getSubscribeInfo 972 973getSubscribeInfo(callback: AsyncCallback\<CommonEventSubscribeInfo>): void 974 975获取订阅者的订阅信息(callback形式)。 976 977**系统能力**:SystemCapability.Notification.CommonEvent 978 979**参数:** 980 981| 参数名 | 类型 | 必填 | 说明 | 982| -------- | ------------------------------------------------------------ | ---- | ---------------------- | 983| callback | AsyncCallback\<[CommonEventSubscribeInfo](#commoneventsubscribeinfo)> | 是 | 表示订阅者的订阅信息。 | 984 985**示例:** 986 987```js 988let subscriber; //创建成功的订阅者对象 989 990//获取订阅者信息回调 991function getSubscribeInfoCallback(err, SubscribeInfo) { 992 if (err.code) { 993 console.error("getSubscribeInfo failed " + JSON.stringify(err)); 994 } else { 995 console.info("SubscribeInfo " + JSON.stringify(SubscribeInfo)); 996 } 997} 998subscriber.getSubscribeInfo(getSubscribeInfoCallback); 999``` 1000 1001### getSubscribeInfo 1002 1003getSubscribeInfo(): Promise\<CommonEventSubscribeInfo> 1004 1005获取订阅者的订阅信息(Promise形式)。 1006 1007**系统能力**:SystemCapability.Notification.CommonEvent 1008 1009**返回值:** 1010 1011| 类型 | 说明 | 1012| ------------------------------------------------------------ | ---------------------- | 1013| Promise\<[CommonEventSubscribeInfo](#commoneventsubscribeinfo)> | 表示订阅者的订阅信息。 | 1014 1015**示例:** 1016 1017```js 1018let subscriber; //创建成功的订阅者对象 1019 1020subscriber.getSubscribeInfo().then((SubscribeInfo) => { 1021 console.info("SubscribeInfo " + JSON.stringify(SubscribeInfo)); 1022}).catch((err) => { 1023 console.error("getSubscribeInfo failed " + JSON.stringify(err)); 1024}); 1025``` 1026 1027### finishCommonEvent<sup>9+</sup> 1028 1029finishCommonEvent(callback: AsyncCallback\<void\>): void 1030 1031结束当前有序公共事件(callback形式)。 1032 1033**系统能力**:SystemCapability.Notification.CommonEvent 1034 1035**参数:** 1036 1037| 参数名 | 类型 | 必填 | 说明 | 1038| -------- | -------------------- | ---- | -------------------------------- | 1039| callback | AsyncCallback\<void> | 是 | 表示有序公共事件结束后的回调函数。 | 1040 1041**示例:** 1042 1043```js 1044let subscriber; //创建成功的订阅者对象 1045 1046//结束当前有序公共事件的回调 1047function finishCommonEventCallback(err) { 1048 if (err.code) { 1049 console.error("finishCommonEvent failed " + JSON.stringify(err)); 1050} else { 1051 console.info("FinishCommonEvent"); 1052} 1053} 1054subscriber.finishCommonEvent(finishCommonEventCallback); 1055``` 1056 1057### finishCommonEvent<sup>9+</sup> 1058 1059finishCommonEvent(): Promise\<void\> 1060 1061结束当前有序公共事件(Promise形式)。 1062 1063**系统能力**:SystemCapability.Notification.CommonEvent 1064 1065**返回值:** 1066 1067| 类型 | 说明 | 1068| ---------------- | -------------------- | 1069| Promise\<void> | 返回一个Promise的结果。 | 1070 1071**示例:** 1072 1073```js 1074let subscriber; //创建成功的订阅者对象 1075 1076subscriber.finishCommonEvent().then(() => { 1077 console.info("FinishCommonEvent"); 1078}).catch((err) => { 1079 console.error("finishCommonEvent failed " + JSON.stringify(err)); 1080}); 1081``` 1082 1083## CommonEventData 1084 1085公共事件数据体。 1086 1087**系统能力:** 以下各项对应的系统能力均为SystemCapability.Notification.CommonEvent 1088 1089| 名称 | 类型 | 可读 | 可写 | 说明 | 1090| ---------- |-------------------- | ---- | ---- | ------------------------------------------------------- | 1091| event | string | 是 | 否 | 表示当前接收的公共事件名称。 | 1092| bundleName | string | 是 | 否 | 表示包名称。 | 1093| code | number | 是 | 否 | 表示公共事件的结果代码,用于传递int类型的数据。 | 1094| data | string | 是 | 否 | 表示公共事件的自定义结果数据,用于传递string类型的数据。 | 1095| parameters | {[key: string]: any} | 是 | 否 | 表示公共事件的附加信息。 | 1096 1097 1098## CommonEventPublishData 1099 1100公共事件发送的数据体,包含公共事件内容和属性。 1101 1102**系统能力:** 以下各项对应的系统能力均为SystemCapability.Notification.CommonEvent 1103 1104| 名称 | 类型 | 可读 | 可写 | 说明 | 1105| --------------------- | -------------------- | ---- | ---- | ---------------------------- | 1106| bundleName | string | 是 | 否 | 表示包名称。 | 1107| code | number | 是 | 否 | 表示公共事件的结果代码。 | 1108| data | string | 是 | 否 | 表示公共事件的自定义结果数据。 | 1109| subscriberPermissions | Array\<string> | 是 | 否 | 表示订阅者的权限。 | 1110| isOrdered | boolean | 是 | 否 | 表示是否是有序事件。 | 1111| isSticky | boolean | 是 | 否 | 表示是否是粘性事件。仅系统应用或系统服务允许发送粘性事件。 | 1112| parameters | {[key: string]: any} | 是 | 否 | 表示公共事件的附加信息。 | 1113 1114## CommonEventSubscribeInfo 1115 1116订阅者信息。 1117 1118**系统能力:** 以下各项对应的系统能力均为SystemCapability.Notification.CommonEvent 1119 1120| 名称 | 类型 | 可读 | 可写 | 说明 | 1121| ------------------- | -------------- | ---- | ---- | ------------------------------------------------------------ | 1122| events | Array\<string> | 是 | 否 | 表示要发送的公共事件。 | 1123| publisherPermission | string | 是 | 否 | 表示发布者的权限。 | 1124| publisherDeviceId | string | 是 | 否 | 表示设备ID,该值必须是同一ohos网络上的现有设备ID。 | 1125| userId | number | 是 | 否 | 表示用户ID。此参数是可选的,默认值当前用户的ID。如果指定了此参数,则该值必须是系统中现有的用户ID。 | 1126| priority | number | 是 | 否 | 表示订阅者的优先级。值的范围是-100到1000。 | 1127