• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.commonEventManager (公共事件模块)
2
3本模块提供了公共事件的能力,包括公共事件的权限列表,发布公共事件,订阅或取消订阅公共事件,获取或修改公共事件结果代码、结果数据等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import CommonEventManager from '@ohos.commonEventManager';
13```
14
15## Support
16
17系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。
18
19全部系统公共事件枚举定义请参见[系统公共事件定义](./commonEventManager-definitions.md)。
20
21## CommonEventManager.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以下错误码详细介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
38
39|错误码ID    |错误信息            |
40|-----------|--------------------|
41|1500004    |not System services or System app|
42|1500007    |message send error|
43|1500008    |CEMS error|
44|1500009    |system error|
45
46**示例:**
47
48```ts
49//发布公共事件回调
50function publishCallBack(err) {
51	if (err) {
52        console.error("publish failed " + JSON.stringify(err));
53    } else {
54        console.info("publish");
55    }
56}
57
58//发布公共事件
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
70发布公共事件指定发布信息(callback形式)。
71
72**系统能力:** SystemCapability.Notification.CommonEvent
73
74**参数:**
75
76| 参数名     | 类型                   | 必填 | 说明                   |
77| -------- | ---------------------- | ---- | ---------------------- |
78| event    | string                 | 是   | 表示要发布的公共事件。  |
79| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
80| callback | syncCallback\<void>   | 是   | 表示被指定的回调方法。  |
81
82**错误码:**
83|错误码ID    |错误信息            |
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**示例:**
92
93
94```ts
95//公共事件相关信息
96var options = {
97	code: 0,			 //公共事件的初始代码
98	data: "initial data",//公共事件的初始数据
99	isOrdered: true	 //有序公共事件
100}
101
102//发布公共事件回调
103function publishCallBack(err) {
104	if (err) {
105        console.error("publish failed " + JSON.stringify(err));
106    } else {
107        console.info("publish");
108    }
109}
110
111//发布公共事件
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
125向指定用户发布公共事件(callback形式)。
126
127**系统能力:** SystemCapability.Notification.CommonEvent
128
129**系统API**:此接口为系统接口,三方应用不支持调用。
130
131**参数:**
132
133| 参数名     | 类型                 | 必填 | 说明                               |
134| -------- | -------------------- | ---- | ---------------------------------- |
135| event    | string               | 是   | 表示要发送的公共事件。             |
136| userId   | number               | 是   | 表示指定向该用户ID发送此公共事件。 |
137| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。             |
138
139**错误码:**
140|错误码ID    |错误信息            |
141|-----------|--------------------|
142|1500004    |not System services or System app|
143|1500007    |message send error|
144|1500008    |CEMS error|
145|1500009    |system error|
146
147**示例:**
148
149```ts
150//发布公共事件回调
151function publishAsUserCallBack(err) {
152	if (err) {
153        console.error("publishAsUser failed " + JSON.stringify(err));
154    } else {
155        console.info("publishAsUser");
156    }
157}
158
159//指定发送的用户
160var userId = 100;
161
162//发布公共事件
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
176向指定用户发布公共事件并指定发布信息(callback形式)。
177
178**系统能力:** SystemCapability.Notification.CommonEvent
179
180**系统API**:此接口为系统接口,三方应用不支持调用。
181
182**参数:**
183
184| 参数名     | 类型                   | 必填 | 说明                   |
185| -------- | ---------------------- | ---- | ---------------------- |
186| event    | string                 | 是   | 表示要发布的公共事件。  |
187| userId   | number | 是 | 表示指定向该用户ID发送此公共事件。 |
188| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
189| callback | AsyncCallback\<void>   | 是   | 表示被指定的回调方法。  |
190
191**错误码:**
192|错误码ID    |错误信息            |
193|-----------|--------------------|
194|1500004    |not System services or System app|
195|1500007    |message send error|
196|1500008    |CEMS error|
197|1500009    |system error|
198
199**示例:**
200
201
202```ts
203//公共事件相关信息
204var options = {
205	code: 0,			 //公共事件的初始代码
206	data: "initial data",//公共事件的初始数据
207}
208
209//发布公共事件回调
210function publishAsUserCallBack(err) {
211	if (err) {
212        console.error("publishAsUser failed " + JSON.stringify(err));
213    } else {
214        console.info("publishAsUser");
215    }
216}
217
218//指定发送的用户
219var userId = 100;
220
221//发布公共事件
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
235创建订阅者(callback形式)。
236
237**系统能力:** SystemCapability.Notification.CommonEvent
238
239**参数:**
240
241| 参数名          | 类型                                                         | 必填 | 说明                       |
242| ------------- | ------------------------------------------------------------ | ---- | -------------------------- |
243| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)        | 是   | 表示订阅信息。             |
244| callback      | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是   | 表示创建订阅者的回调方法。 |
245
246**示例:**
247
248
249```ts
250var subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
251
252//订阅者信息
253var subscribeInfo = {
254    events: ["event"]
255};
256
257//创建订阅者回调
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//创建订阅者
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
281创建订阅者(Promise形式)。
282
283**系统能力:** SystemCapability.Notification.CommonEvent
284
285**参数:**
286
287| 参数名          | 类型                                                  | 必填 | 说明           |
288| ------------- | ----------------------------------------------------- | ---- | -------------- |
289| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是   | 表示订阅信息。 |
290
291**返回值:**
292| 类型                                                      | 说明             |
293| --------------------------------------------------------- | ---------------- |
294| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 返回订阅者对象。 |
295
296**示例:**
297
298```ts
299var subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
300
301//订阅者信息
302var subscribeInfo = {
303	events: ["event"]
304};
305
306//创建订阅者
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
326订阅公共事件(callback形式)。
327
328**系统能力:** SystemCapability.Notification.CommonEvent
329
330**参数:**
331
332| 参数名       | 类型                                                | 必填 | 说明                             |
333| ---------- | ---------------------------------------------------- | ---- | -------------------------------- |
334| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)     | 是   | 表示订阅者对象。                 |
335| callback   | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是   | 表示接收公共事件数据的回调函数。 |
336
337**示例:**
338
339```ts
340//订阅者信息
341var subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
342
343//订阅者信息
344var subscribeInfo = {
345    events: ["event"]
346};
347
348//订阅公共事件回调
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//创建订阅者回调
358function createSubscriberCallBack(err, commonEventSubscriber) {
359    if(!err) {
360        console.info("createSubscriber");
361        subscriber = commonEventSubscriber;
362        //订阅公共事件
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//创建订阅者
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
387取消订阅公共事件(callback形式)。
388
389**系统能力:** SystemCapability.Notification.CommonEvent
390
391**参数:**
392
393| 参数名       | 类型                                             | 必填 | 说明                     |
394| ---------- | ----------------------------------------------- | ---- | ------------------------ |
395| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是   | 表示订阅者对象。         |
396| callback   | AsyncCallback\<void>                            | 否   | 表示取消订阅的回调方法。 |
397
398**示例:**
399
400```ts
401var subscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
402//订阅者信息
403var subscribeInfo = {
404    events: ["event"]
405};
406//订阅公共事件回调
407function subscribeCallBack(err, data) {
408    if (err) {
409        console.info("subscribe failed " + JSON.stringify(err));
410    } else {
411        console.info("subscribe");
412    }
413}
414//创建订阅者回调
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        //订阅公共事件
422        try {
423            CommonEventManager.subscribe(subscriber, subscribeCallBack);
424        } catch(err) {
425            console.info("subscribe failed " + JSON.stringify(err));
426        }
427    }
428}
429//取消订阅公共事件回调
430function unsubscribeCallBack(err) {
431    if (err) {
432        console.info("unsubscribe failed " + JSON.stringify(err));
433    } else {
434        console.info("unsubscribe");
435    }
436}
437//创建订阅者
438try {
439    CommonEventManager.createSubscriber(subscribeInfo, createSubscriberCallBack);
440} catch (err) {
441    console.info("createSubscriber failed " + JSON.stringify(err));
442}
443
444//取消订阅公共事件
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
458获取公共事件的结果代码(callback形式)。
459
460**系统能力**:SystemCapability.Notification.CommonEvent
461
462**参数:**
463
464| 参数名   | 类型                   | 必填 | 说明               |
465| -------- | ---------------------- | ---- | ------------------ |
466| callback | AsyncCallback\<number> | 是   | 公共事件的结果代码。 |
467
468**示例:**
469
470```ts
471var subscriber;	//创建成功的订阅者对象
472
473//获取有序公共事件的结果代码回调
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
488获取公共事件的结果代码(Promise形式)。
489
490**系统能力**:SystemCapability.Notification.CommonEvent
491
492**返回值:**
493
494| 类型             | 说明                 |
495| ---------------- | -------------------- |
496| Promise\<number> | 公共事件的结果代码。 |
497
498**示例:**
499
500```ts
501var subscriber;	//创建成功的订阅者对象
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
514设置公共事件的结果代码(callback形式)。
515
516**系统能力**:SystemCapability.Notification.CommonEvent
517
518**参数:**
519
520| 参数名   | 类型                 | 必填 | 说明                   |
521| -------- | -------------------- | ---- | ---------------------- |
522| code     | number               | 是   | 公共事件的结果代码。   |
523| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。 |
524
525**示例:**
526
527```ts
528var subscriber;	//创建成功的订阅者对象
529
530//设置有序公共事件的结果代码回调
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
545设置公共事件的结果代码(Promise形式)。
546
547**系统能力**:SystemCapability.Notification.CommonEvent
548
549**参数:**
550
551| 参数名 | 类型   | 必填 | 说明               |
552| ------ | ------ | ---- | ------------------ |
553| code   | number | 是   | 公共事件的结果代码。 |
554
555**返回值:**
556
557| 类型             | 说明                 |
558| ---------------- | -------------------- |
559| Promise\<void>   | 返回一个Promise的结果。 |
560
561**示例:**
562
563```ts
564var subscriber;	//创建成功的订阅者对象
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
577获取公共事件的结果数据(callback形式)。
578
579**系统能力**:SystemCapability.Notification.CommonEvent
580
581**参数:**
582
583| 参数名   | 类型                   | 必填 | 说明                 |
584| -------- | ---------------------- | ---- | -------------------- |
585| callback | AsyncCallback\<string> | 是   | 公共事件的结果数据。 |
586
587**示例:**
588
589```ts
590var subscriber;	//创建成功的订阅者对象
591
592//获取有序公共事件的结果数据回调
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
607获取公共事件的结果数据(Promise形式)。
608
609**系统能力**:SystemCapability.Notification.CommonEvent
610
611**返回值:**
612
613| 类型             | 说明               |
614| ---------------- | ------------------ |
615| Promise\<string> | 公共事件的结果数据。 |
616
617**示例:**
618
619```ts
620var subscriber;	//创建成功的订阅者对象
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
633设置公共事件的结果数据(callback形式)。
634
635**系统能力**:SystemCapability.Notification.CommonEvent
636
637**参数:**
638
639| 参数名   | 类型                 | 必填 | 说明                 |
640| -------- | -------------------- | ---- | -------------------- |
641| data     | string               | 是   | 公共事件的结果数据。   |
642| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。 |
643
644**示例:**
645
646```ts
647var subscriber;	//创建成功的订阅者对象
648
649//设置有序公共事件的结果数据回调
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
664设置公共事件的结果数据(Promise形式)。
665
666**系统能力**:SystemCapability.Notification.CommonEvent
667
668**参数:**
669
670| 参数名 | 类型   | 必填 | 说明                 |
671| ------ | ------ | ---- | -------------------- |
672| data   | string | 是   | 公共事件的结果数据。 |
673
674**返回值:**
675
676| 类型             | 说明                 |
677| ---------------- | -------------------- |
678| Promise\<void>   | 返回一个Promise的结果。 |
679
680**示例:**
681
682```ts
683var subscriber;	//创建成功的订阅者对象
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
696设置公共事件的结果代码和结果数据(callback形式)。
697
698**系统能力**:SystemCapability.Notification.CommonEvent
699
700**参数:**
701
702| 参数名   | 类型                 | 必填 | 说明                   |
703| -------- | -------------------- | ---- | ---------------------- |
704| code     | number               | 是   | 公共事件的结果代码。   |
705| data     | string               | 是   | 公共事件的结果数据。   |
706| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。 |
707
708**示例:**
709
710```ts
711var subscriber;	//创建成功的订阅者对象
712
713//设置有序公共事件的结果代码和结果数据回调
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
728设置公共事件的结果代码和结果数据(Promise形式)。
729
730**系统能力**:SystemCapability.Notification.CommonEvent
731
732**参数:**
733
734| 参数名 | 类型   | 必填 | 说明                 |
735| ------ | ------ | ---- | -------------------- |
736| code   | number | 是   | 公共事件的结果代码。 |
737| data   | string | 是   | 公共事件的结果数据。 |
738
739**返回值:**
740
741| 类型             | 说明                 |
742| ---------------- | -------------------- |
743| Promise\<void>   | 返回一个Promise的结果。 |
744
745**示例:**
746
747```ts
748var subscriber;	//创建成功的订阅者对象
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
761查询当前公共事件的是否为有序公共事件(callback形式)。
762
763返回true代表是有序公共事件,false代表不是有序公共事件。
764
765**系统能力**:SystemCapability.Notification.CommonEvent
766
767**参数:**
768
769| 参数名   | 类型                    | 必填 | 说明                               |
770| -------- | ----------------------- | ---- | ---------------------------------- |
771| callback | AsyncCallback\<boolean> | 是   | 当前公共事件的是否为有序公共事件。 |
772
773**示例:**
774
775```ts
776var subscriber;	//创建成功的订阅者对象
777
778//获取当前公共事件是否为有序事件的回调
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
793查询当前公共事件的是否为有序公共事件(Promise形式)。
794
795返回true代表是有序公共事件,false代表不是有序公共事件。
796
797**系统能力**:SystemCapability.Notification.CommonEvent
798
799**返回值:**
800
801| 类型              | 说明                             |
802| ----------------- | -------------------------------- |
803| Promise\<boolean> | 当前公共事件的是否为有序公共事件。 |
804
805**示例:**
806
807```ts
808var subscriber;	//创建成功的订阅者对象
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
821检查当前公共事件是否为一个粘性事件(callback形式)。
822
823返回true代表是粘性公共事件,false代表不是粘性公共事件。
824
825**系统能力**:SystemCapability.Notification.CommonEvent
826
827**参数:**
828
829| 参数名   | 类型                    | 必填 | 说明                               |
830| -------- | ----------------------- | ---- | ---------------------------------- |
831| callback | AsyncCallback\<boolean> | 是   | 当前公共事件的是否为粘性公共事件。 |
832
833**示例:**
834
835```ts
836var subscriber;	//创建成功的订阅者对象
837
838//获取当前公共事件是否为粘性事件的回调
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
853检查当前公共事件是否为一个粘性事件(Promise形式)。
854
855返回true代表是粘性公共事件,false代表不是粘性公共事件。
856
857**系统能力**:SystemCapability.Notification.CommonEvent
858
859**返回值:**
860
861| 类型              | 说明                             |
862| ----------------- | -------------------------------- |
863| Promise\<boolean> | 当前公共事件的是否为粘性公共事件。 |
864
865**示例:**
866
867```ts
868var subscriber;	//创建成功的订阅者对象
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
881取消当前的公共事件,仅对有序公共事件有效,取消后,公共事件不再向下一个订阅者传递(callback形式)。
882
883**系统能力**:SystemCapability.Notification.CommonEvent
884
885**参数:**
886
887| 参数名   | 类型                 | 必填 | 说明                 |
888| -------- | -------------------- | ---- | -------------------- |
889| callback | AsyncCallback\<void> | 是   | 取消当前的公共事件。 |
890
891**示例:**
892
893```ts
894var subscriber;	//创建成功的订阅者对象
895
896//取消当前有序公共事件的回调
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
911取消当前的公共事件,仅对有序公共事件有效,取消后,公共事件不再向下一个订阅者传递(Promise形式)。
912
913**系统能力**:SystemCapability.Notification.CommonEvent
914
915**返回值:**
916
917| 类型             | 说明                 |
918| ---------------- | -------------------- |
919| Promise\<void>   | 返回一个Promise的结果。 |
920
921**示例:**
922
923```ts
924var subscriber;	//创建成功的订阅者对象
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
937清除当前公共事件的取消状态,仅对有序公共事件有效(callback形式)。
938
939**系统能力**:SystemCapability.Notification.CommonEvent
940
941**参数:**
942
943| 参数名   | 类型                 | 必填 | 说明                 |
944| -------- | -------------------- | ---- | -------------------- |
945| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。 |
946
947**示例:**
948
949```ts
950var subscriber;	//创建成功的订阅者对象
951
952//清除当前公共事件取消状态的回调
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
967清除当前公共事件的取消状态,仅对有序公共事件有效(Promise形式)。
968
969**系统能力**:SystemCapability.Notification.CommonEvent
970
971**返回值:**
972
973| 类型             | 说明                 |
974| ---------------- | -------------------- |
975| Promise\<void>   | 返回一个Promise的结果。 |
976
977**示例:**
978
979```ts
980var subscriber;	//创建成功的订阅者对象
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
993获取当前有序公共事件是否取消的状态(callback形式)。
994
995**系统能力**:SystemCapability.Notification.CommonEvent
996
997**参数:**
998
999| 参数名   | 类型                    | 必填 | 说明                               |
1000| -------- | ----------------------- | ---- | ---------------------------------- |
1001| callback | AsyncCallback\<boolean> | 是   | 表示当前有序公共事件是否取消的状态。 |
1002
1003**示例:**
1004
1005```ts
1006var subscriber;	//创建成功的订阅者对象
1007
1008//获取当前有序公共事件是否取消的回调
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
1023获取当前有序公共事件是否取消的状态(Promise形式)。
1024
1025**系统能力**:SystemCapability.Notification.CommonEvent
1026
1027**返回值:**
1028
1029| 类型              | 说明                               |
1030| ----------------- | ---------------------------------- |
1031| Promise\<boolean> | 表示当前有序公共事件是否取消的状态。 |
1032
1033**示例:**
1034
1035```ts
1036var subscriber;	//创建成功的订阅者对象
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
1049获取订阅者的订阅信息(callback形式)。
1050
1051**系统能力**:SystemCapability.Notification.CommonEvent
1052
1053**参数:**
1054
1055| 参数名   | 类型                                                         | 必填 | 说明                   |
1056| -------- | ------------------------------------------------------------ | ---- | ---------------------- |
1057| callback | AsyncCallback\<[CommonEventSubscribeInfo](#commoneventsubscribeinfo)> | 是   | 表示订阅者的订阅信息。 |
1058
1059**示例:**
1060
1061```ts
1062var subscriber;	//创建成功的订阅者对象
1063
1064//获取订阅者信息回调
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
1079获取订阅者的订阅信息(Promise形式)。
1080
1081**系统能力**:SystemCapability.Notification.CommonEvent
1082
1083**返回值:**
1084
1085| 类型                                                         | 说明                   |
1086| ------------------------------------------------------------ | ---------------------- |
1087| Promise\<[CommonEventSubscribeInfo](#commoneventsubscribeinfo)> | 表示订阅者的订阅信息。 |
1088
1089**示例:**
1090
1091```ts
1092var subscriber;	//创建成功的订阅者对象
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
1105结束当前有序公共事件(callback形式)。
1106
1107**系统能力**:SystemCapability.Notification.CommonEvent
1108
1109**参数:**
1110
1111| 参数名   | 类型                  | 必填 | 说明                              |
1112| -------- | -------------------- | ---- | -------------------------------- |
1113| callback | AsyncCallback\<void> | 是   | 表示有序公共事件结束后的回调函数。 |
1114
1115**示例:**
1116
1117```ts
1118var subscriber; //创建成功的订阅者对象
1119
1120//结束当前有序公共事件的回调
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
1135结束当前有序公共事件(Promise形式)。
1136
1137**系统能力**:SystemCapability.Notification.CommonEvent
1138
1139**返回值:**
1140
1141| 类型             | 说明                 |
1142| ---------------- | -------------------- |
1143| Promise\<void>   | 返回一个Promise的结果。 |
1144
1145**示例:**
1146
1147```ts
1148var subscriber;	//创建成功的订阅者对象
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**系统能力:** 以下各项对应的系统能力均为SystemCapability.Notification.CommonEvent
1160
1161| 名称       | 类型                 | 可读 | 可写 | 说明                                                    |
1162| ---------- |-------------------- | ---- | ---- |  ------------------------------------------------------- |
1163| event      | string               | 是  | 否  | 表示当前接收的公共事件名称。                              |
1164| bundleName | string               | 是  | 否  | 表示包名称。                                              |
1165| code       | number               | 是  | 否  | 表示公共事件的结果代码,用于传递int类型的数据。           |
1166| data       | string               | 是  | 否  | 表示公共事件的自定义结果数据,用于传递string类型的数据。 |
1167| parameters | {[key: string]: any} | 是  | 否  | 表示公共事件的附加信息。                                  |
1168
1169
1170## CommonEventPublishData
1171
1172**系统能力:** 以下各项对应的系统能力均为SystemCapability.Notification.CommonEvent
1173
1174| 名称                  | 类型                 | 可读 | 可写 | 说明                         |
1175| --------------------- | -------------------- | ---- | ---- | ---------------------------- |
1176| bundleName            | string               | 是  | 否  | 表示包名称。                   |
1177| code                  | number               | 是  | 否  | 表示公共事件的结果代码。       |
1178| data                  | string               | 是  | 否  | 表示公共事件的自定义结果数据。 |
1179| subscriberPermissions | Array\<string>       | 是  | 否  | 表示订阅者的权限。             |
1180| isOrdered             | boolean              | 是  | 否  | 表示是否是有序事件。           |
1181| isSticky              | boolean              | 是  | 否  | 表示是否是粘性事件。仅系统应用或系统服务允许发送粘性事件。 |
1182| parameters            | {[key: string]: any} | 是  | 否  | 表示公共事件的附加信息。       |
1183
1184## CommonEventSubscribeInfo
1185
1186**系统能力:** 以下各项对应的系统能力均为SystemCapability.Notification.CommonEvent
1187
1188| 名称                | 类型           | 可读 | 可写 | 说明                                                         |
1189| ------------------- | -------------- | ---- | ---- | ------------------------------------------------------------ |
1190| events              | Array\<string> | 是  | 否  | 表示要发送的公共事件。                                         |
1191| publisherPermission | string         | 是  | 否  | 表示发布者的权限。                                             |
1192| publisherDeviceId   | string         | 是  | 否  | 表示设备ID,该值必须是同一ohos网络上的现有设备ID。             |
1193| userId              | number         | 是  | 否  | 表示用户ID。此参数是可选的,默认值当前用户的ID。如果指定了此参数,则该值必须是系统中现有的用户ID。 |
1194| priority            | number         | 是  | 否  | 表示订阅者的优先级。值的范围是-100到1000。                     |
1195