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