1e41f4b71Sopenharmony_ci# @ohos.commonEventManager (公共事件模块)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块提供了公共事件相关的能力,包括发布公共事件、订阅公共事件、以及退订公共事件。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## 导入模块
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciimport { commonEventManager } from '@kit.BasicServicesKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## Support
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci系统公共事件是指由系统服务或系统应用发布的事件,订阅这些公共事件需要特定的权限、使用相应的值,详见[系统定义的公共事件](./common_event/commonEventManager-definitions.md)。
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## commonEventManager.publish
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_cipublish(event: string, callback: AsyncCallback\<void>): void
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci发布公共事件。使用callback异步回调。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**参数:**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci| 参数名     | 类型                 | 必填 | 说明                   |
32e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------- |
33e41f4b71Sopenharmony_ci| event    | string               | 是   | 表示要发送的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 |
34e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是   | 表示事件发布后将要执行的回调函数。 |
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci**错误码:**
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                            |
41e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 
42e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      | 
43e41f4b71Sopenharmony_ci| 1500004  | A third-party application cannot send system common events.                |
44e41f4b71Sopenharmony_ci| 1500007  | error sending message to Common Event Service. |
45e41f4b71Sopenharmony_ci| 1500008  | Common Event Service does not complete initialization. |
46e41f4b71Sopenharmony_ci| 1500009  | error obtaining system parameters.  |
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci**示例:**
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci```ts
51e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci// 发布公共事件回调
54e41f4b71Sopenharmony_cifunction publishCB(err: BusinessError) {
55e41f4b71Sopenharmony_ci  if (err) {
56e41f4b71Sopenharmony_ci    console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
57e41f4b71Sopenharmony_ci  } else {
58e41f4b71Sopenharmony_ci    console.info("publish success");
59e41f4b71Sopenharmony_ci  }
60e41f4b71Sopenharmony_ci}
61e41f4b71Sopenharmony_ci// 发布公共事件
62e41f4b71Sopenharmony_citry {
63e41f4b71Sopenharmony_ci  commonEventManager.publish("event", publishCB);
64e41f4b71Sopenharmony_ci} catch (error) {
65e41f4b71Sopenharmony_ci  let err: BusinessError = error as BusinessError;
66e41f4b71Sopenharmony_ci  console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
67e41f4b71Sopenharmony_ci}
68e41f4b71Sopenharmony_ci```
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci## commonEventManager.publish
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_cipublish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci发布公共事件。使用callback异步回调。
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci**参数:**
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci| 参数名     | 类型                   | 必填 | 说明                   |
83e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ---------------------- |
84e41f4b71Sopenharmony_ci| event    | string                 | 是   | 表示要发布的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。  |
85e41f4b71Sopenharmony_ci| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
86e41f4b71Sopenharmony_ci| callback | syncCallback\<void>   | 是   | 表示被指定的回调方法。  |
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**错误码:**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                            |
93e41f4b71Sopenharmony_ci| -------- | ----------------------------------- |
94e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      | 
95e41f4b71Sopenharmony_ci| 1500004  | A third-party application cannot send system common events.                |
96e41f4b71Sopenharmony_ci| 1500007  | error sending message to Common Event Service. |
97e41f4b71Sopenharmony_ci| 1500008  | Common Event Service does not complete initialization. |
98e41f4b71Sopenharmony_ci| 1500009  | error obtaining system parameters.  |
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci**示例:**
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci```ts
103e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci// 公共事件相关信息
106e41f4b71Sopenharmony_cilet options:commonEventManager.CommonEventPublishData = {
107e41f4b71Sopenharmony_ci  code: 0,			 // 公共事件的初始代码
108e41f4b71Sopenharmony_ci  data: "initial data",// 公共事件的初始数据
109e41f4b71Sopenharmony_ci  isOrdered: true	 // 有序公共事件
110e41f4b71Sopenharmony_ci}
111e41f4b71Sopenharmony_ci// 发布公共事件回调
112e41f4b71Sopenharmony_cifunction publishCB(err: BusinessError) {
113e41f4b71Sopenharmony_ci  if (err) {
114e41f4b71Sopenharmony_ci    console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
115e41f4b71Sopenharmony_ci  } else {
116e41f4b71Sopenharmony_ci    console.info("publish success");
117e41f4b71Sopenharmony_ci  }
118e41f4b71Sopenharmony_ci}
119e41f4b71Sopenharmony_ci// 发布公共事件
120e41f4b71Sopenharmony_citry {
121e41f4b71Sopenharmony_ci  commonEventManager.publish("event", options, publishCB);
122e41f4b71Sopenharmony_ci} catch (error) {
123e41f4b71Sopenharmony_ci  let err: BusinessError = error as BusinessError;
124e41f4b71Sopenharmony_ci  console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
125e41f4b71Sopenharmony_ci}
126e41f4b71Sopenharmony_ci```
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci## commonEventManager.createSubscriber
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_cicreateSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci创建订阅者。使用callback异步回调。
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci**参数:**
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci| 参数名          | 类型                                                         | 必填 | 说明                       |
141e41f4b71Sopenharmony_ci| ------------- | ------------------------------------------------------------ | ---- | -------------------------- |
142e41f4b71Sopenharmony_ci| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)        | 是   | 表示订阅信息。             |
143e41f4b71Sopenharmony_ci| callback      | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是   | 表示创建订阅者的回调方法。 |
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci**错误码:**
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                            |
150e41f4b71Sopenharmony_ci| -------- | ----------------------------------- |
151e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      | 
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci**示例:**
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci```ts
156e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
159e41f4b71Sopenharmony_cilet subscriber:commonEventManager.CommonEventSubscriber;
160e41f4b71Sopenharmony_ci// 订阅者信息
161e41f4b71Sopenharmony_cilet subscribeInfo:commonEventManager.CommonEventSubscribeInfo = {
162e41f4b71Sopenharmony_ci  events: ["event"]
163e41f4b71Sopenharmony_ci};
164e41f4b71Sopenharmony_ci// 创建订阅者回调
165e41f4b71Sopenharmony_cifunction createCB(err: BusinessError, commonEventSubscriber:commonEventManager.CommonEventSubscriber) {
166e41f4b71Sopenharmony_ci  if(!err) {
167e41f4b71Sopenharmony_ci    console.info("createSubscriber success");
168e41f4b71Sopenharmony_ci    subscriber = commonEventSubscriber;
169e41f4b71Sopenharmony_ci  } else {
170e41f4b71Sopenharmony_ci    console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
171e41f4b71Sopenharmony_ci  }
172e41f4b71Sopenharmony_ci}
173e41f4b71Sopenharmony_ci// 创建订阅者
174e41f4b71Sopenharmony_citry {
175e41f4b71Sopenharmony_ci  commonEventManager.createSubscriber(subscribeInfo, createCB);
176e41f4b71Sopenharmony_ci} catch (error) {
177e41f4b71Sopenharmony_ci  let err: BusinessError = error as BusinessError;
178e41f4b71Sopenharmony_ci  console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
179e41f4b71Sopenharmony_ci}
180e41f4b71Sopenharmony_ci```
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci## commonEventManager.createSubscriber
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_cicreateSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber>
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci创建订阅者。使用Promise异步回调。
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**参数:**
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci| 参数名          | 类型                                                  | 必填 | 说明           |
195e41f4b71Sopenharmony_ci| ------------- | ----------------------------------------------------- | ---- | -------------- |
196e41f4b71Sopenharmony_ci| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是   | 表示订阅信息。 |
197e41f4b71Sopenharmony_ci
198e41f4b71Sopenharmony_ci**返回值:**
199e41f4b71Sopenharmony_ci| 类型                                                      | 说明             |
200e41f4b71Sopenharmony_ci| --------------------------------------------------------- | ---------------- |
201e41f4b71Sopenharmony_ci| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 以Promise形式返回订阅者对象。 |
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci**错误码:**
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                            |
208e41f4b71Sopenharmony_ci| -------- | ----------------------------------- |
209e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      | 
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci**示例:**
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci```ts
214e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
217e41f4b71Sopenharmony_cilet subscriber:commonEventManager.CommonEventSubscriber;
218e41f4b71Sopenharmony_ci// 订阅者信息
219e41f4b71Sopenharmony_cilet subscribeInfo:commonEventManager.CommonEventSubscribeInfo = {
220e41f4b71Sopenharmony_ci  events: ["event"]
221e41f4b71Sopenharmony_ci};
222e41f4b71Sopenharmony_ci// 创建订阅者
223e41f4b71Sopenharmony_cicommonEventManager.createSubscriber(subscribeInfo).then((commonEventSubscriber:commonEventManager.CommonEventSubscriber) => {
224e41f4b71Sopenharmony_ci  console.info("createSubscriber success");
225e41f4b71Sopenharmony_ci  subscriber = commonEventSubscriber;
226e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => {
227e41f4b71Sopenharmony_ci  console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
228e41f4b71Sopenharmony_ci});
229e41f4b71Sopenharmony_ci```
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci## commonEventManager.createSubscriberSync<sup>10+</sup>
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_cicreateSubscriberSync(subscribeInfo: CommonEventSubscribeInfo): CommonEventSubscriber
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_cicreateSubscriber的同步接口。
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ci**参数:**
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci| 参数名          | 类型                                                  | 必填 | 说明           |
244e41f4b71Sopenharmony_ci| ------------- | ----------------------------------------------------- | ---- | -------------- |
245e41f4b71Sopenharmony_ci| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是   | 表示订阅信息。 |
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci**返回值:**
248e41f4b71Sopenharmony_ci| 类型                                                      | 说明             |
249e41f4b71Sopenharmony_ci| --------------------------------------------------------- | ---------------- |
250e41f4b71Sopenharmony_ci| [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 返回订阅者对象。 |
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci**错误码:**
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                            |
257e41f4b71Sopenharmony_ci| -------- | ----------------------------------- |
258e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      | 
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci**示例:**
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci```ts
263e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
266e41f4b71Sopenharmony_cilet subscriber: commonEventManager.CommonEventSubscriber;
267e41f4b71Sopenharmony_ci// 订阅者信息
268e41f4b71Sopenharmony_cilet subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
269e41f4b71Sopenharmony_ci  events: ["event"]
270e41f4b71Sopenharmony_ci};
271e41f4b71Sopenharmony_ci// 创建订阅者
272e41f4b71Sopenharmony_citry {
273e41f4b71Sopenharmony_ci  subscriber = commonEventManager.createSubscriberSync(subscribeInfo);
274e41f4b71Sopenharmony_ci} catch (error) {
275e41f4b71Sopenharmony_ci  let err: BusinessError = error as BusinessError;
276e41f4b71Sopenharmony_ci  console.error(`createSubscriberSync failed, code is ${err.code}, message is ${err.message}`);
277e41f4b71Sopenharmony_ci}
278e41f4b71Sopenharmony_ci```
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci## commonEventManager.subscribe
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_cisubscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci订阅公共事件。使用callback异步回调。
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci**参数:**
291e41f4b71Sopenharmony_ci
292e41f4b71Sopenharmony_ci| 参数名       | 类型                                                | 必填 | 说明                             |
293e41f4b71Sopenharmony_ci| ---------- | ---------------------------------------------------- | ---- | -------------------------------- |
294e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)     | 是   | 表示订阅者对象。                 |
295e41f4b71Sopenharmony_ci| callback   | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是   | 表示接收公共事件数据的回调函数。 |
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci**错误码:**
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                            |
302e41f4b71Sopenharmony_ci| -------- | ----------------------------------- |
303e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      | 
304e41f4b71Sopenharmony_ci| 801  | capability not supported.               |
305e41f4b71Sopenharmony_ci| 1500007  | error sending message to Common Event Service. |
306e41f4b71Sopenharmony_ci| 1500008  | Common Event Service does not complete initialization. |
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci**示例:**
309e41f4b71Sopenharmony_ci
310e41f4b71Sopenharmony_ci```ts
311e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
312e41f4b71Sopenharmony_ci
313e41f4b71Sopenharmony_ci// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
314e41f4b71Sopenharmony_cilet subscriber:commonEventManager.CommonEventSubscriber;
315e41f4b71Sopenharmony_ci// 订阅者信息
316e41f4b71Sopenharmony_cilet subscribeInfo:commonEventManager.CommonEventSubscribeInfo = {
317e41f4b71Sopenharmony_ci  events: ["event"]
318e41f4b71Sopenharmony_ci};
319e41f4b71Sopenharmony_ci// 订阅公共事件回调
320e41f4b71Sopenharmony_cifunction SubscribeCB(err: BusinessError, data:commonEventManager.CommonEventData) {
321e41f4b71Sopenharmony_ci  if (err) {
322e41f4b71Sopenharmony_ci    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
323e41f4b71Sopenharmony_ci  } else {
324e41f4b71Sopenharmony_ci    console.info("subscribe success, Consume callback " + JSON.stringify(data));
325e41f4b71Sopenharmony_ci  }
326e41f4b71Sopenharmony_ci}
327e41f4b71Sopenharmony_ci// 创建订阅者回调
328e41f4b71Sopenharmony_cifunction createCB(err: BusinessError, commonEventSubscriber:commonEventManager.CommonEventSubscriber) {
329e41f4b71Sopenharmony_ci  if(!err) {
330e41f4b71Sopenharmony_ci    console.info("createSubscriber success");
331e41f4b71Sopenharmony_ci    subscriber = commonEventSubscriber;
332e41f4b71Sopenharmony_ci    // 订阅公共事件
333e41f4b71Sopenharmony_ci    try {
334e41f4b71Sopenharmony_ci      commonEventManager.subscribe(subscriber, SubscribeCB);
335e41f4b71Sopenharmony_ci    } catch (error) {
336e41f4b71Sopenharmony_ci      let err: BusinessError = error as BusinessError;
337e41f4b71Sopenharmony_ci      console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
338e41f4b71Sopenharmony_ci    }
339e41f4b71Sopenharmony_ci  } else {
340e41f4b71Sopenharmony_ci    console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
341e41f4b71Sopenharmony_ci  }
342e41f4b71Sopenharmony_ci}
343e41f4b71Sopenharmony_ci// 创建订阅者
344e41f4b71Sopenharmony_citry {
345e41f4b71Sopenharmony_ci  commonEventManager.createSubscriber(subscribeInfo, createCB);
346e41f4b71Sopenharmony_ci} catch (error) {
347e41f4b71Sopenharmony_ci  let err: BusinessError = error as BusinessError;
348e41f4b71Sopenharmony_ci  console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
349e41f4b71Sopenharmony_ci}
350e41f4b71Sopenharmony_ci```
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci## commonEventManager.unsubscribe
353e41f4b71Sopenharmony_ci
354e41f4b71Sopenharmony_ciunsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ci取消订阅公共事件。使用callback异步回调。
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ci**参数:**
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci| 参数名       | 类型                                             | 必填 | 说明                     |
365e41f4b71Sopenharmony_ci| ---------- | ----------------------------------------------- | ---- | ------------------------ |
366e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是   | 表示订阅者对象。         |
367e41f4b71Sopenharmony_ci| callback   | AsyncCallback\<void>                            | 否   | 表示取消订阅的回调方法。 |
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci**错误码:**
370e41f4b71Sopenharmony_ci
371e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                            |
374e41f4b71Sopenharmony_ci| -------- | ----------------------------------- |
375e41f4b71Sopenharmony_ci| 401     | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.      | 
376e41f4b71Sopenharmony_ci| 801  | capability not supported.               |
377e41f4b71Sopenharmony_ci| 1500007  | error sending message to Common Event Service. |
378e41f4b71Sopenharmony_ci| 1500008  | Common Event Service does not complete initialization. |
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci**示例:**
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci```ts
383e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
386e41f4b71Sopenharmony_cilet subscriber:commonEventManager.CommonEventSubscriber; 
387e41f4b71Sopenharmony_ci// 订阅者信息
388e41f4b71Sopenharmony_cilet subscribeInfo:commonEventManager.CommonEventSubscribeInfo = {
389e41f4b71Sopenharmony_ci  events: ["event"]
390e41f4b71Sopenharmony_ci};
391e41f4b71Sopenharmony_ci// 订阅公共事件回调
392e41f4b71Sopenharmony_cifunction subscribeCB(err: BusinessError, data:commonEventManager.CommonEventData) {
393e41f4b71Sopenharmony_ci  if (err) {
394e41f4b71Sopenharmony_ci    console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
395e41f4b71Sopenharmony_ci  } else {
396e41f4b71Sopenharmony_ci    console.info("subscribe success, Consume callback " + JSON.stringify(data));
397e41f4b71Sopenharmony_ci  }
398e41f4b71Sopenharmony_ci}
399e41f4b71Sopenharmony_ci// 创建订阅者回调
400e41f4b71Sopenharmony_cifunction createCB(err: BusinessError, commonEventSubscriber:commonEventManager.CommonEventSubscriber) {
401e41f4b71Sopenharmony_ci  if (err) {
402e41f4b71Sopenharmony_ci    console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
403e41f4b71Sopenharmony_ci  } else {
404e41f4b71Sopenharmony_ci    console.info("createSubscriber success");
405e41f4b71Sopenharmony_ci    subscriber = commonEventSubscriber;
406e41f4b71Sopenharmony_ci    // 订阅公共事件
407e41f4b71Sopenharmony_ci    try {
408e41f4b71Sopenharmony_ci      commonEventManager.subscribe(subscriber, subscribeCB);
409e41f4b71Sopenharmony_ci    } catch (error) {
410e41f4b71Sopenharmony_ci      let err: BusinessError = error as BusinessError;
411e41f4b71Sopenharmony_ci      console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
412e41f4b71Sopenharmony_ci    }
413e41f4b71Sopenharmony_ci  }
414e41f4b71Sopenharmony_ci}
415e41f4b71Sopenharmony_ci// 取消订阅公共事件回调
416e41f4b71Sopenharmony_cifunction unsubscribeCB(err: BusinessError) {
417e41f4b71Sopenharmony_ci  if (err) {
418e41f4b71Sopenharmony_ci    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
419e41f4b71Sopenharmony_ci  } else {
420e41f4b71Sopenharmony_ci    console.info("unsubscribe success");
421e41f4b71Sopenharmony_ci  }
422e41f4b71Sopenharmony_ci}
423e41f4b71Sopenharmony_ci// 创建订阅者
424e41f4b71Sopenharmony_citry {
425e41f4b71Sopenharmony_ci  commonEventManager.createSubscriber(subscribeInfo, createCB);
426e41f4b71Sopenharmony_ci} catch (error) {
427e41f4b71Sopenharmony_ci  let err: BusinessError = error as BusinessError;
428e41f4b71Sopenharmony_ci  console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
429e41f4b71Sopenharmony_ci}
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ci// 取消订阅公共事件
432e41f4b71Sopenharmony_ci// 等待异步接口subscribe执行完毕,开发者根据实际业务选择是否需要添加setTimeout
433e41f4b71Sopenharmony_cisetTimeout(() => {
434e41f4b71Sopenharmony_ci  try {
435e41f4b71Sopenharmony_ci    commonEventManager.unsubscribe(subscriber, unsubscribeCB);
436e41f4b71Sopenharmony_ci  } catch (error) {
437e41f4b71Sopenharmony_ci    let err: BusinessError = error as BusinessError;
438e41f4b71Sopenharmony_ci    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
439e41f4b71Sopenharmony_ci  }
440e41f4b71Sopenharmony_ci}, 500);
441e41f4b71Sopenharmony_ci```
442