• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# CommonEventSubscriber
2
3## getCode
4
5```ts
6getCode(callback: AsyncCallback<number>): void
7```
8
9Obtains the code of this common event. This API uses an asynchronous callback to return the result.
10
11**System capability**: SystemCapability.Notification.CommonEvent
12
13**Parameters**
14
15| Name  | Type                  | Mandatory| Description              |
16| -------- | ---------------------- | ---- | ------------------ |
17| callback | AsyncCallback\<number\> | Yes  | Common event code.|
18
19**Example**
20
21```ts
22let subscriber;	// Subscriber object successfully created.
23
24// Callback for result code obtaining of an ordered common event.
25function getCodeCB(err, code) {
26    if (err.code) {
27        console.error(`getCode failed, code is ${err.code}, message is ${err.message}`);
28    } else {
29        console.info("getCode " + JSON.stringify(code));
30    }
31}
32subscriber.getCode(getCodeCB);
33```
34
35## getCode
36
37```ts
38getCode(): Promise<number>
39```
40
41Obtains the code of this common event. This API uses a promise to return the result.
42
43**System capability**: SystemCapability.Notification.CommonEvent
44
45**Return value**
46
47| Type            | Description                |
48| ---------------- | -------------------- |
49| Promise\<number> | Common event code.|
50
51**Example**
52
53```ts
54let subscriber;	// Subscriber object successfully created.
55
56subscriber.getCode().then((code) => {
57    console.info("getCode " + JSON.stringify(code));
58}).catch((err) => {
59    console.error(`getCode failed, code is ${err.code}, message is ${err.message}`);
60});
61```
62
63## setCode
64
65```ts
66setCode(code: number, callback: AsyncCallback<void>): void
67```
68
69Sets the code for this common event. This API uses an asynchronous callback to return the result.
70
71**System capability**: SystemCapability.Notification.CommonEvent
72
73**Parameters**
74
75| Name  | Type                | Mandatory| Description                  |
76| -------- | -------------------- | ---- | ---------------------- |
77| code     | number               | Yes  | Common event code.  |
78| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
79
80**Example**
81
82```ts
83let subscriber;	// Subscriber object successfully created.
84
85// Callback for result code setting of an ordered common event.
86function setCodeCB(err) {
87    if (err.code) {
88        console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
89    } else {
90        console.info("setCode");
91    }
92}
93subscriber.setCode(1, setCodeCB);
94```
95
96## setCode
97
98```ts
99setCode(code: number): Promise<void>
100```
101
102Sets the code for this common event. This API uses a promise to return the result.
103
104**System capability**: SystemCapability.Notification.CommonEvent
105
106**Parameters**
107
108| Name| Type  | Mandatory| Description              |
109| ------ | ------ | ---- | ------------------ |
110| code   | number | Yes  | Common event code.|
111
112**Return value**
113
114| Type            | Description                |
115| ---------------- | -------------------- |
116| Promise\<void>   | Promise used to return the result.|
117
118**Example**
119
120```ts
121let subscriber;	// Subscriber object successfully created.
122
123subscriber.setCode(1).then(() => {
124    console.info("setCode");
125}).catch((err) => {
126    console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
127});
128```
129
130## getData
131
132```ts
133getData(callback: AsyncCallback<string>): void
134```
135
136Obtains the data of this common event. This API uses an asynchronous callback to return the result.
137
138**System capability**: SystemCapability.Notification.CommonEvent
139
140**Parameters**
141
142| Name  | Type                  | Mandatory| Description                |
143| -------- | ---------------------- | ---- | -------------------- |
144| callback | AsyncCallback\<string> | Yes  | Common event data.|
145
146**Example**
147
148```ts
149let subscriber;	// Subscriber object successfully created.
150
151// Callback for result data obtaining of an ordered common event.
152function getDataCB(err, data) {
153    if (err.code) {
154        console.error(`getData failed, code is ${err.code}, message is ${err.message}`);
155    } else {
156        console.info("getData " + JSON.stringify(data));
157    }
158}
159subscriber.getData(getDataCB);
160```
161
162## getData
163
164```ts
165getData(): Promise<string>
166```
167
168Obtains the data of this common event. This API uses a promise to return the result.
169
170**System capability**: SystemCapability.Notification.CommonEvent
171
172**Return value**
173
174| Type            | Description              |
175| ---------------- | ------------------ |
176| Promise\<string> | Common event data.|
177
178**Example**
179
180```ts
181let subscriber;	// Subscriber object successfully created.
182
183subscriber.getData().then((data) => {
184    console.info("getData " + JSON.stringify(data));
185}).catch((err) => {
186    console.error(`getData failed, code is ${err.code}, message is ${err.message}`);
187});
188```
189
190## setData
191
192setData(data: string, callback: AsyncCallback\<void>): void
193
194Sets the data for this common event. This API uses an asynchronous callback to return the result.
195
196**System capability**: SystemCapability.Notification.CommonEvent
197
198**Parameters**
199
200| Name  | Type                | Mandatory| Description                |
201| -------- | -------------------- | ---- | -------------------- |
202| data     | string               | Yes  | Common event data.  |
203| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
204
205**Example**
206
207```ts
208let subscriber;	// Subscriber object successfully created.
209
210// Callback for result data setting of an ordered common event
211function setDataCB(err) {
212    if (err.code) {
213        console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
214    } else {
215        console.info("setData");
216    }
217}
218subscriber.setData("publish_data_changed", setDataCB);
219```
220
221## setData
222
223```ts
224setData(data: string): Promise<void>
225```
226
227Sets the data for this common event. This API uses a promise to return the result.
228
229**System capability**: SystemCapability.Notification.CommonEvent
230
231**Parameters**
232
233| Name| Type  | Mandatory| Description                |
234| ------ | ------ | ---- | -------------------- |
235| data   | string | Yes  | Common event data.|
236
237**Return value**
238
239| Type            | Description                |
240| ---------------- | -------------------- |
241| Promise\<void>   | Promise used to return the result.|
242
243**Example**
244
245```ts
246let subscriber;	// Subscriber object successfully created.
247
248subscriber.setData("publish_data_changed").then(() => {
249    console.info("setData");
250}).catch((err) => {
251    console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
252});
253```
254
255## setCodeAndData
256
257```ts
258setCodeAndData(code: number, data: string, callback:AsyncCallback<void>): void
259```
260
261Sets the code and data for this common event. This API uses an asynchronous callback to return the result.
262
263**System capability**: SystemCapability.Notification.CommonEvent
264
265**Parameters**
266
267| Name  | Type                | Mandatory| Description                  |
268| -------- | -------------------- | ---- | ---------------------- |
269| code     | number               | Yes  | Common event code.  |
270| data     | string               | Yes  | Common event data.  |
271| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
272
273**Example**
274
275```ts
276let subscriber;	// Subscriber object successfully created.
277
278// Callback for code and data setting of an ordered common event.
279function setCodeDataCB(err) {
280    if (err.code) {
281        console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`);
282    } else {
283        console.info("setCodeDataCallback");
284    }
285}
286subscriber.setCodeAndData(1, "publish_data_changed", setCodeDataCB);
287```
288
289## setCodeAndData
290
291```ts
292setCodeAndData(code: number, data: string): Promise<void>
293```
294
295Sets the code and data for this common event. This API uses a promise to return the result.
296
297**System capability**: SystemCapability.Notification.CommonEvent
298
299**Parameters**
300
301| Name| Type  | Mandatory| Description                |
302| ------ | ------ | ---- | -------------------- |
303| code   | number | Yes  | Common event code.|
304| data   | string | Yes  | Common event data.|
305
306**Return value**
307
308| Type            | Description                |
309| ---------------- | -------------------- |
310| Promise\<void>   | Promise used to return the result.|
311
312**Example**
313
314```ts
315let subscriber;	// Subscriber object successfully created.
316
317subscriber.setCodeAndData(1, "publish_data_changed").then(() => {
318    console.info("setCodeAndData");
319}).catch((err) => {
320    console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`);
321});
322```
323
324## isOrderedCommonEvent
325
326```ts
327isOrderedCommonEvent(callback: AsyncCallback<boolean>): void
328```
329
330Checks whether this common event is an ordered one. This API uses an asynchronous callback to return the result.
331
332**System capability**: SystemCapability.Notification.CommonEvent
333
334**Parameters**
335
336| Name  | Type                   | Mandatory| Description                              |
337| -------- | ----------------------- | ---- | ---------------------------------- |
338| callback | AsyncCallback\<boolean> | Yes  | Returns **true** if the common event is an ordered one; returns **false** otherwise.|
339
340**Example**
341
342```ts
343let subscriber;	// Subscriber object successfully created.
344
345// Callback for checking whether the current common event is an ordered one.
346function isOrderedCB(err, isOrdered) {
347    if (err.code) {
348        console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`);
349    } else {
350        console.info("isOrdered " + JSON.stringify(isOrdered));
351    }
352}
353subscriber.isOrderedCommonEvent(isOrderedCB);
354```
355
356## isOrderedCommonEvent
357
358```ts
359isOrderedCommonEvent(): Promise<boolean>
360```
361
362Checks whether this common event is an ordered one. This API uses a promise to return the result.
363
364**System capability**: SystemCapability.Notification.CommonEvent
365
366**Return value**
367
368| Type             | Description                            |
369| ----------------- | -------------------------------- |
370| Promise\<boolean> | Returns **true** if the common event is an ordered one; returns **false** otherwise.|
371
372**Example**
373
374```ts
375let subscriber;	// Subscriber object successfully created.
376
377subscriber.isOrderedCommonEvent().then((isOrdered) => {
378    console.info("isOrdered " + JSON.stringify(isOrdered));
379}).catch((err) => {
380    console.error(`isOrdered failed, code is ${err.code}, message is ${err.message}`);
381});
382```
383
384## isStickyCommonEvent
385
386```ts
387isStickyCommonEvent(callback: AsyncCallback<boolean>): void
388```
389
390Checks whether this common event is a sticky one. This API uses an asynchronous callback to return the result.
391
392**System capability**: SystemCapability.Notification.CommonEvent
393
394**Parameters**
395
396| Name  | Type                   | Mandatory| Description                              |
397| -------- | ----------------------- | ---- | ---------------------------------- |
398| callback | AsyncCallback\<boolean> | Yes  | Returns **true** if the common event is a sticky one; returns **false** otherwise.|
399
400**Example**
401
402```ts
403let subscriber;	// Subscriber object successfully created.
404
405// Callback for checking whether the current common event is a sticky one.
406function isStickyCB(err, isSticky) {
407    if (err.code) {
408        console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`);
409    } else {
410        console.info("isSticky " + JSON.stringify(isSticky));
411    }
412}
413subscriber.isStickyCommonEvent(isStickyCB);
414```
415
416## isStickyCommonEvent
417
418```ts
419isStickyCommonEvent(): Promise<boolean>
420```
421
422Checks whether this common event is a sticky one. This API uses a promise to return the result.
423
424**System capability**: SystemCapability.Notification.CommonEvent
425
426**Return value**
427
428| Type             | Description                            |
429| ----------------- | -------------------------------- |
430| Promise\<boolean> | Returns **true** if the common event is a sticky one; returns **false** otherwise.|
431
432**Example**
433
434```ts
435let subscriber;	// Subscriber object successfully created.
436
437subscriber.isStickyCommonEvent().then((isSticky) => {
438    console.info("isSticky " + JSON.stringify(isSticky));
439}).catch((err) => {
440    console.error(`isSticky failed, code is ${err.code}, message is ${err.message}`);
441});
442```
443
444## abortCommonEvent
445
446```ts
447abortCommonEvent(callback: AsyncCallback<void>): void
448```
449
450Aborts this common event. After the abort, the common event is not sent to the next subscriber. This API takes effect only for ordered common events. It uses an asynchronous callback to return the result.
451
452**System capability**: SystemCapability.Notification.CommonEvent
453
454**Parameters**
455
456| Name  | Type                | Mandatory| Description                |
457| -------- | -------------------- | ---- | -------------------- |
458| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
459
460**Example**
461
462```ts
463let subscriber;	// Subscriber object successfully created.
464
465// Callback for common event aborting.
466function abortCB(err) {
467    if (err.code) {
468		console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
469    } else {
470        console.info("abortCommonEvent");
471    }
472}
473subscriber.abortCommonEvent(abortCB);
474```
475
476## abortCommonEvent
477
478```ts
479abortCommonEvent(): Promise<void>
480```
481
482Aborts this common event. After the abort, the common event is not sent to the next subscriber. This API takes effect only for ordered common events. It uses a promise to return the result.
483
484**System capability**: SystemCapability.Notification.CommonEvent
485
486**Return value**
487
488| Type            | Description                |
489| ---------------- | -------------------- |
490| Promise\<void>   | Promise used to return the result.|
491
492**Example**
493
494```ts
495let subscriber;	// Subscriber object successfully created.
496
497subscriber.abortCommonEvent().then(() => {
498    console.info("abortCommonEvent");
499}).catch((err) => {
500    console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
501});
502```
503
504## clearAbortCommonEvent
505
506```ts
507clearAbortCommonEvent(callback: AsyncCallback<void>): void
508```
509
510Clears the aborted state of this common event. This API takes effect only for ordered common events. It uses an asynchronous callback to return the result.
511
512**System capability**: SystemCapability.Notification.CommonEvent
513
514**Parameters**
515
516| Name  | Type                | Mandatory| Description                |
517| -------- | -------------------- | ---- | -------------------- |
518| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
519
520**Example**
521
522```ts
523let subscriber;	// Subscriber object successfully created.
524
525// Callback for clearing the aborted state of the current common event.
526function clearAbortCB(err) {
527    if (err.code) {
528        console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
529    } else {
530        console.info("clearAbortCommonEvent");
531    }
532}
533subscriber.clearAbortCommonEvent(clearAbortCB);
534```
535
536## clearAbortCommonEvent
537
538```ts
539clearAbortCommonEvent(): Promise<void>
540```
541
542Clears the aborted state of this common event. This API takes effect only for ordered common events. It uses a promise to return the result.
543
544**System capability**: SystemCapability.Notification.CommonEvent
545
546**Return value**
547
548| Type            | Description                |
549| ---------------- | -------------------- |
550| Promise\<void>   | Promise used to return the result.|
551
552**Example**
553
554```ts
555let subscriber;	// Subscriber object successfully created.
556
557subscriber.clearAbortCommonEvent().then(() => {
558    console.info("clearAbortCommonEvent");
559}).catch((err) => {
560    console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
561});
562```
563
564## getAbortCommonEvent
565
566```ts
567getAbortCommonEvent(callback: AsyncCallback<boolean>): void
568```
569
570Checks whether this common event is in the aborted state. This API takes effect only for ordered common events. It uses an asynchronous callback to return the result.
571
572**System capability**: SystemCapability.Notification.CommonEvent
573
574**Parameters**
575
576| Name  | Type                   | Mandatory| Description                              |
577| -------- | ----------------------- | ---- | ---------------------------------- |
578| callback | AsyncCallback\<boolean> | Yes  | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise.|
579
580**Example**
581
582```ts
583let subscriber;	// Subscriber object successfully created.
584
585// Callback for checking whether the current common event is in the aborted state.
586function getAbortCB(err, abortEvent) {
587    if (err.code) {
588        console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
589    } else {
590        console.info("abortCommonEvent " + abortEvent)
591    }
592}
593subscriber.getAbortCommonEvent(getAbortCB);
594```
595
596## getAbortCommonEvent
597
598```ts
599getAbortCommonEvent(): Promise<boolean>
600```
601
602Checks whether this common event is in the aborted state. This API takes effect only for ordered common events. It uses a promise to return the result.
603
604**System capability**: SystemCapability.Notification.CommonEvent
605
606**Return value**
607
608| Type             | Description                              |
609| ----------------- | ---------------------------------- |
610| Promise\<boolean> | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise.|
611
612**Example**
613
614```ts
615let subscriber;	// Subscriber object successfully created.
616
617subscriber.getAbortCommonEvent().then((abortEvent) => {
618    console.info("abortCommonEvent " + JSON.stringify(abortEvent));
619}).catch((err) => {
620    console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
621});
622```
623
624## getSubscribeInfo
625
626```ts
627getSubscribeInfo(callback: AsyncCallback<CommonEventSubscribeInfo>): void
628```
629
630Obtains the subscriber information. This API uses an asynchronous callback to return the result.
631
632**System capability**: SystemCapability.Notification.CommonEvent
633
634**Parameters**
635
636| Name  | Type                                                        | Mandatory| Description                  |
637| -------- | ------------------------------------------------------------ | ---- | ---------------------- |
638| callback | AsyncCallback\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Yes  | Callback used to return the subscriber information.|
639
640**Example**
641
642```ts
643let subscriber;	// Subscriber object successfully created.
644
645// Callback for subscriber information obtaining.
646function getCB(err, subscribeInfo) {
647    if (err.code) {
648        console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
649    } else {
650        console.info("subscribeInfo " + JSON.stringify(subscribeInfo));
651    }
652}
653subscriber.getSubscribeInfo(getCB);
654```
655
656## getSubscribeInfo
657
658```ts
659getSubscribeInfo(): Promise<CommonEventSubscribeInfo>
660```
661
662Obtains the subscriber information. This API uses a promise to return the result.
663
664**System capability**: SystemCapability.Notification.CommonEvent
665
666**Return value**
667
668| Type                                                        | Description                  |
669| ------------------------------------------------------------ | ---------------------- |
670| Promise\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Promise used to return the subscriber information.|
671
672**Example**
673
674```ts
675let subscriber;	// Subscriber object successfully created.
676
677subscriber.getSubscribeInfo().then((subscribeInfo) => {
678    console.info("subscribeInfo " + JSON.stringify(subscribeInfo));
679}).catch((err) => {
680    console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
681});
682```
683
684## finishCommonEvent<sup>9+</sup>
685
686```ts
687finishCommonEvent(callback: AsyncCallback<void>): void
688```
689
690Finishes this common event. This API takes effect only for ordered common events. It uses an asynchronous callback to return the result.
691
692**System capability**: SystemCapability.Notification.CommonEvent
693
694**Parameters**
695
696| Name  | Type                 | Mandatory| Description                             |
697| -------- | -------------------- | ---- | -------------------------------- |
698| callback | AsyncCallback\<void> | Yes  | Callback returned after the ordered common event is finished.|
699
700**Example**
701
702```ts
703let subscriber; // Subscriber object successfully created.
704
705// Callback for ordered common event finishing.
706function finishCB(err) {
707  if (err.code) {
708    console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
709} else {
710    console.info("FinishCommonEvent");
711}
712
713subscriber.finishCommonEvent(finishCB);
714```
715
716## finishCommonEvent<sup>9+</sup>
717
718```ts
719finishCommonEvent(): Promise<void\>
720```
721
722Finishes this common event. This API takes effect only for ordered common events. It uses a promise to return the result.
723
724**System capability**: SystemCapability.Notification.CommonEvent
725
726**Return value**
727
728| Type            | Description                |
729| ---------------- | -------------------- |
730| Promise\<void>   | Promise used to return the result.|
731
732**Example**
733
734```ts
735let subscriber;	// Subscriber object successfully created.
736
737subscriber.finishCommonEvent().then(() => {
738    console.info("FinishCommonEvent");
739}).catch((err) => {
740    console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
741});
742```
743