1# CommonEventSubscriber
2
3The **CommonEventSubscriber** module provides APIs for describing the common event subscriber.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## How to Use
10
11Before using the **CommonEventSubscriber** module, you must obtain a **subscriber** object by calling **commonEventManager.createSubscriber**.
12
13```ts
14import { commonEventManager } from '@kit.BasicServicesKit';
15import { BusinessError } from '@kit.BasicServicesKit';
16
17// Define a subscriber to save the created subscriber object for subsequent subscription and unsubscription.
18let subscriber:commonEventManager.CommonEventSubscriber;
19// Subscriber information.
20let subscribeInfo:commonEventManager.CommonEventSubscribeInfo = {
21	events: ["event"]
22};
23// Callback for subscriber creation.
24function createCB(err: BusinessError, commonEventSubscriber:commonEventManager.CommonEventSubscriber) {
25  if (err != null) {
26    console.error(`createSubscriber failed, code is ${err.code}`);
27  } else {
28    console.info("createSubscriber success");
29    subscriber = commonEventSubscriber;
30  }
31}
32// Create a subscriber.
33commonEventManager.createSubscriber(subscribeInfo, createCB);
34```
35
36## getCode
37
38getCode(callback: AsyncCallback\<number>): void
39
40Obtains the result code of an ordered common event. This API uses an asynchronous callback to return the result.
41
42**Atomic service API**: This API can be used in atomic services since API version 11.
43
44**System capability**: SystemCapability.Notification.CommonEvent
45
46**Parameters**
47
48| Name  | Type                  | Mandatory| Description              |
49| -------- | ---------------------- | ---- | ------------------ |
50| callback | AsyncCallback\<number\> | Yes  | Callback used to return the result.|
51
52**Example**
53
54```ts
55// Callback for result code obtaining of an ordered common event.
56function getCodeCallback(err: BusinessError, code:number) {
57  if (err != null) {
58    console.error(`getCode failed, code is ${err.code}, message is ${err.message}`);
59  } else {
60    console.info("getCode " + JSON.stringify(code));
61  }
62}
63subscriber.getCode(getCodeCallback);
64```
65
66## getCode
67
68getCode(): Promise\<number>
69
70Obtains the result code of an ordered common event. This API uses a promise to return the result.
71
72**Atomic service API**: This API can be used in atomic services since API version 11.
73
74**System capability**: SystemCapability.Notification.CommonEvent
75
76**Return value**
77
78| Type            | Description                |
79| ---------------- | -------------------- |
80| Promise\<number> | Promise used to return the result.  |
81
82**Example**
83
84```ts
85subscriber.getCode().then((code:number) => {
86  console.info("getCode " + JSON.stringify(code));
87}).catch((err: BusinessError) => {
88  console.error(`getCode failed, code is ${err.code}, message is ${err.message}`);
89});
90```
91
92## getCodeSync<sup>10+</sup>
93
94getCodeSync(): number
95
96Obtains the result code of an ordered common event.
97
98**Atomic service API**: This API can be used in atomic services since API version 11.
99
100**System capability**: SystemCapability.Notification.CommonEvent
101
102**Return value**
103
104| Type            | Description                |
105| ---------------- | -------------------- |
106| number | Common event code.|
107
108**Example**
109
110```ts
111let code = subscriber.getCodeSync();
112console.info("getCodeSync " + JSON.stringify(code));
113```
114
115## setCode
116
117setCode(code: number, callback: AsyncCallback\<void>): void
118
119Sets the result code of an ordered common event. This API uses an asynchronous callback to return the result.
120
121**Atomic service API**: This API can be used in atomic services since API version 11.
122
123**System capability**: SystemCapability.Notification.CommonEvent
124
125**Parameters**
126
127| Name  | Type                | Mandatory| Description                  |
128| -------- | -------------------- | ---- | ---------------------- |
129| code     | number               | Yes  | Common event code.  |
130| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
131
132**Example**
133
134```ts
135// Callback for result code setting of an ordered common event.
136function setCodeCallback(err: BusinessError) {
137  if (err != null) {
138    console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
139  } else {
140    console.info("setCode success");
141  }
142}
143subscriber.setCode(1, setCodeCallback);
144```
145
146## setCode
147
148setCode(code: number): Promise\<void>
149
150Sets the result code of an ordered common event. This API uses a promise to return the result.
151
152**Atomic service API**: This API can be used in atomic services since API version 11.
153
154**System capability**: SystemCapability.Notification.CommonEvent
155
156**Parameters**
157
158| Name| Type  | Mandatory| Description              |
159| ------ | ------ | ---- | ------------------ |
160| code   | number | Yes  | Common event code.|
161
162**Return value**
163
164| Type            | Description                |
165| ---------------- | -------------------- |
166| Promise\<void>   | Promise that returns no value.|
167
168**Example**
169
170```ts
171subscriber.setCode(1).then(() => {
172  console.info("setCode success");
173}).catch((err: BusinessError) => {
174  console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
175});
176```
177
178## setCodeSync<sup>10+</sup>
179
180setCodeSync(code: number): void
181
182Sets the result code of an ordered common event.
183
184**Atomic service API**: This API can be used in atomic services since API version 11.
185
186**System capability**: SystemCapability.Notification.CommonEvent
187
188**Parameters**
189
190| Name| Type  | Mandatory| Description              |
191| ------ | ------ | ---- | ------------------ |
192| code   | number | Yes  | Common event code.|
193
194**Error codes**
195
196For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
197
198| ID| Error Message                           |
199| -------- | ----------------------------------- |
200| 401      | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed.                    |
201
202**Example**
203
204```ts
205try {
206  subscriber.setCodeSync(1);
207} catch (error) {
208  let err: BusinessError = error as BusinessError;
209  console.error(`setCodeSync failed, code is ${err.code}, message is ${err.message}`);
210}
211```
212
213## getData
214
215getData(callback: AsyncCallback\<string>): void
216
217Obtains the result data of an ordered common event. This API uses an asynchronous callback to return the result.
218
219**Atomic service API**: This API can be used in atomic services since API version 11.
220
221**System capability**: SystemCapability.Notification.CommonEvent
222
223**Parameters**
224
225| Name  | Type                  | Mandatory| Description                |
226| -------- | ---------------------- | ---- | -------------------- |
227| callback | AsyncCallback\<string> | Yes  | Callback used to return the result.|
228
229**Example**
230
231```ts
232// Callback for result data obtaining of an ordered common event.
233function getDataCallback(err: BusinessError, data:string) {
234  if (err != null) {
235    console.error(`getData failed, code is ${err.code}, message is ${err.message}`);
236  } else {
237    console.info("getData " + JSON.stringify(data));
238  }
239}
240subscriber.getData(getDataCallback);
241```
242
243## getData
244
245getData(): Promise\<string>
246
247Obtains the result data of an ordered common event. This API uses a promise to return the result.
248
249**Atomic service API**: This API can be used in atomic services since API version 11.
250
251**System capability**: SystemCapability.Notification.CommonEvent
252
253**Return value**
254
255| Type            | Description              |
256| ---------------- | ------------------ |
257| Promise\<string> | Promise used to return the result.  |
258
259**Example**
260
261```ts
262subscriber.getData().then((data:string) => {
263  console.info("getData " + JSON.stringify(data));
264}).catch((err: BusinessError) => {
265  console.error(`getData failed, code is ${err.code}, message is ${err.message}`);
266});
267```
268
269## getDataSync<sup>10+</sup>
270
271getDataSync(): string
272
273Obtains the result data of an ordered common event.
274
275**Atomic service API**: This API can be used in atomic services since API version 11.
276
277**System capability**: SystemCapability.Notification.CommonEvent
278
279**Return value**
280
281| Type            | Description              |
282| ---------------- | ------------------ |
283| string | Common event data.|
284
285**Example**
286
287```ts
288let data = subscriber.getDataSync();
289console.info("getDataSync " + JSON.stringify(data));
290```
291
292## setData
293
294setData(data: string, callback: AsyncCallback\<void>): void
295
296Sets the result data for an ordered common event. This API uses an asynchronous callback to return the result.
297
298**Atomic service API**: This API can be used in atomic services since API version 11.
299
300**System capability**: SystemCapability.Notification.CommonEvent
301
302**Parameters**
303
304| Name  | Type                | Mandatory| Description                |
305| -------- | -------------------- | ---- | -------------------- |
306| data     | string               | Yes  | Common event data.  |
307| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
308
309**Example**
310
311```ts
312// Callback for result data setting of an ordered common event
313function setDataCallback(err: BusinessError) {
314  if (err != null) {
315    console.error(`setData failed, code is ${err.code}, message is ${err.message}`);
316  } else {
317    console.info("setData success");
318  }
319}
320subscriber.setData("publish_data_changed", setDataCallback);
321```
322
323## setData
324
325setData(data: string): Promise\<void>
326
327Sets the result data for an ordered common event. This API uses a promise to return the result.
328
329**Atomic service API**: This API can be used in atomic services since API version 11.
330
331**System capability**: SystemCapability.Notification.CommonEvent
332
333**Parameters**
334
335| Name| Type  | Mandatory| Description                |
336| ------ | ------ | ---- | -------------------- |
337| data   | string | Yes  | Common event data.|
338
339**Return value**
340
341| Type            | Description                |
342| ---------------- | -------------------- |
343| Promise\<void>   | Promise that returns no value.|
344
345**Example**
346
347```ts
348subscriber.setData("publish_data_changed").then(() => {
349  console.info("setData success");
350}).catch((err: BusinessError) => {
351  console.error(`setData failed, code is ${err.code}, message is ${err.message}`);
352});
353```
354
355## setDataSync<sup>10+</sup>
356
357setDataSync(data: string): void
358
359Sets the result data for an ordered common event.
360
361**Atomic service API**: This API can be used in atomic services since API version 11.
362
363**System capability**: SystemCapability.Notification.CommonEvent
364
365**Parameters**
366
367| Name| Type  | Mandatory| Description                |
368| ------ | ------ | ---- | -------------------- |
369| data   | string | Yes  | Common event data.|
370
371**Error codes**
372
373For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
374
375| ID| Error Message                           |
376| -------- | ----------------------------------- |
377| 401      | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed.                    |
378
379**Example**
380
381```ts
382try {
383  subscriber.setDataSync("publish_data_changed");
384} catch (error) {
385  let err: BusinessError = error as BusinessError;
386  console.error(`setDataSync failed, code is ${err.code}, message is ${err.message}`);
387}
388```
389
390## setCodeAndData
391
392setCodeAndData(code: number, data: string, callback:AsyncCallback\<void>): void
393
394Sets the result code and data of an ordered common event. This API uses an asynchronous callback to return the result.
395
396**Atomic service API**: This API can be used in atomic services since API version 11.
397
398**System capability**: SystemCapability.Notification.CommonEvent
399
400**Parameters**
401
402| Name  | Type                | Mandatory| Description                  |
403| -------- | -------------------- | ---- | ---------------------- |
404| code     | number               | Yes  | Common event code.  |
405| data     | string               | Yes  | Common event data.  |
406| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
407
408**Example**
409
410```ts
411// Callback for code and data setting of an ordered common event.
412function setCodeAndDataCallback(err: BusinessError) {
413  if (err != null) {
414    console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`);
415  } else {
416    console.info("setCodeAndData success");
417  }
418}
419subscriber.setCodeAndData(1, "publish_data_changed", setCodeAndDataCallback);
420```
421
422## setCodeAndData
423
424setCodeAndData(code: number, data: string): Promise\<void>
425
426Sets the result code and data of an ordered common event. This API uses a promise to return the result.
427
428**Atomic service API**: This API can be used in atomic services since API version 11.
429
430**System capability**: SystemCapability.Notification.CommonEvent
431
432**Parameters**
433
434| Name| Type  | Mandatory| Description                |
435| ------ | ------ | ---- | -------------------- |
436| code   | number | Yes  | Common event code.|
437| data   | string | Yes  | Common event data.|
438
439**Return value**
440
441| Type            | Description                |
442| ---------------- | -------------------- |
443| Promise\<void>   | Promise that returns no value.|
444
445**Example**
446
447```ts
448subscriber.setCodeAndData(1, "publish_data_changed").then(() => {
449  console.info("setCodeAndData success");
450}).catch((err: BusinessError) => {
451  console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`);
452});
453```
454
455## setCodeAndDataSync<sup>10+</sup>
456
457setCodeAndDataSync(code: number, data: string): void
458
459Sets the result code and data of an ordered common event.
460
461**Atomic service API**: This API can be used in atomic services since API version 11.
462
463**System capability**: SystemCapability.Notification.CommonEvent
464
465**Parameters**
466
467| Name| Type  | Mandatory| Description                |
468| ------ | ------ | ---- | -------------------- |
469| code   | number | Yes  | Common event code.|
470| data   | string | Yes  | Common event data.|
471
472**Error codes**
473
474For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
475
476| ID| Error Message                           |
477| -------- | ----------------------------------- |
478| 401      | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed.                    |
479
480**Example**
481
482```ts
483try {
484  subscriber.setCodeAndDataSync(1, "publish_data_changed");
485} catch (error) {
486  let err: BusinessError = error as BusinessError;
487  console.error(`setCodeAndDataSync failed, code is ${err.code}, message is ${err.message}`);
488}
489
490```
491
492## isOrderedCommonEvent
493
494isOrderedCommonEvent(callback: AsyncCallback\<boolean>): void
495
496Checks whether the current common event is an ordered common event. This API uses an asynchronous callback to return the result.
497
498**System capability**: SystemCapability.Notification.CommonEvent
499
500**Parameters**
501
502| Name  | Type                   | Mandatory| Description                              |
503| -------- | ----------------------- | ---- | ---------------------------------- |
504| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. Returns **true** if the common event is an ordered one; returns **false** otherwise.|
505
506**Example**
507
508```ts
509// Callback for checking whether the current common event is an ordered one.
510function isOrderedCommonEventCallback(err: BusinessError, isOrdered:boolean) {
511  if (err != null) {
512    console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`);
513  } else {
514    console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered));
515  }
516}
517subscriber.isOrderedCommonEvent(isOrderedCommonEventCallback);
518```
519
520## isOrderedCommonEvent
521
522isOrderedCommonEvent(): Promise\<boolean>
523
524Checks whether the current common event is an ordered common event. This API uses a promise to return the result.
525
526**System capability**: SystemCapability.Notification.CommonEvent
527
528**Return value**
529
530| Type             | Description                            |
531| ----------------- | -------------------------------- |
532| Promise\<boolean> | Promise used to return the result. Returns **true** if the common event is an ordered one; returns **false** otherwise.|
533
534**Example**
535
536```ts
537subscriber.isOrderedCommonEvent().then((isOrdered:boolean) => {
538  console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered));
539}).catch((err: BusinessError) => {
540  console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`);
541});
542```
543
544## isOrderedCommonEventSync<sup>10+</sup>
545
546isOrderedCommonEventSync(): boolean
547
548Checks whether the current common event is an ordered common event.
549
550**System capability**: SystemCapability.Notification.CommonEvent
551
552**Return value**
553
554| Type             | Description                            |
555| ----------------- | -------------------------------- |
556| boolean | Returns **true** if the common event is an ordered one; returns **false** otherwise.|
557
558**Example**
559
560```ts
561let isOrdered  = subscriber.isOrderedCommonEventSync();
562console.info("isOrderedCommonEventSync " + JSON.stringify(isOrdered));
563```
564
565## isStickyCommonEvent
566
567isStickyCommonEvent(callback: AsyncCallback\<boolean>): void
568
569Checks whether a common event is a sticky one. This API uses an asynchronous callback to return the result.
570
571**System capability**: SystemCapability.Notification.CommonEvent
572
573**Parameters**
574
575| Name  | Type                   | Mandatory| Description                              |
576| -------- | ----------------------- | ---- | ---------------------------------- |
577| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. Returns **true** if the common event is a sticky one; returns **false** otherwise. |
578
579**Example**
580
581```ts
582// Callback for checking whether the current common event is a sticky one.
583function isStickyCommonEventCallback(err: BusinessError, isSticky:boolean) {
584  if (err != null) {
585    console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`);
586  } else {
587    console.info("isStickyCommonEvent " + JSON.stringify(isSticky));
588  }
589}
590subscriber.isStickyCommonEvent(isStickyCommonEventCallback);
591```
592
593## isStickyCommonEvent
594
595isStickyCommonEvent(): Promise\<boolean>
596
597Checks whether a common event is a sticky one. This API uses a promise to return the result.
598
599**System capability**: SystemCapability.Notification.CommonEvent
600
601**Return value**
602
603| Type             | Description                            |
604| ----------------- | -------------------------------- |
605| Promise\<boolean> | Promise used to return the result. Returns **true** if the common event is a sticky one; returns **false** otherwise. |
606
607**Example**
608
609```ts
610subscriber.isStickyCommonEvent().then((isSticky:boolean) => {
611  console.info("isStickyCommonEvent " + JSON.stringify(isSticky));
612}).catch((err: BusinessError) => {
613  console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`);
614});
615```
616
617## isStickyCommonEventSync<sup>10+</sup>
618
619isStickyCommonEventSync(): boolean
620
621Checks whether a common event is a sticky one.
622
623**System capability**: SystemCapability.Notification.CommonEvent
624
625**Return value**
626
627| Type             | Description                            |
628| ----------------- | -------------------------------- |
629| boolean | Returns **true** if the common event is a sticky one; returns **false** otherwise. |
630
631**Example**
632
633```ts
634let isSticky  = subscriber.isStickyCommonEventSync();
635console.info("isStickyCommonEventSync " + JSON.stringify(isSticky));
636```
637
638## abortCommonEvent
639
640abortCommonEvent(callback: AsyncCallback\<void>): void
641
642Aborts an ordered common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. This API uses an asynchronous callback to return the result.
643
644**System capability**: SystemCapability.Notification.CommonEvent
645
646**Parameters**
647
648| Name  | Type                | Mandatory| Description                |
649| -------- | -------------------- | ---- | -------------------- |
650| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
651
652**Example**
653
654```ts
655// Callback for ordered common event aborting.
656function abortCommonEventCallback(err: BusinessError) {
657  if (err != null) {
658	console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
659  } else {
660    console.info("abortCommonEvent success");
661  }
662}
663function finishCommonEventCallback(err: BusinessError) {
664  if (err != null) {
665    console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
666  } else {
667    console.info("finishCommonEvent success");
668  }
669}
670subscriber.abortCommonEvent(abortCommonEventCallback);
671subscriber.finishCommonEvent(finishCommonEventCallback);
672```
673
674## abortCommonEvent
675
676abortCommonEvent(): Promise\<void>
677
678Aborts an ordered common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. This API uses a promise to return the result.
679
680**System capability**: SystemCapability.Notification.CommonEvent
681
682**Return value**
683
684| Type            | Description                |
685| ---------------- | -------------------- |
686| Promise\<void>   | Promise that returns no value.|
687
688**Example**
689
690```ts
691subscriber.abortCommonEvent().then(() => {
692  console.info("abortCommonEvent success");
693}).catch((err: BusinessError) => {
694  console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
695});
696subscriber.finishCommonEvent().then(() => {
697  console.info("finishCommonEvent success");
698}).catch((err: BusinessError) => {
699  console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
700});
701```
702
703## abortCommonEventSync<sup>10+</sup>
704
705abortCommonEventSync(): void
706
707Aborts an ordered common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber.
708
709**System capability**: SystemCapability.Notification.CommonEvent
710
711**Example**
712
713```ts
714subscriber.abortCommonEventSync();
715subscriber.finishCommonEvent().then(() => {
716  console.info("finishCommonEvent success");
717}).catch((err: BusinessError) => {
718  console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
719});
720```
721
722## clearAbortCommonEvent
723
724clearAbortCommonEvent(callback: AsyncCallback\<void>): void
725
726Clears the aborted state of an ordered common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. This API uses an asynchronous callback to return the result.
727
728**System capability**: SystemCapability.Notification.CommonEvent
729
730**Parameters**
731
732| Name  | Type                | Mandatory| Description                |
733| -------- | -------------------- | ---- | -------------------- |
734| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
735
736**Example**
737
738```ts
739// Callback for clearing the aborted state of the current common event.
740function clearAbortCommonEventCallback(err: BusinessError) {
741  if (err != null) {
742    console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
743  } else {
744    console.info("clearAbortCommonEvent success");
745  }
746}
747function finishCommonEventCallback(err: BusinessError) {
748  if (err != null) {
749    console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
750  } else {
751    console.info("finishCommonEvent success");
752  }
753}
754subscriber.clearAbortCommonEvent(clearAbortCommonEventCallback);
755subscriber.finishCommonEvent(finishCommonEventCallback);
756```
757
758## clearAbortCommonEvent
759
760clearAbortCommonEvent(): Promise\<void>
761
762Clears the aborted state of an ordered common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. This API uses a promise to return the result.
763
764**System capability**: SystemCapability.Notification.CommonEvent
765
766**Return value**
767
768| Type            | Description                |
769| ---------------- | -------------------- |
770| Promise\<void>   | Promise that returns no value.|
771
772**Example**
773
774```ts
775subscriber.clearAbortCommonEvent().then(() => {
776  console.info("clearAbortCommonEvent success");
777}).catch((err: BusinessError) => {
778  console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
779});
780subscriber.finishCommonEvent().then(() => {
781  console.info("finishCommonEvent success");
782}).catch((err: BusinessError) => {
783  console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
784});
785```
786
787## clearAbortCommonEventSync<sup>10+</sup>
788
789clearAbortCommonEventSync(): void
790
791Clears the aborted state of an ordered common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber.
792
793**System capability**: SystemCapability.Notification.CommonEvent
794
795**Example**
796
797```ts
798subscriber.clearAbortCommonEventSync();
799subscriber.finishCommonEvent().then(() => {
800  console.info("finishCommonEvent success");
801}).catch((err: BusinessError) => {
802  console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
803});
804```
805
806## getAbortCommonEvent
807
808getAbortCommonEvent(callback: AsyncCallback\<boolean>): void
809
810Checks whether this ordered common event should be aborted. This API uses an asynchronous callback to return the result.
811
812**System capability**: SystemCapability.Notification.CommonEvent
813
814**Parameters**
815
816| Name  | Type                   | Mandatory| Description                              |
817| -------- | ----------------------- | ---- | ---------------------------------- |
818| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise.|
819
820**Example**
821
822```ts
823// Callback for checking whether the ordered common event is in the aborted state.
824function getAbortCommonEventCallback(err: BusinessError, abortEvent:boolean) {
825  if (err != null) {
826    console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
827  } else {
828    console.info("getAbortCommonEvent " + JSON.stringify(abortEvent));
829  }
830}
831subscriber.getAbortCommonEvent(getAbortCommonEventCallback);
832```
833
834## getAbortCommonEvent
835
836getAbortCommonEvent(): Promise\<boolean>
837
838Checks whether this ordered common event should be aborted. This API uses a promise to return the result.
839
840**System capability**: SystemCapability.Notification.CommonEvent
841
842**Return value**
843
844| Type             | Description                              |
845| ----------------- | ---------------------------------- |
846| Promise\<boolean> | Promise used to return the result. Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise.|
847
848**Example**
849
850```ts
851subscriber.getAbortCommonEvent().then((abortEvent:boolean) => {
852  console.info("getAbortCommonEvent " + JSON.stringify(abortEvent));
853}).catch((err: BusinessError) => {
854  console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
855});
856```
857
858## getAbortCommonEventSync<sup>10+</sup>
859
860getAbortCommonEventSync(): boolean
861
862Checks whether this ordered common event should be aborted.
863
864**System capability**: SystemCapability.Notification.CommonEvent
865
866**Return value**
867
868| Type             | Description                              |
869| ----------------- | ---------------------------------- |
870| boolean | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise.|
871
872**Example**
873
874```ts
875let abortEvent = subscriber.getAbortCommonEventSync();
876console.info("getAbortCommonEventSync " + JSON.stringify(abortEvent));
877```
878
879## getSubscribeInfo
880
881getSubscribeInfo(callback: AsyncCallback\<CommonEventSubscribeInfo>): void
882
883Obtains the subscriber information. This API uses an asynchronous callback to return the result.
884
885**Atomic service API**: This API can be used in atomic services since API version 11.
886
887**System capability**: SystemCapability.Notification.CommonEvent
888
889**Parameters**
890
891| Name  | Type                                                        | Mandatory| Description                  |
892| -------- | ------------------------------------------------------------ | ---- | ---------------------- |
893| callback | AsyncCallback\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Yes  | Callback used to return the result.|
894
895**Example**
896
897```ts
898// Callback for subscriber information obtaining.
899function getSubscribeInfoCallback(err: BusinessError, subscribeInfo:commonEventManager.CommonEventSubscribeInfo) {
900  if (err != null) {
901    console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
902  } else {
903    console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo));
904  }
905}
906subscriber.getSubscribeInfo(getSubscribeInfoCallback);
907```
908
909## getSubscribeInfo
910
911getSubscribeInfo(): Promise\<CommonEventSubscribeInfo>
912
913Obtains the subscriber information. This API uses a promise to return the result.
914
915**Atomic service API**: This API can be used in atomic services since API version 11.
916
917**System capability**: SystemCapability.Notification.CommonEvent
918
919**Return value**
920
921| Type                                                        | Description                  |
922| ------------------------------------------------------------ | ---------------------- |
923| Promise\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Promise used to return the result.|
924
925**Example**
926
927```ts
928subscriber.getSubscribeInfo().then((subscribeInfo:commonEventManager.CommonEventSubscribeInfo) => {
929  console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo));
930}).catch((err: BusinessError) => {
931  console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
932});
933```
934
935## getSubscribeInfoSync<sup>10+</sup>
936
937getSubscribeInfoSync(): CommonEventSubscribeInfo
938
939Obtains the subscriber information.
940
941**Atomic service API**: This API can be used in atomic services since API version 11.
942
943**System capability**: SystemCapability.Notification.CommonEvent
944
945**Return value**
946
947| Type                                                        | Description                  |
948| ------------------------------------------------------------ | ---------------------- |
949| [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Subscriber information.|
950
951**Example**
952
953```ts
954let subscribeInfo = subscriber.getSubscribeInfoSync();
955console.info("getSubscribeInfoSync " + JSON.stringify(subscribeInfo));
956```
957
958## finishCommonEvent<sup>9+</sup>
959
960finishCommonEvent(callback: AsyncCallback\<void>): void
961
962Finishes this ordered common event. This API uses an asynchronous callback to return the result.
963
964**System capability**: SystemCapability.Notification.CommonEvent
965
966**Parameters**
967
968| Name  | Type                 | Mandatory| Description                             |
969| -------- | -------------------- | ---- | -------------------------------- |
970| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
971
972**Example**
973
974```ts
975// Callback for ordered common event finishing.
976function finishCommonEventCallback(err: BusinessError) {
977  if (err != null) {
978    console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
979  } else {
980    console.info("finishCommonEvent success");
981  }
982}
983subscriber.finishCommonEvent(finishCommonEventCallback);
984```
985
986## finishCommonEvent<sup>9+</sup>
987
988finishCommonEvent(): Promise\<void>
989
990Finishes this ordered common event. This API uses a promise to return the result.
991
992**System capability**: SystemCapability.Notification.CommonEvent
993
994**Return value**
995
996| Type            | Description                |
997| ---------------- | -------------------- |
998| Promise\<void>   | Promise that returns no value.|
999
1000**Example**
1001
1002```ts
1003subscriber.finishCommonEvent().then(() => {
1004  console.info("finishCommonEvent success");
1005}).catch((err: BusinessError) => {
1006  console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
1007});
1008```
1009