1e41f4b71Sopenharmony_ci# @ohos.commonEvent (Common Event) (System API)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **CommonEvent** module provides common event capabilities, including the capabilities to publish, subscribe to, and unsubscribe from common events, as well obtaining and setting the common event result code and result data.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci> The APIs provided by this module are no longer maintained since API version 9. You are advised to use [@ohos.commonEventManager](js-apis-commonEventManager.md).
7e41f4b71Sopenharmony_ci>
8e41f4b71Sopenharmony_ci> 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.
9e41f4b71Sopenharmony_ci>
10e41f4b71Sopenharmony_ci> This topic describes only system APIs provided by the module. For details about its public APIs, see [CommonEvent](./js-apis-commonEvent.md).
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## Modules to Import
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci```ts
15e41f4b71Sopenharmony_ciimport CommonEvent from '@ohos.commonEvent';
16e41f4b71Sopenharmony_ci```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## Support
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciA 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.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ciFor details about the definitions of all system common events, see [System Common Events](./common_event/commonEvent-definitions.md).
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci## CommonEvent.publishAsUser<sup>(deprecated)</sup>
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_cipublishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciPublishes a common event to a specific user. This API uses an asynchronous callback to return the result.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci> **NOTE**<br>
31e41f4b71Sopenharmony_ci> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [commonEventManager.publishAsUser](js-apis-commonEventManager-sys.md#commoneventmanagerpublishasuser) instead.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**Parameters**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci| Name    | Type                | Mandatory| Description                              |
40e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------------------- |
41e41f4b71Sopenharmony_ci| event    | string               | Yes  | Name of the common event to publish.            |
42e41f4b71Sopenharmony_ci| userId   | number               | Yes  | User ID.|
43e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.            |
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**Example**
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci```ts
48e41f4b71Sopenharmony_ciimport Base from '@ohos.base';
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci// Callback for common event publication
51e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) {
52e41f4b71Sopenharmony_ci	if (err.code) {
53e41f4b71Sopenharmony_ci        console.error(`publishAsUser failed, code is ${err.code}`);
54e41f4b71Sopenharmony_ci    } else {
55e41f4b71Sopenharmony_ci        console.info("publishAsUser");
56e41f4b71Sopenharmony_ci    }
57e41f4b71Sopenharmony_ci}
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci// Specify the user to whom the common event will be published.
60e41f4b71Sopenharmony_cilet userId = 100;
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci// Publish a common event.
63e41f4b71Sopenharmony_ciCommonEvent.publishAsUser("event", userId, publishCB);
64e41f4b71Sopenharmony_ci```
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci## CommonEvent.publishAsUser<sup>(deprecated)</sup>
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_cipublishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ciPublishes a common event with given attributes to a specific user. This API uses an asynchronous callback to return the result.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci> **NOTE**<br>
73e41f4b71Sopenharmony_ci> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [commonEventManager.publishAsUser](js-apis-commonEventManager-sys.md#commoneventmanagerpublishasuser-1) instead.
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**Parameters**
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci| Name    | Type                  | Mandatory| Description                  |
82e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ---------------------- |
83e41f4b71Sopenharmony_ci| event    | string                 | Yes  | Name of the common event to publish. |
84e41f4b71Sopenharmony_ci| userId   | number | Yes| User ID.|
85e41f4b71Sopenharmony_ci| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | Yes  | Attributes of the common event to publish.|
86e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void>   | Yes  | Callback used to return the result. |
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Example**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci```ts
92e41f4b71Sopenharmony_ciimport Base from '@ohos.base';
93e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager';
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci// Attributes of a common event.
96e41f4b71Sopenharmony_cilet options:CommonEventManager.CommonEventPublishData = {
97e41f4b71Sopenharmony_ci	code: 0,			 // Result code of the common event.
98e41f4b71Sopenharmony_ci	data: "initial data",// Result data of the common event.
99e41f4b71Sopenharmony_ci}
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci// Callback for common event publication
102e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) {
103e41f4b71Sopenharmony_ci    if (err.code) {
104e41f4b71Sopenharmony_ci        console.error(`publishAsUser failed, code is ${err.code}`);
105e41f4b71Sopenharmony_ci    } else {
106e41f4b71Sopenharmony_ci        console.info("publishAsUser");
107e41f4b71Sopenharmony_ci    }
108e41f4b71Sopenharmony_ci}
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci// Specify the user to whom the common event will be published.
111e41f4b71Sopenharmony_cilet userId = 100;
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci// Publish a common event.
114e41f4b71Sopenharmony_ciCommonEvent.publishAsUser("event", userId, options, publishCB);
115e41f4b71Sopenharmony_ci```
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ciunsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ciUnsubscribes from common events. This API uses an asynchronous callback to return the result.
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci> **NOTE**<br>
123e41f4b71Sopenharmony_ci>This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagerunsubscribe) instead.
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**Parameters**
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci| Name      | Type                                            | Mandatory| Description                    |
130e41f4b71Sopenharmony_ci| ---------- | ----------------------------------------------- | ---- | ------------------------ |
131e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Yes  | Subscriber object.        |
132e41f4b71Sopenharmony_ci| callback   | AsyncCallback\<void>                            | No  | Callback used to return the result.|
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**Example**
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci```ts
137e41f4b71Sopenharmony_ciimport Base from '@ohos.base';
138e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager';
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber;	// Used to save the created subscriber object for subsequent subscription and unsubscription.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci// Subscriber information.
143e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
144e41f4b71Sopenharmony_ci    events: ["event"]
145e41f4b71Sopenharmony_ci};
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci// Callback for common event subscription.
148e41f4b71Sopenharmony_cifunction subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
149e41f4b71Sopenharmony_ci    if (err.code) {
150e41f4b71Sopenharmony_ci        console.error(`subscribe failed, code is ${err.code}`);
151e41f4b71Sopenharmony_ci    } else {
152e41f4b71Sopenharmony_ci        console.info("subscribe " + JSON.stringify(data));
153e41f4b71Sopenharmony_ci    }
154e41f4b71Sopenharmony_ci}
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci// Callback for subscriber creation.
157e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
158e41f4b71Sopenharmony_ci    if (err.code) {
159e41f4b71Sopenharmony_ci        console.error(`createSubscriber failed, code is ${err.code}`);
160e41f4b71Sopenharmony_ci    } else {
161e41f4b71Sopenharmony_ci        console.info("createSubscriber");
162e41f4b71Sopenharmony_ci        subscriber = commonEventSubscriber;
163e41f4b71Sopenharmony_ci        // Subscribe to a common event.
164e41f4b71Sopenharmony_ci        CommonEvent.subscribe(subscriber, subscribeCB);
165e41f4b71Sopenharmony_ci    }
166e41f4b71Sopenharmony_ci}
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci// Callback for common event unsubscription.
169e41f4b71Sopenharmony_cifunction unsubscribeCB(err:Base.BusinessError) {
170e41f4b71Sopenharmony_ci    if (err.code) {
171e41f4b71Sopenharmony_ci        console.error(`unsubscribe failed, code is ${err.code}`);
172e41f4b71Sopenharmony_ci    } else {
173e41f4b71Sopenharmony_ci        console.info("unsubscribe");
174e41f4b71Sopenharmony_ci    }
175e41f4b71Sopenharmony_ci}
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci// Create a subscriber.
178e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB);
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci// Unsubscribe from the common event.
181e41f4b71Sopenharmony_ciCommonEvent.unsubscribe(subscriber, unsubscribeCB);
182e41f4b71Sopenharmony_ci```
183