• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 # @ohos.commonEventManager (Common Event)
2 
3 The **CommonEventManager** module provides common event capabilities, including the capabilities to publish, subscribe to, and unsubscribe from common events.
4 
5 > **NOTE**
6 >
7 > The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8 
9 ## Modules to Import
10 
11 ```ts
12 import CommonEventManager from '@ohos.commonEventManager';
13 ```
14 
15 ## Support
16 
17 A system common event is an event that is published by a system service or system application and requires specific permissions to subscribe to. To publish or subscribe to this type of event, you must follow the event-specific definitions.
18 
19 For details about the definitions of all system common events, see [System Common Events](./commonEventManager-definitions.md).
20 
21 ## CommonEventManager.publish
22 
23 publish(event: string, callback: AsyncCallback\<void>): void
24 
25 Publishes a common event and executes an asynchronous callback after the event is published.
26 
27 **System capability**: SystemCapability.Notification.CommonEvent
28 
29 **Parameters**
30 
31 | Name    | Type                | Mandatory| Description                  |
32 | -------- | -------------------- | ---- | ---------------------- |
33 | event    | string               | Yes  | Name of the common event to publish.|
34 | callback | AsyncCallback\<void> | Yes  | Callback to execute after the event is published.|
35 
36 **Error codes**
37 
38  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
39 
40 | ID| Error Message                           |
41 | -------- | ----------------------------------- |
42 | 1500004  | not System services.                |
43 | 1500007  | error sending message to Common Event Service. |
44 | 1500008  | Common Event Service does not complete initialization. |
45 | 1500009  | error obtaining system parameters.  |
46 
47 **Example**
48 
49 ```ts
50 import Base from '@ohos.base';
51 
52 // Callback for common event publication
53 function publishCB(err:Base.BusinessError) {
54     if (err) {
55         console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
56     } else {
57         console.info("publish");
58     }
59 }
60 
61 // Publish a common event.
62 try {
63     CommonEventManager.publish("event", publishCB);
64 } catch (error) {
65     let err:Base.BusinessError = error as Base.BusinessError;
66     console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
67 }
68 ```
69 
70 ## CommonEventManager.publish
71 
72 publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
73 
74 Publishes a common event with given attributes. This API uses an asynchronous callback to return the result.
75 
76 **System capability**: SystemCapability.Notification.CommonEvent
77 
78 **Parameters**
79 
80 | Name    | Type                  | Mandatory| Description                  |
81 | -------- | ---------------------- | ---- | ---------------------- |
82 | event    | string                 | Yes  | Name of the common event to publish. |
83 | options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | Yes  | Attributes of the common event to publish.|
84 | callback | syncCallback\<void>   | Yes  | Callback used to return the result. |
85 
86 **Error codes**
87 
88  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
89 
90 | ID| Error Message                           |
91 | -------- | ----------------------------------- |
92 | 1500004  | not System services.                |
93 | 1500007  | error sending message to Common Event Service. |
94 | 1500008  | Common Event Service does not complete initialization. |
95 | 1500009  | error obtaining system parameters.  |
96 
97 **Example**
98 
99 ```ts
100 import Base from '@ohos.base';
101 
102 // Attributes of a common event.
103 let options:CommonEventManager.CommonEventPublishData = {
104 	code: 0,			 // Result code of the common event.
105 	data: "initial data",// Result data of the common event.
106 	isOrdered: true	 // The common event is an ordered one.
107 }
108 
109 // Callback for common event publication
110 function publishCB(err:Base.BusinessError) {
111 	if (err) {
112         console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
113     } else {
114         console.info("publish");
115     }
116 }
117 
118 // Publish a common event.
119 try {
120     CommonEventManager.publish("event", options, publishCB);
121 } catch (error) {
122     let err:Base.BusinessError = error as Base.BusinessError;
123     console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
124 }
125 ```
126 
127 ## CommonEventManager.publishAsUser<sup>
128 
129 publishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void
130 
131 Publishes a common event to a specific user. This API uses an asynchronous callback to return the result.
132 
133 **System capability**: SystemCapability.Notification.CommonEvent
134 
135 **System API**: This is a system API and cannot be called by third-party applications.
136 
137 **Parameters**
138 
139 | Name    | Type                | Mandatory| Description                              |
140 | -------- | -------------------- | ---- | ---------------------------------- |
141 | event    | string               | Yes  | Name of the common event to publish.            |
142 | userId   | number               | Yes  | User ID.|
143 | callback | AsyncCallback\<void> | Yes  | Callback used to return the result.            |
144 
145 **Error codes**
146 
147  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
148 
149 | ID| Error Message                           |
150 | -------- | ----------------------------------- |
151 | 1500004  | not System services.                |
152 | 1500007  | error sending message to Common Event Service. |
153 | 1500008  | Common Event Service does not complete initialization. |
154 | 1500009  | error obtaining system parameters.  |
155 
156 **Example**
157 
158 ```ts
159 import Base from '@ohos.base';
160 
161 // Callback for common event publication
162 function publishCB(err:Base.BusinessError) {
163 	if (err) {
164         console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
165     } else {
166         console.info("publishAsUser");
167     }
168 }
169 
170 // Specify the user to whom the common event will be published.
171 let userId = 100;
172 
173 // Publish a common event.
174 try {
175     CommonEventManager.publishAsUser("event", userId, publishCB);
176 } catch (error) {
177     let err:Base.BusinessError = error as Base.BusinessError;
178     console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
179 }
180 ```
181 
182 ## CommonEventManager.publishAsUser
183 
184 publishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
185 
186 Publishes a common event with given attributes to a specific user. This API uses an asynchronous callback to return the result.
187 
188 **System capability**: SystemCapability.Notification.CommonEvent
189 
190 **System API**: This is a system API and cannot be called by third-party applications.
191 
192 **Parameters**
193 
194 | Name    | Type                  | Mandatory| Description                  |
195 | -------- | ---------------------- | ---- | ---------------------- |
196 | event    | string                 | Yes  | Name of the common event to publish. |
197 | userId   | number | Yes| User ID.|
198 | options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | Yes  | Attributes of the common event to publish.|
199 | callback | AsyncCallback\<void>   | Yes  | Callback used to return the result. |
200 
201 **Error codes**
202 
203  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
204 
205 | ID| Error Message                           |
206 | -------- | ----------------------------------- |
207 | 1500004  | not System services or System app.                |
208 | 1500007  | error sending message to Common Event Service. |
209 | 1500008  | Common Event Service does not complete initialization. |
210 | 1500009  | error obtaining system parameters.  |
211 
212 **Example**
213 
214 
215 ```ts
216 import Base from '@ohos.base';
217 
218 // Attributes of a common event.
219 let options:CommonEventManager.CommonEventPublishData = {
220 	code: 0,			 // Result code of the common event.
221 	data: "initial data",// Result data of the common event.
222 }
223 
224 // Callback for common event publication.
225 function publishCB(err:Base.BusinessError) {
226 	if (err) {
227         console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
228     } else {
229         console.info("publishAsUser");
230     }
231 }
232 
233 // Specify the user to whom the common event will be published.
234 let userId = 100;
235 
236 // Publish a common event.
237 try {
238     CommonEventManager.publishAsUser("event", userId, options, publishCB);
239 } catch (error) {
240     let err:Base.BusinessError = error as Base.BusinessError;
241     console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
242 }
243 ```
244 
245 ## CommonEventManager.createSubscriber
246 
247 createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void
248 
249 Creates a subscriber. This API uses an asynchronous callback to return the result.
250 
251 **System capability**: SystemCapability.Notification.CommonEvent
252 
253 **Parameters**
254 
255 | Name         | Type                                                        | Mandatory| Description                      |
256 | ------------- | ------------------------------------------------------------ | ---- | -------------------------- |
257 | subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)        | Yes  | Subscriber information.            |
258 | callback      | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Yes  | Callback used to return the result.|
259 
260 **Example**
261 
262 ```ts
263 import Base from '@ohos.base';
264 
265 let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
266 
267 // Subscriber information.
268 let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
269     events: ["event"]
270 };
271 
272 // Callback for subscriber creation.
273 function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
274     if(!err) {
275         console.info("createSubscriber");
276         subscriber = commonEventSubscriber;
277     } else {
278         console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
279     }
280 }
281 
282 // Create a subscriber.
283 try {
284     CommonEventManager.createSubscriber(subscribeInfo, createCB);
285 } catch (error) {
286     let err:Base.BusinessError = error as Base.BusinessError;
287     console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
288 }
289 ```
290 
291 ## CommonEventManager.createSubscriber
292 
293 createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber>
294 
295 Creates a subscriber. 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 | subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Yes  | Subscriber information.|
304 
305 **Return value**
306 | Type                                                     | Description            |
307 | --------------------------------------------------------- | ---------------- |
308 | Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Promise used to return the subscriber object.|
309 
310 **Example**
311 
312 ```ts
313 import Base from '@ohos.base';
314 
315 let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
316 
317 // Subscriber information.
318 let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
319 	events: ["event"]
320 };
321 
322 // Create a subscriber.
323 CommonEventManager.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
324     console.info("createSubscriber");
325     subscriber = commonEventSubscriber;
326 }).catch((err:Base.BusinessError) => {
327     console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
328 });
329 
330 ```
331 
332 ## CommonEventManager.createSubscriberSync<sup>10+</sup>
333 
334 createSubscriberSync(subscribeInfo: CommonEventSubscribeInfo): CommonEventSubscriber
335 
336 Creates a subscriber. The API returns the result synchronously.
337 
338 **System capability**: SystemCapability.Notification.CommonEvent
339 
340 **Parameters**
341 
342 | Name         | Type                                                 | Mandatory| Description          |
343 | ------------- | ----------------------------------------------------- | ---- | -------------- |
344 | subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Yes  | Subscriber information.|
345 
346 **Return value**
347 | Type                                                     | Description            |
348 | --------------------------------------------------------- | ---------------- |
349 | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Promise used to return the subscriber object.|
350 
351 **Example**
352 
353 ```ts
354 import Base from '@ohos.base';
355 
356 let subscriber: CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
357 
358 // Subscriber information.
359 let subscribeInfo: CommonEventManager.CommonEventSubscribeInfo = {
360   events: ["event"]
361 };
362 
363 // Create a subscriber.
364 try {
365     subscriber = CommonEventManager.createSubscriberSync(subscribeInfo);
366 } catch (error) {
367     let err:Base.BusinessError = error as Base.BusinessError;
368     console.error(`createSubscriberSync failed, code is ${err.code}, message is ${err.message}`);
369 }
370 
371 ```
372 
373 ## CommonEventManager.subscribe
374 
375 subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void
376 
377 Subscribes to common events. This API uses an asynchronous callback to return the result.
378 
379 **System capability**: SystemCapability.Notification.CommonEvent
380 
381 **Parameters**
382 
383 | Name      | Type                                               | Mandatory| Description                            |
384 | ---------- | ---------------------------------------------------- | ---- | -------------------------------- |
385 | subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)     | Yes  | Subscriber object.                |
386 | callback   | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | Yes  | Callback used to return the result.|
387 
388 **Error codes**
389 
390  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
391 
392 | ID| Error Message                           |
393 | -------- | ----------------------------------- |
394 | 801  | capability not supported.               |
395 | 1500007  | error sending message to Common Event Service. |
396 | 1500008  | Common Event Service does not complete initialization. |
397 
398 **Example**
399 
400 ```ts
401 import Base from '@ohos.base';
402 
403 let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
404 
405 // Subscriber information.
406 let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
407     events: ["event"]
408 };
409 
410 // Callback for common event subscription.
411 function SubscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
412     if (err) {
413         console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
414     } else {
415         console.info("subscribe ");
416     }
417 }
418 
419 // Callback for subscriber creation.
420 function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
421     if(!err) {
422         console.info("createSubscriber");
423         subscriber = commonEventSubscriber;
424         // Subscribe to a common event.
425         try {
426             CommonEventManager.subscribe(subscriber, SubscribeCB);
427         } catch (error) {
428             let err:Base.BusinessError = error as Base.BusinessError;
429             console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
430         }
431     } else {
432         console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
433     }
434 }
435 
436 // Create a subscriber.
437 try {
438     CommonEventManager.createSubscriber(subscribeInfo, createCB);
439 } catch (error) {
440     let err:Base.BusinessError = error as Base.BusinessError;
441     console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
442 }
443 ```
444 
445 ## CommonEventManager.unsubscribe
446 
447 unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void
448 
449 Unsubscribes from common events. This API uses an asynchronous callback to return the result.
450 
451 **System capability**: SystemCapability.Notification.CommonEvent
452 
453 **Parameters**
454 
455 | Name      | Type                                            | Mandatory| Description                    |
456 | ---------- | ----------------------------------------------- | ---- | ------------------------ |
457 | subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Yes  | Subscriber object.        |
458 | callback   | AsyncCallback\<void>                            | No  | Callback used to return the result.|
459 
460 **Error codes**
461 
462  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
463 
464 | ID| Error Message                           |
465 | -------- | ----------------------------------- |
466 | 801  | capability not supported.               |
467 | 1500007  | error sending message to Common Event Service. |
468 | 1500008  | Common Event Service does not complete initialization. |
469 
470 **Example**
471 
472 ```ts
473 import Base from '@ohos.base';
474 
475 let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription.
476 // Subscriber information.
477 let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
478     events: ["event"]
479 };
480 // Callback for common event subscription.
481 function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
482     if (err) {
483         console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
484     } else {
485         console.info("subscribe");
486     }
487 }
488 // Callback for subscriber creation.
489 function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
490     if (err) {
491         console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
492     } else {
493         console.info("createSubscriber");
494         subscriber = commonEventSubscriber;
495         // Subscribe to a common event.
496         try {
497             CommonEventManager.subscribe(subscriber, subscribeCB);
498         } catch (error) {
499             let err:Base.BusinessError = error as Base.BusinessError;
500             console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
501         }
502     }
503 }
504 // Callback for common event unsubscription.
505 function unsubscribeCB(err:Base.BusinessError) {
506     if (err) {
507         console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
508     } else {
509         console.info("unsubscribe");
510     }
511 }
512 // Create a subscriber.
513 try {
514     CommonEventManager.createSubscriber(subscribeInfo, createCB);
515 } catch (error) {
516     let err:Base.BusinessError = error as Base.BusinessError;
517     console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
518 }
519 
520 // Unsubscribe from the common event.
521 // Wait until execution of the asynchronous API subscribe is completed. Add setTimeout when necessary.
522 setTimeout(() => {
523   try {
524     CommonEventManager.unsubscribe(subscriber, unsubscribeCB);
525   } catch (error) {
526     let err:Base.BusinessError = error as Base.BusinessError;
527     console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
528   }
529 }, 500);
530 ```
531 
532 ## CommonEventManager.removeStickyCommonEvent<sup>10+</sup>
533 
534 removeStickyCommonEvent(event: string, callback: AsyncCallback\<void>): void
535 
536 Removes a sticky common event. This API uses an asynchronous callback to return the result.
537 
538 **System capability**: SystemCapability.Notification.CommonEvent
539 
540 **Required permissions**: ohos.permission.COMMONEVENT_STICKY
541 
542 **System API**: This is a system API and cannot be called by third-party applications.
543 
544 **Parameters**
545 
546 | Name  | Type                | Mandatory| Description                            |
547 | -------- | -------------------- | ---- | -------------------------------- |
548 | event    | string               | Yes  | Sticky common event to remove.      |
549 | callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
550 
551 **Error codes**
552 
553  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
554 
555 | ID| Error Message                           |
556 | -------- | ----------------------------------- |
557 | 1500004  | not system service.                 |
558 | 1500007  | error sending message to Common Event Service.             |
559 | 1500008  | Common Event Service does not complete initialization.     |
560 
561 **Example**
562 
563 
564 ```ts
565 import Base from '@ohos.base';
566 
567 CommonEventManager.removeStickyCommonEvent("sticky_event", (err:Base.BusinessError) => {
568     if (err) {
569         console.info(`Remove sticky event AsyncCallback failed, errCode: ${err.code}, errMes: ${err.message}`);
570         return;
571     }
572     console.info(`Remove sticky event AsyncCallback success`);
573 });
574 ```
575 
576 ## CommonEventManager.removeStickyCommonEvent<sup>10+</sup>
577 
578 removeStickyCommonEvent(event: string): Promise\<void>
579 
580 Removes a sticky common event. This API uses a promise to return the result.
581 
582 **System capability**: SystemCapability.Notification.CommonEvent
583 
584 **Required permissions**: ohos.permission.COMMONEVENT_STICKY
585 
586 **System API**: This is a system API and cannot be called by third-party applications.
587 
588 **Parameters**
589 
590 | Name| Type  | Mandatory| Description                      |
591 | ------ | ------ | ---- | -------------------------- |
592 | event  | string | Yes  | Sticky common event to remove.|
593 
594 **Return value**
595 
596 | Type          | Description                        |
597 | -------------- | ---------------------------- |
598 | Promise\<void> | Promise used to return the result.|
599 
600 **Error codes**
601 
602  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
603 
604 | ID| Error Message                           |
605 | -------- | ----------------------------------- |
606 | 1500004  | not system service.                 |
607 | 1500007  | error sending message to Common Event Service.             |
608 | 1500008  | Common Event Service does not complete initialization.     |
609 
610 **Example**
611 
612 
613 ```ts
614 import Base from '@ohos.base';
615 
616 CommonEventManager.removeStickyCommonEvent("sticky_event").then(() => {
617     console.info(`Remove sticky event AsyncCallback success`);
618 }).catch ((err:Base.BusinessError) => {
619     console.info(`Remove sticky event AsyncCallback failed, errCode: ${err.code}, errMes: ${err.message}`);
620 });
621 ```
622 
623 ## CommonEventManager.setStaticSubscriberState<sup>10+</sup>
624 
625 setStaticSubscriberState(enable: boolean, callback: AsyncCallback\<void>): void;
626 
627 Enables or disables static subscription for the current application. This API uses an asynchronous callback to return the result.
628 
629 **Model restriction**: This API can be used only in the stage model.
630 
631 **System capability**: SystemCapability.Notification.CommonEvent
632 
633 **System API**: This is a system API and cannot be called by third-party applications.
634 
635 **Parameters**
636 
637 | Name| Type  | Mandatory| Description                      |
638 | ------ | ------ | ---- | -------------------------- |
639 | enable  | boolean | Yes  | Whether static subscription is enabled.<br> **true**: enabled.<br>**false**: disabled.|
640 | callback  | AsyncCallback\<void> | Yes  | Callback used to return the result.|
641 
642 **Error codes**
643 
644  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
645 
646 | ID| Error Message                           |
647 | -------- | ----------------------------------- |
648 | 1500007  | error sending message to Common Event Service.             |
649 | 1500008  | Common Event Service does not complete initialization.     |
650 
651 **Example**
652 
653 
654 ```ts
655 import Base from '@ohos.base';
656 
657 CommonEventManager.setStaticSubscriberState(true, (err:Base.BusinessError) => {
658     if (!err) {
659         console.info(`Set static subscriber state callback failed, err is null.`);
660         return;
661     }
662     if (err.code !== undefined && err.code != null) {
663         console.info(`Set static subscriber state callback failed, errCode: ${err.code}, errMes: ${err.message}`);
664         return;
665     }
666     console.info(`Set static subscriber state callback success`);
667 });
668 ```
669 
670 ## CommonEventManager.setStaticSubscriberState<sup>10+</sup>
671 
672 setStaticSubscriberState(enable: boolean): Promise\<void>;
673 
674 Enables or disables static subscription for the current application. This API uses a promise to return the result.
675 
676 **Model restriction**: This API can be used only in the stage model.
677 
678 **System capability**: SystemCapability.Notification.CommonEvent
679 
680 **System API**: This is a system API and cannot be called by third-party applications.
681 
682 **Parameters**
683 
684 | Name| Type  | Mandatory| Description                      |
685 | ------ | ------ | ---- | -------------------------- |
686 | enable  | boolean | Yes  | Whether static subscription is enabled.<br> **true**: enabled.<br>**false**: disabled.|
687 
688 **Return value**
689 
690 | Type          | Description                        |
691 | -------------- | ---------------------------- |
692 | Promise\<void> | Promise used to return the result.|
693 
694 **Error codes**
695 
696  For details about the error codes, see [Event Error Codes](../errorcodes/errorcode-CommonEventService.md).
697 
698 | ID| Error Message                           |
699 | -------- | ----------------------------------- |
700 | 1500007  | error sending message to Common Event Service.             |
701 | 1500008  | Common Event Service does not complete initialization.     |
702 
703 **Example**
704 
705 
706 ```ts
707 import Base from '@ohos.base';
708 
709 CommonEventManager.setStaticSubscriberState(false).then(() => {
710     console.info(`Set static subscriber state promise success`);
711 }).catch ((err:Base.BusinessError) => {
712     console.info(`Set static subscriber state promise failed, errCode: ${err.code}, errMes: ${err.message}`);
713 });
714 ```
715