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