js-apis-inner-commonEvent-commonEventSubscriber.md 21.4 KB
Newer Older
E
ester.zhou 已提交
1 2
# CommonEventSubscriber

E
ester.zhou 已提交
3 4 5 6 7 8 9
> **NOTE**
>
> 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.

## Usage

Before using the **CommonEventSubscriber** module, you must obtain a **subscriber** object by calling **CommonEvent.createSubscriber**.
E
ester.zhou 已提交
10 11

```ts
E
ester.zhou 已提交
12
import CommonEvent from '@ohos.commonEvent';
X
XKK 已提交
13 14 15
import CommonEventManager from '@ohos.commonEventManager';
import Base from '@ohos.base';
let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
E
ester.zhou 已提交
16 17

// Subscriber information.
X
XKK 已提交
18
let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
E
ester.zhou 已提交
19 20 21 22
	events: ["event"]
};

// Callback for subscriber creation.
X
XKK 已提交
23
function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
X
XKK 已提交
24
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
25 26 27 28 29 30 31 32 33
        console.error(`createSubscriber failed, code is ${err.code}`);
    } else {
        console.info("createSubscriber");
        subscriber = commonEventSubscriber;
    }
}

// Create a subscriber.
CommonEvent.createSubscriber(subscribeInfo, createCB);
E
ester.zhou 已提交
34 35
```

E
ester.zhou 已提交
36 37 38 39
## getCode

getCode(callback: AsyncCallback\<number>): void

E
ester.zhou 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53
Obtains the code of this common event. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                  | Mandatory| Description              |
| -------- | ---------------------- | ---- | ------------------ |
| callback | AsyncCallback\<number\> | Yes  | Common event code.|

**Example**

```ts
// Callback for result code obtaining of an ordered common event.
X
XKK 已提交
54
function getCodeCB(err:Base.BusinessError, code:number) {
X
XKK 已提交
55
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
56 57 58 59 60 61 62 63 64 65
        console.error(`getCode failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("getCode " + JSON.stringify(code));
    }
}
subscriber.getCode(getCodeCB);
```

## getCode

E
ester.zhou 已提交
66
getCode(): Promise\<number>
E
ester.zhou 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80

Obtains the code of this common event. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<number> | Common event code.|

**Example**

```ts
X
XKK 已提交
81
subscriber.getCode().then((code:number) => {
E
ester.zhou 已提交
82
    console.info("getCode " + JSON.stringify(code));
X
XKK 已提交
83
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
84 85 86 87 88 89
    console.error(`getCode failed, code is ${err.code}, message is ${err.message}`);
});
```

## setCode

E
ester.zhou 已提交
90
setCode(code: number, callback: AsyncCallback\<void>): void
E
ester.zhou 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

Sets the code for this common event. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                | Mandatory| Description                  |
| -------- | -------------------- | ---- | ---------------------- |
| code     | number               | Yes  | Common event code.  |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

```ts
// Callback for result code setting of an ordered common event.
X
XKK 已提交
107
function setCodeCB(err:Base.BusinessError) {
X
XKK 已提交
108
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
109 110 111 112 113 114 115 116 117 118
        console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setCode");
    }
}
subscriber.setCode(1, setCodeCB);
```

## setCode

E
ester.zhou 已提交
119
setCode(code: number): Promise\<void>
E
ester.zhou 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

Sets the code for this common event. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name| Type  | Mandatory| Description              |
| ------ | ------ | ---- | ------------------ |
| code   | number | Yes  | Common event code.|

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<void>   | Promise used to return the result.|

**Example**

```ts
subscriber.setCode(1).then(() => {
    console.info("setCode");
X
XKK 已提交
142
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
143 144 145 146 147 148
    console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
});
```

## getData

E
ester.zhou 已提交
149
getData(callback: AsyncCallback\<string>): void
E
ester.zhou 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

Obtains the data of this common event. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                  | Mandatory| Description                |
| -------- | ---------------------- | ---- | -------------------- |
| callback | AsyncCallback\<string> | Yes  | Common event data.|

**Example**

```ts
// Callback for result data obtaining of an ordered common event.
X
XKK 已提交
165
function getDataCB(err:Base.BusinessError, data:string) {
X
XKK 已提交
166
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
167 168 169 170 171 172 173 174 175 176
        console.error(`getData failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("getData " + JSON.stringify(data));
    }
}
subscriber.getData(getDataCB);
```

## getData

E
ester.zhou 已提交
177
getData(): Promise\<string>
E
ester.zhou 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191

Obtains the data of this common event. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type            | Description              |
| ---------------- | ------------------ |
| Promise\<string> | Common event data.|

**Example**

```ts
X
XKK 已提交
192
subscriber.getData().then((data:string) => {
E
ester.zhou 已提交
193
    console.info("getData " + JSON.stringify(data));
X
XKK 已提交
194
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    console.error(`getData failed, code is ${err.code}, message is ${err.message}`);
});
```

## setData

setData(data: string, callback: AsyncCallback\<void>): void

Sets the data for this common event. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                | Mandatory| Description                |
| -------- | -------------------- | ---- | -------------------- |
| data     | string               | Yes  | Common event data.  |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

```ts
// Callback for result data setting of an ordered common event
X
XKK 已提交
218
function setDataCB(err:Base.BusinessError) {
X
XKK 已提交
219
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
220 221 222 223 224 225 226 227 228 229
        console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setData");
    }
}
subscriber.setData("publish_data_changed", setDataCB);
```

## setData

E
ester.zhou 已提交
230
setData(data: string): Promise\<void>
E
ester.zhou 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

Sets the data for this common event. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name| Type  | Mandatory| Description                |
| ------ | ------ | ---- | -------------------- |
| data   | string | Yes  | Common event data.|

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<void>   | Promise used to return the result.|

**Example**

```ts
subscriber.setData("publish_data_changed").then(() => {
    console.info("setData");
X
XKK 已提交
253
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
254 255 256 257 258 259
    console.error(`setCode failed, code is ${err.code}, message is ${err.message}`);
});
```

## setCodeAndData

E
ester.zhou 已提交
260
setCodeAndData(code: number, data: string, callback:AsyncCallback\<void>): void
E
ester.zhou 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

Sets the code and data for this common event. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                | Mandatory| Description                  |
| -------- | -------------------- | ---- | ---------------------- |
| code     | number               | Yes  | Common event code.  |
| data     | string               | Yes  | Common event data.  |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

```ts
// Callback for code and data setting of an ordered common event.
X
XKK 已提交
278
function setCodeDataCB(err:Base.BusinessError) {
X
XKK 已提交
279
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
280 281 282 283 284 285 286 287 288 289
        console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("setCodeDataCallback");
    }
}
subscriber.setCodeAndData(1, "publish_data_changed", setCodeDataCB);
```

## setCodeAndData

E
ester.zhou 已提交
290
setCodeAndData(code: number, data: string): Promise\<void>
E
ester.zhou 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

Sets the code and data for this common event. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name| Type  | Mandatory| Description                |
| ------ | ------ | ---- | -------------------- |
| code   | number | Yes  | Common event code.|
| data   | string | Yes  | Common event data.|

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<void>   | Promise used to return the result.|

**Example**

```ts
subscriber.setCodeAndData(1, "publish_data_changed").then(() => {
    console.info("setCodeAndData");
X
XKK 已提交
314
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
315 316 317 318 319 320
    console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`);
});
```

## isOrderedCommonEvent

E
ester.zhou 已提交
321
isOrderedCommonEvent(callback: AsyncCallback\<boolean>): void
E
ester.zhou 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

Checks whether this common event is an ordered one. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                   | Mandatory| Description                              |
| -------- | ----------------------- | ---- | ---------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Returns **true** if the common event is an ordered one; returns **false** otherwise.|

**Example**

```ts
// Callback for checking whether the current common event is an ordered one.
X
XKK 已提交
337
function isOrderedCB(err:Base.BusinessError, isOrdered:boolean) {
X
XKK 已提交
338
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
339 340 341 342 343 344 345 346 347 348
        console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("isOrdered " + JSON.stringify(isOrdered));
    }
}
subscriber.isOrderedCommonEvent(isOrderedCB);
```

## isOrderedCommonEvent

E
ester.zhou 已提交
349
isOrderedCommonEvent(): Promise\<boolean>
E
ester.zhou 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363

Checks whether this common event is an ordered one. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type             | Description                            |
| ----------------- | -------------------------------- |
| Promise\<boolean> | Returns **true** if the common event is an ordered one; returns **false** otherwise.|

**Example**

```ts
X
XKK 已提交
364
subscriber.isOrderedCommonEvent().then((isOrdered:boolean) => {
E
ester.zhou 已提交
365
    console.info("isOrdered " + JSON.stringify(isOrdered));
X
XKK 已提交
366
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
367 368 369 370 371 372
    console.error(`isOrdered failed, code is ${err.code}, message is ${err.message}`);
});
```

## isStickyCommonEvent

E
ester.zhou 已提交
373
isStickyCommonEvent(callback: AsyncCallback\<boolean>): void
E
ester.zhou 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

Checks whether this common event is a sticky one. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                   | Mandatory| Description                              |
| -------- | ----------------------- | ---- | ---------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Returns **true** if the common event is a sticky one; returns **false** otherwise.|

**Example**

```ts
// Callback for checking whether the current common event is a sticky one.
X
XKK 已提交
389
function isStickyCB(err:Base.BusinessError, isSticky:boolean) {
X
XKK 已提交
390
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
391 392 393 394 395 396 397 398 399 400
        console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("isSticky " + JSON.stringify(isSticky));
    }
}
subscriber.isStickyCommonEvent(isStickyCB);
```

## isStickyCommonEvent

E
ester.zhou 已提交
401
isStickyCommonEvent(): Promise\<boolean>
E
ester.zhou 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415

Checks whether this common event is a sticky one. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type             | Description                            |
| ----------------- | -------------------------------- |
| Promise\<boolean> | Returns **true** if the common event is a sticky one; returns **false** otherwise.|

**Example**

```ts
X
XKK 已提交
416
subscriber.isStickyCommonEvent().then((isSticky:boolean) => {
E
ester.zhou 已提交
417
    console.info("isSticky " + JSON.stringify(isSticky));
X
XKK 已提交
418
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
419 420 421 422 423 424
    console.error(`isSticky failed, code is ${err.code}, message is ${err.message}`);
});
```

## abortCommonEvent

E
ester.zhou 已提交
425
abortCommonEvent(callback: AsyncCallback\<void>): void
E
ester.zhou 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

Aborts 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.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                | Mandatory| Description                |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

```ts
// Callback for common event aborting.
X
XKK 已提交
441
function abortCB(err:Base.BusinessError) {
X
XKK 已提交
442
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
443 444 445 446 447 448 449 450 451 452
		console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("abortCommonEvent");
    }
}
subscriber.abortCommonEvent(abortCB);
```

## abortCommonEvent

E
ester.zhou 已提交
453
abortCommonEvent(): Promise\<void>
E
ester.zhou 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

Aborts 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.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<void>   | Promise used to return the result.|

**Example**

```ts
subscriber.abortCommonEvent().then(() => {
    console.info("abortCommonEvent");
X
XKK 已提交
470
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
471 472 473 474 475 476
    console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
});
```

## clearAbortCommonEvent

E
ester.zhou 已提交
477
clearAbortCommonEvent(callback: AsyncCallback\<void>): void
E
ester.zhou 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

Clears 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.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                | Mandatory| Description                |
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

**Example**

```ts
// Callback for clearing the aborted state of the current common event.
X
XKK 已提交
493
function clearAbortCB(err:Base.BusinessError) {
X
XKK 已提交
494
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
495 496 497 498 499 500 501 502 503 504
        console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("clearAbortCommonEvent");
    }
}
subscriber.clearAbortCommonEvent(clearAbortCB);
```

## clearAbortCommonEvent

E
ester.zhou 已提交
505
clearAbortCommonEvent(): Promise\<void>
E
ester.zhou 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

Clears the aborted state of this common event. This API takes effect only for ordered common events. It uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<void>   | Promise used to return the result.|

**Example**

```ts
subscriber.clearAbortCommonEvent().then(() => {
    console.info("clearAbortCommonEvent");
X
XKK 已提交
522
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
523 524 525 526 527 528
    console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
});
```

## getAbortCommonEvent

E
ester.zhou 已提交
529
getAbortCommonEvent(callback: AsyncCallback\<boolean>): void
E
ester.zhou 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

Checks 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.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                   | Mandatory| Description                              |
| -------- | ----------------------- | ---- | ---------------------------------- |
| callback | AsyncCallback\<boolean> | Yes  | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise.|

**Example**

```ts
// Callback for checking whether the current common event is in the aborted state.
X
XKK 已提交
545
function getAbortCB(err:Base.BusinessError, abortEvent:boolean) {
X
XKK 已提交
546
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
547 548 549 550 551 552 553 554 555 556
        console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("abortCommonEvent " + abortEvent)
    }
}
subscriber.getAbortCommonEvent(getAbortCB);
```

## getAbortCommonEvent

E
ester.zhou 已提交
557
getAbortCommonEvent(): Promise\<boolean>
E
ester.zhou 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570 571

Checks 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.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type             | Description                              |
| ----------------- | ---------------------------------- |
| Promise\<boolean> | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise.|

**Example**

```ts
X
XKK 已提交
572
subscriber.getAbortCommonEvent().then((abortEvent:boolean) => {
E
ester.zhou 已提交
573
    console.info("abortCommonEvent " + JSON.stringify(abortEvent));
X
XKK 已提交
574
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
575 576 577 578 579 580
    console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`);
});
```

## getSubscribeInfo

E
ester.zhou 已提交
581
getSubscribeInfo(callback: AsyncCallback\<CommonEventSubscribeInfo>): void
E
ester.zhou 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

Obtains the subscriber information. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                                                        | Mandatory| Description                  |
| -------- | ------------------------------------------------------------ | ---- | ---------------------- |
| callback | AsyncCallback\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Yes  | Callback used to return the subscriber information.|

**Example**

```ts
// Callback for subscriber information obtaining.
X
XKK 已提交
597
function getCB(err:Base.BusinessError, subscribeInfo:CommonEventManager.CommonEventSubscribeInfo) {
X
XKK 已提交
598
    if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
599 600 601 602 603 604 605 606 607 608
        console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
    } else {
        console.info("subscribeInfo " + JSON.stringify(subscribeInfo));
    }
}
subscriber.getSubscribeInfo(getCB);
```

## getSubscribeInfo

E
ester.zhou 已提交
609
getSubscribeInfo(): Promise\<CommonEventSubscribeInfo>
E
ester.zhou 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623

Obtains the subscriber information. This API uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type                                                        | Description                  |
| ------------------------------------------------------------ | ---------------------- |
| Promise\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Promise used to return the subscriber information.|

**Example**

```ts
X
XKK 已提交
624
subscriber.getSubscribeInfo().then((subscribeInfo:CommonEventManager.CommonEventSubscribeInfo) => {
E
ester.zhou 已提交
625
    console.info("subscribeInfo " + JSON.stringify(subscribeInfo));
X
XKK 已提交
626
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
627 628 629 630 631 632
    console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`);
});
```

## finishCommonEvent<sup>9+</sup>

E
ester.zhou 已提交
633
finishCommonEvent(callback: AsyncCallback\<void>): void
E
ester.zhou 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

Finishes this common event. This API takes effect only for ordered common events. It uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Parameters**

| Name  | Type                 | Mandatory| Description                             |
| -------- | -------------------- | ---- | -------------------------------- |
| callback | AsyncCallback\<void> | Yes  | Callback returned after the ordered common event is finished.|

**Example**

```ts
// Callback for ordered common event finishing.
X
XKK 已提交
649
function finishCB(err:Base.BusinessError) {
X
XKK 已提交
650
  if (err.code !== undefined && err.code != null) {
E
ester.zhou 已提交
651
    console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
E
ester.zhou 已提交
652
  } else {
E
ester.zhou 已提交
653
    console.info("FinishCommonEvent");
E
ester.zhou 已提交
654
  }
E
ester.zhou 已提交
655 656 657 658 659 660 661
}

subscriber.finishCommonEvent(finishCB);
```

## finishCommonEvent<sup>9+</sup>

E
ester.zhou 已提交
662
finishCommonEvent(): Promise\<void>
E
ester.zhou 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

Finishes this common event. This API takes effect only for ordered common events. It uses a promise to return the result.

**System capability**: SystemCapability.Notification.CommonEvent

**Return value**

| Type            | Description                |
| ---------------- | -------------------- |
| Promise\<void>   | Promise used to return the result.|

**Example**

```ts
subscriber.finishCommonEvent().then(() => {
    console.info("FinishCommonEvent");
X
XKK 已提交
679
}).catch((err:Base.BusinessError) => {
E
ester.zhou 已提交
680 681 682
    console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`);
});
```