1# Subscribing to Common Events in Dynamic Mode
2
3
4## When to Use
5
6In dynamic subscription mode, an application, when it is running, subscribes to a common event and then receives the event once it is published, together with the parameters passed in the event.
7
8For example, if an application expects to be notified of low battery so that it can reduce power consumption accordingly when running, then the application can subscribe to the low-battery event. Upon receiving the event, the application can close some unnecessary tasks to reduce power consumption.
9
10Certain system common events [require specific permissions](../../security/AccessToken/determine-application-mode.md) to subscribe to. For details, see [System Common Events](../../reference/apis-basic-services-kit/common_event/commonEventManager-definitions.md).
11
12
13## Available APIs
14
15For details about the APIs, see [API Reference](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#commoneventmanagersubscribe).
16
17| API| Description|
18| -------- | -------- |
19| createSubscriber(subscribeInfo: [CommonEventSubscribeInfo](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventSubscribeInfo.md), callback: AsyncCallback<[CommonEventSubscriber](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventSubscriber.md#how-to-use)>): void| Creates a subscriber. This API uses an asynchronous callback to return the result.|
20| createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber> | Creates a subscriber. This API uses a promise to return the result.|
21| subscribe(subscriber:&nbsp;CommonEventSubscriber,&nbsp;callback:&nbsp;AsyncCallback<CommonEventData\>):&nbsp;void | Subscribes to common events.|
22
23
24## How to Develop
25
261. Import the **commonEventManager** module.
27
28   ```ts
29   import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
30   import { hilog } from '@kit.PerformanceAnalysisKit';
31
32   const TAG: string = 'ProcessModel';
33   const DOMAIN_NUMBER: number = 0xFF00;
34   ```
35
362. Create a **subscribeInfo** object. For details about the data types and parameters of the object, see [CommonEventSubscribeInfo](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventSubscribeInfo.md).
37
38   ```ts
39   // Used to save the created subscriber object for subsequent subscription and unsubscription.
40   let subscriber: commonEventManager.CommonEventSubscriber | null = null;
41   //Subscriber information. Replace the event field with the actual event name.
42   let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
43       events: ['event'], // Subscribe to the common event screen-off.
44   };
45   ```
46
473. Create a subscriber object and save the returned object for subsequent operations such as subscription and unsubscription.
48
49   ```ts
50   // Callback for subscriber creation.
51   commonEventManager.createSubscriber(subscribeInfo, (err: BusinessError, data: commonEventManager.CommonEventSubscriber) => {
52     if (err) {
53       hilog.error(DOMAIN_NUMBER, TAG, `Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
54       return;
55     }
56     hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in creating subscriber.');
57     subscriber = data;
58   })
59   ```
60
614. Create a subscription callback, which is triggered when an event is received. The data returned in the subscription callback contains information such as the common event name and data carried by the publisher. For details about the data types and parameters of the common event data, see [CommonEventData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventData.md).
62
63   ```ts
64   // Callback for common event subscription.
65   if (subscriber !== null) {
66     commonEventManager.subscribe(subscriber, (err: BusinessError, data: commonEventManager.CommonEventData) => {
67       if (err) {
68         hilog.error(DOMAIN_NUMBER, TAG, `Failed to subscribe common event. Code is ${err.code}, message is ${err.message}`);
69         return;
70       }
71       // ...
72     })
73   } else {
74     hilog.error(DOMAIN_NUMBER, TAG, `Need create subscriber`);
75   }
76   ```
77
78<!--RP1-->
79<!--RP1End-->
80