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