1# @ohos.commonEvent (Common Event) 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> 7> - 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). 8> 9> - 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. 10 11## Modules to Import 12 13```ts 14import CommonEvent from '@ohos.commonEvent'; 15``` 16 17## Support 18 19A 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. 20 21For details about the definitions of all system common events, see [System Common Events](./commonEvent-definitions.md). 22 23## CommonEvent.publish<sup>(deprecated)</sup> 24 25publish(event: string, callback: AsyncCallback\<void>): void 26 27Publishes a common event. This API uses an asynchronous callback to return the result. 28 29> **NOTE** 30> 31> 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. 32 33**System capability**: SystemCapability.Notification.CommonEvent 34 35**Parameters** 36 37| Name | Type | Mandatory| Description | 38| -------- | -------------------- | ---- | ---------------------- | 39| event | string | Yes | Name of the common event to publish.| 40| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 41 42**Example** 43 44```ts 45import Base from '@ohos.base'; 46 47// Callback for common event publication 48function publishCB(err:Base.BusinessError) { 49 if (err.code) { 50 console.error(`publish failed, code is ${err.code}`); 51 } else { 52 console.info("publish"); 53 } 54} 55 56// Publish a common event. 57CommonEvent.publish("event", publishCB); 58``` 59 60## CommonEvent.publish<sup>(deprecated)</sup> 61 62publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 63 64Publishes a common event with given attributes. This API uses an asynchronous callback to return the result. 65 66> **NOTE** 67> 68> 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. 69 70**System capability**: SystemCapability.Notification.CommonEvent 71 72**Parameters** 73 74| Name | Type | Mandatory| Description | 75| -------- | ---------------------- | ---- | ---------------------- | 76| event | string | Yes | Name of the common event to publish. | 77| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | Yes | Attributes of the common event to publish.| 78| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 79 80**Example** 81 82 83```ts 84import Base from '@ohos.base'; 85import CommonEventManager from '@ohos.commonEventManager'; 86 87// Attributes of a common event. 88let options:CommonEventManager.CommonEventPublishData = { 89 code: 0, // Result code of the common event. 90 data: "initial data";// Result data of the common event. 91 isOrdered: true // The common event is an ordered one. 92} 93 94// Callback for common event publication 95function publishCB(err:Base.BusinessError) { 96 if (err.code) { 97 console.error(`publish failed, code is ${err.code}`); 98 } else { 99 console.info("publish"); 100 } 101} 102 103// Publish a common event. 104CommonEvent.publish("event", options, publishCB); 105``` 106 107## CommonEvent.publishAsUser<sup>(deprecated)</sup> 108 109publishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void 110 111Publishes a common event to a specific user. This API uses an asynchronous callback to return the result. 112 113> **NOTE** 114> 115> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [commonEventManager.publishAsUser](js-apis-commonEventManager.md#commoneventmanagerpublishasuser) instead. 116 117**System capability**: SystemCapability.Notification.CommonEvent 118 119**System API**: This is a system API and cannot be called by third-party applications. 120 121**Parameters** 122 123| Name | Type | Mandatory| Description | 124| -------- | -------------------- | ---- | ---------------------------------- | 125| event | string | Yes | Name of the common event to publish. | 126| userId | number | Yes | User ID.| 127| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 128 129**Example** 130 131```ts 132import Base from '@ohos.base'; 133 134// Callback for common event publication 135function publishCB(err:Base.BusinessError) { 136 if (err.code) { 137 console.error(`publishAsUser failed, code is ${err.code}`); 138 } else { 139 console.info("publishAsUser"); 140 } 141} 142 143// Specify the user to whom the common event will be published. 144let userId = 100; 145 146// Publish a common event. 147CommonEvent.publishAsUser("event", userId, publishCB); 148``` 149 150## CommonEvent.publishAsUser<sup>(deprecated)</sup> 151 152publishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 153 154Publishes a common event with given attributes to a specific user. This API uses an asynchronous callback to return the result. 155 156> **NOTE** 157> 158> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [commonEventManager.publishAsUser](js-apis-commonEventManager.md#commoneventmanagerpublishasuser-1) instead. 159 160**System capability**: SystemCapability.Notification.CommonEvent 161 162**System API**: This is a system API and cannot be called by third-party applications. 163 164**Parameters** 165 166| Name | Type | Mandatory| Description | 167| -------- | ---------------------- | ---- | ---------------------- | 168| event | string | Yes | Name of the common event to publish. | 169| userId | number | Yes| User ID.| 170| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | Yes | Attributes of the common event to publish.| 171| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 172 173**Example** 174 175 176```ts 177import Base from '@ohos.base'; 178import CommonEventManager from '@ohos.commonEventManager'; 179 180// Attributes of a common event. 181let options:CommonEventManager.CommonEventPublishData = { 182 code: 0, // Result code of the common event. 183 data: "initial data",// Result data of the common event. 184} 185 186// Callback for common event publication 187function publishCB(err:Base.BusinessError) { 188 if (err.code) { 189 console.error(`publishAsUser failed, code is ${err.code}`); 190 } else { 191 console.info("publishAsUser"); 192 } 193} 194 195// Specify the user to whom the common event will be published. 196let userId = 100; 197 198// Publish a common event. 199CommonEvent.publishAsUser("event", userId, options, publishCB); 200``` 201 202## CommonEvent.createSubscriber<sup>(deprecated)</sup> 203 204createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void 205 206Creates a subscriber. This API uses an asynchronous callback to return the result. 207 208> **NOTE** 209> 210> 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. 211 212**System capability**: SystemCapability.Notification.CommonEvent 213 214**Parameters** 215 216| Name | Type | Mandatory| Description | 217| ------------- | ------------------------------------------------------------ | ---- | -------------------------- | 218| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Yes | Subscriber information. | 219| callback | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Yes | Callback used to return the result.| 220 221**Example** 222 223 224```ts 225import Base from '@ohos.base'; 226import CommonEventManager from '@ohos.commonEventManager'; 227 228let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 229 230// Subscriber information. 231let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 232 events: ["event"] 233}; 234 235// Callback for subscriber creation. 236function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 237 if (err.code) { 238 console.error(`createSubscriber failed, code is ${err.code}`); 239 } else { 240 console.info("createSubscriber"); 241 subscriber = commonEventSubscriber; 242 } 243} 244 245// Create a subscriber. 246CommonEvent.createSubscriber(subscribeInfo, createCB); 247``` 248 249## CommonEvent.createSubscriber<sup>(deprecated)</sup> 250 251createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber> 252 253Creates a subscriber. This API uses a promise to return the result. 254 255> **NOTE** 256> 257>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. 258 259**System capability**: SystemCapability.Notification.CommonEvent 260 261**Parameters** 262 263| Name | Type | Mandatory| Description | 264| ------------- | ----------------------------------------------------- | ---- | -------------- | 265| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Yes | Subscriber information.| 266 267**Return value** 268| Type | Description | 269| --------------------------------------------------------- | ---------------- | 270| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | Promise used to return the subscriber object.| 271 272**Example** 273 274```ts 275import Base from '@ohos.base'; 276import CommonEventManager from '@ohos.commonEventManager'; 277 278let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 279 280// Subscriber information. 281let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 282 events: ["event"] 283}; 284 285// Create a subscriber. 286CommonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => { 287 console.info("createSubscriber"); 288 subscriber = commonEventSubscriber; 289}).catch((err:Base.BusinessError) => { 290 console.error(`createSubscriber failed, code is ${err.code}`); 291}); 292``` 293 294## CommonEvent.subscribe<sup>(deprecated)</sup> 295 296subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void 297 298Subscribes to common events. This API uses an asynchronous callback to return the result. 299 300> **NOTE** 301> 302> 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. 303 304**System capability**: SystemCapability.Notification.CommonEvent 305 306**Parameters** 307 308| Name | Type | Mandatory| Description | 309| ---------- | ---------------------------------------------------- | ---- | -------------------------------- | 310| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Yes | Subscriber object. | 311| callback | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | Yes | Callback used to return the result.| 312 313**Example** 314 315```ts 316import Base from '@ohos.base'; 317import CommonEventManager from '@ohos.commonEventManager'; 318 319let subscriber:CommonEventManager.CommonEventSubscriber;// Used to save the created subscriber object for subsequent subscription and unsubscription. 320 321// Subscriber information. 322let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 323 events: ["event"] 324}; 325 326// Callback for common event subscription. 327function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) { 328 if (err.code) { 329 console.error(`subscribe failed, code is ${err.code}`); 330 } else { 331 console.info("subscribe " + JSON.stringify(data)); 332 } 333} 334 335// Callback for subscriber creation. 336function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 337 if (err.code) { 338 console.error(`createSubscriber failed, code is ${err.code}`); 339 } else { 340 console.info("createSubscriber"); 341 subscriber = commonEventSubscriber; 342 // Subscribe to a common event. 343 CommonEvent.subscribe(subscriber, subscribeCB); 344 } 345} 346 347// Create a subscriber. 348CommonEvent.createSubscriber(subscribeInfo, createCB); 349``` 350 351## CommonEvent.unsubscribe<sup>(deprecated)</sup> 352 353unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void 354 355Unsubscribes from common events. This API uses an asynchronous callback to return the result. 356 357> **NOTE** 358> 359> 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. 360 361**System capability**: SystemCapability.Notification.CommonEvent 362 363**Parameters** 364 365| Name | Type | Mandatory| Description | 366| ---------- | ----------------------------------------------- | ---- | ------------------------ | 367| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | Yes | Subscriber object. | 368| callback | AsyncCallback\<void> | No | Callback used to return the result.| 369 370**Example** 371 372```ts 373import Base from '@ohos.base'; 374import CommonEventManager from '@ohos.commonEventManager'; 375 376let subscriber:CommonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 377 378// Subscriber information. 379let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 380 events: ["event"] 381}; 382 383// Callback for common event subscription. 384function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) { 385 if (err.code) { 386 console.error(`subscribe failed, code is ${err.code}`); 387 } else { 388 console.info("subscribe " + JSON.stringify(data)); 389 } 390} 391 392// Callback for subscriber creation. 393function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 394 if (err.code) { 395 console.error(`createSubscriber failed, code is ${err.code}`); 396 } else { 397 console.info("createSubscriber"); 398 subscriber = commonEventSubscriber; 399 // Subscribe to a common event. 400 CommonEvent.subscribe(subscriber, subscribeCB); 401 } 402} 403 404// Callback for common event unsubscription. 405function unsubscribeCB(err:Base.BusinessError) { 406 if (err.code) { 407 console.error(`unsubscribe failed, code is ${err.code}`); 408 } else { 409 console.info("unsubscribe"); 410 } 411} 412 413// Create a subscriber. 414CommonEvent.createSubscriber(subscribeInfo, createCB); 415 416// Unsubscribe from the common event. 417CommonEvent.unsubscribe(subscriber, unsubscribeCB); 418``` 419