1e41f4b71Sopenharmony_ci# @ohos.commonEvent (Common Event) 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## Modules to Import 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport CommonEvent from '@ohos.commonEvent'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Support 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_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. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciFor details about the definitions of all system common events, see [System Common Events](./common_event/commonEvent-definitions.md). 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## CommonEvent.publish<sup>(deprecated)</sup> 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_cipublish(event: string, callback: AsyncCallback\<void>): void 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciPublishes a common event. This API uses an asynchronous callback to return the result. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci> **NOTE**<br> 29e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish) instead. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**Parameters** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 36e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------- | 37e41f4b71Sopenharmony_ci| event | string | Yes | Name of the common event to publish.| 38e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**Example** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci```ts 43e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci// Callback for common event publication 46e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) { 47e41f4b71Sopenharmony_ci if (err.code) { 48e41f4b71Sopenharmony_ci console.error(`publish failed, code is ${err.code}`); 49e41f4b71Sopenharmony_ci } else { 50e41f4b71Sopenharmony_ci console.info("publish"); 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci} 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci// Publish a common event. 55e41f4b71Sopenharmony_ciCommonEvent.publish("event", publishCB); 56e41f4b71Sopenharmony_ci``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci## CommonEvent.publish<sup>(deprecated)</sup> 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_cipublish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ciPublishes a common event with given attributes. This API uses an asynchronous callback to return the result. 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci> **NOTE**<br> 65e41f4b71Sopenharmony_ci> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish-1) instead. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**Parameters** 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 72e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ---------------------- | 73e41f4b71Sopenharmony_ci| event | string | Yes | Name of the common event to publish. | 74e41f4b71Sopenharmony_ci| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | Yes | Attributes of the common event to publish.| 75e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**Example** 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci```ts 81e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 82e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci// Attributes of a common event. 85e41f4b71Sopenharmony_cilet options:CommonEventManager.CommonEventPublishData = { 86e41f4b71Sopenharmony_ci code: 0, // Result code of the common event. 87e41f4b71Sopenharmony_ci data: "initial data";// Result data of the common event. 88e41f4b71Sopenharmony_ci isOrdered: true // The common event is an ordered one. 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci// Callback for common event publication 92e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) { 93e41f4b71Sopenharmony_ci if (err.code) { 94e41f4b71Sopenharmony_ci console.error(`publish failed, code is ${err.code}`); 95e41f4b71Sopenharmony_ci } else { 96e41f4b71Sopenharmony_ci console.info("publish"); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci} 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci// Publish a common event. 101e41f4b71Sopenharmony_ciCommonEvent.publish("event", options, publishCB); 102e41f4b71Sopenharmony_ci``` 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci## CommonEvent.createSubscriber<sup>(deprecated)</sup> 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_cicreateSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciCreates a subscriber. This API uses an asynchronous callback to return the result. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci> **NOTE**<br> 111e41f4b71Sopenharmony_ci>This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber) instead. 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**Parameters** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 118e41f4b71Sopenharmony_ci| ------------- | ------------------------------------------------------------ | ---- | -------------------------- | 119e41f4b71Sopenharmony_ci| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Yes | Subscriber information. | 120e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Yes | Callback used to return the result.| 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**Example** 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci```ts 126e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 127e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci// Subscriber information. 132e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 133e41f4b71Sopenharmony_ci events: ["event"] 134e41f4b71Sopenharmony_ci}; 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci// Callback for subscriber creation. 137e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 138e41f4b71Sopenharmony_ci if (err.code) { 139e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 140e41f4b71Sopenharmony_ci } else { 141e41f4b71Sopenharmony_ci console.info("createSubscriber"); 142e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci} 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci// Create a subscriber. 147e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB); 148e41f4b71Sopenharmony_ci``` 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci## CommonEvent.createSubscriber<sup>(deprecated)</sup> 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_cicreateSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber> 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ciCreates a subscriber. This API uses a promise to return the result. 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci> **NOTE**<br> 157e41f4b71Sopenharmony_ci>This API is supported since API version 7 and deprecated since API version 9. You are advised to use [commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber-1) instead. 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci**Parameters** 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 164e41f4b71Sopenharmony_ci| ------------- | ----------------------------------------------------- | ---- | -------------- | 165e41f4b71Sopenharmony_ci| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Yes | Subscriber information.| 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci**Return value** 168e41f4b71Sopenharmony_ci| Type | Description | 169e41f4b71Sopenharmony_ci| --------------------------------------------------------- | ---------------- | 170e41f4b71Sopenharmony_ci| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Promise used to return the subscriber object.| 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**Example** 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci```ts 175e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 176e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci// Subscriber information. 181e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 182e41f4b71Sopenharmony_ci events: ["event"] 183e41f4b71Sopenharmony_ci}; 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci// Create a subscriber. 186e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => { 187e41f4b71Sopenharmony_ci console.info("createSubscriber"); 188e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 189e41f4b71Sopenharmony_ci}).catch((err:Base.BusinessError) => { 190e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 191e41f4b71Sopenharmony_ci}); 192e41f4b71Sopenharmony_ci``` 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci## CommonEvent.subscribe<sup>(deprecated)</sup> 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_cisubscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ciSubscribes to common events. This API uses an asynchronous callback to return the result. 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci> **NOTE**<br> 201e41f4b71Sopenharmony_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#commoneventmanagersubscribe) instead. 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**Parameters** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 208e41f4b71Sopenharmony_ci| ---------- | ---------------------------------------------------- | ---- | -------------------------------- | 209e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Yes | Subscriber object. | 210e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | Yes | Callback used to return the result.| 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci**Example** 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci```ts 215e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 216e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber;// Used to save the created subscriber object for subsequent subscription and unsubscription. 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci// Subscriber information. 221e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 222e41f4b71Sopenharmony_ci events: ["event"] 223e41f4b71Sopenharmony_ci}; 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci// Callback for common event subscription. 226e41f4b71Sopenharmony_cifunction subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) { 227e41f4b71Sopenharmony_ci if (err.code) { 228e41f4b71Sopenharmony_ci console.error(`subscribe failed, code is ${err.code}`); 229e41f4b71Sopenharmony_ci } else { 230e41f4b71Sopenharmony_ci console.info("subscribe " + JSON.stringify(data)); 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci} 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci// Callback for subscriber creation. 235e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 236e41f4b71Sopenharmony_ci if (err.code) { 237e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 238e41f4b71Sopenharmony_ci } else { 239e41f4b71Sopenharmony_ci console.info("createSubscriber"); 240e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 241e41f4b71Sopenharmony_ci // Subscribe to a common event. 242e41f4b71Sopenharmony_ci CommonEvent.subscribe(subscriber, subscribeCB); 243e41f4b71Sopenharmony_ci } 244e41f4b71Sopenharmony_ci} 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci// Create a subscriber. 247e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB); 248e41f4b71Sopenharmony_ci``` 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci## CommonEvent.unsubscribe<sup>(deprecated)</sup> 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ciunsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ciUnsubscribes from common events. This API uses an asynchronous callback to return the result. 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci> **NOTE**<br> 257e41f4b71Sopenharmony_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. 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**Parameters** 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 264e41f4b71Sopenharmony_ci| ---------- | ----------------------------------------------- | ---- | ------------------------ | 265e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Yes | Subscriber object. | 266e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | No | Callback used to return the result.| 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci**Example** 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci```ts 271e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 272e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci// Subscriber information. 277e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 278e41f4b71Sopenharmony_ci events: ["event"] 279e41f4b71Sopenharmony_ci}; 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci// Callback for common event subscription. 282e41f4b71Sopenharmony_cifunction subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) { 283e41f4b71Sopenharmony_ci if (err.code) { 284e41f4b71Sopenharmony_ci console.error(`subscribe failed, code is ${err.code}`); 285e41f4b71Sopenharmony_ci } else { 286e41f4b71Sopenharmony_ci console.info("subscribe " + JSON.stringify(data)); 287e41f4b71Sopenharmony_ci } 288e41f4b71Sopenharmony_ci} 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci// Callback for subscriber creation. 291e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 292e41f4b71Sopenharmony_ci if (err.code) { 293e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 294e41f4b71Sopenharmony_ci } else { 295e41f4b71Sopenharmony_ci console.info("createSubscriber"); 296e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 297e41f4b71Sopenharmony_ci // Subscribe to a common event. 298e41f4b71Sopenharmony_ci CommonEvent.subscribe(subscriber, subscribeCB); 299e41f4b71Sopenharmony_ci } 300e41f4b71Sopenharmony_ci} 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci// Callback for common event unsubscription. 303e41f4b71Sopenharmony_cifunction unsubscribeCB(err:Base.BusinessError) { 304e41f4b71Sopenharmony_ci if (err.code) { 305e41f4b71Sopenharmony_ci console.error(`unsubscribe failed, code is ${err.code}`); 306e41f4b71Sopenharmony_ci } else { 307e41f4b71Sopenharmony_ci console.info("unsubscribe"); 308e41f4b71Sopenharmony_ci } 309e41f4b71Sopenharmony_ci} 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci// Create a subscriber. 312e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB); 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci// Unsubscribe from the common event. 315e41f4b71Sopenharmony_ciCommonEvent.unsubscribe(subscriber, unsubscribeCB); 316e41f4b71Sopenharmony_ci``` 317