1e41f4b71Sopenharmony_ci# CommonEventSubscriber 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **CommonEventSubscriber** module provides APIs for describing the common event subscriber. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Usage 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciBefore using the **CommonEventSubscriber** module, you must obtain a **subscriber** object by calling **commonEventManager.createSubscriber**. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { commonEventManager } from '@kit.BasicServicesKit'; 15e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 16e41f4b71Sopenharmony_cilet subscriber: commonEventManager.CommonEventSubscriber; // Used to save the created subscriber object for subsequent subscription and unsubscription. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci// Subscriber information. 19e41f4b71Sopenharmony_cilet subscribeInfo:commonEventManager.CommonEventSubscribeInfo = { 20e41f4b71Sopenharmony_ci events: ["event"] 21e41f4b71Sopenharmony_ci}; 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci// Callback for subscriber creation. 24e41f4b71Sopenharmony_cifunction createCB(err: BusinessError, commonEventSubscriber:commonEventManager.CommonEventSubscriber) { 25e41f4b71Sopenharmony_ci if (err != null) { 26e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 27e41f4b71Sopenharmony_ci } else { 28e41f4b71Sopenharmony_ci console.info("createSubscriber success"); 29e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 30e41f4b71Sopenharmony_ci } 31e41f4b71Sopenharmony_ci} 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci// Create a subscriber. 34e41f4b71Sopenharmony_cicommonEventManager.createSubscriber(subscribeInfo, createCB); 35e41f4b71Sopenharmony_ci``` 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci## getCode 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_cigetCode(callback: AsyncCallback\<number>): void 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ciObtains the result code of an ordered common event. This API uses an asynchronous callback to return the result. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Parameters** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 50e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------------ | 51e41f4b71Sopenharmony_ci| callback | AsyncCallback\<number\> | Yes | Common event code. | 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**Example** 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci```ts 56e41f4b71Sopenharmony_ci// Callback for result code obtaining of an ordered common event. 57e41f4b71Sopenharmony_cifunction getCodeCallback(err: BusinessError, code:number) { 58e41f4b71Sopenharmony_ci if (err != null) { 59e41f4b71Sopenharmony_ci console.error(`getCode failed, code is ${err.code}, message is ${err.message}`); 60e41f4b71Sopenharmony_ci } else { 61e41f4b71Sopenharmony_ci console.info("getCode " + JSON.stringify(code)); 62e41f4b71Sopenharmony_ci } 63e41f4b71Sopenharmony_ci} 64e41f4b71Sopenharmony_cisubscriber.getCode(getCodeCallback); 65e41f4b71Sopenharmony_ci``` 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci## getCode 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_cigetCode(): Promise\<number> 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ciObtains the result code of an ordered common event. This API uses a promise to return the result. 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**Return value** 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci| Type | Description | 80e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 81e41f4b71Sopenharmony_ci| Promise\<number> | Common event code. | 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**Example** 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci```ts 86e41f4b71Sopenharmony_cisubscriber.getCode().then((code:number) => { 87e41f4b71Sopenharmony_ci console.info("getCode " + JSON.stringify(code)); 88e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 89e41f4b71Sopenharmony_ci console.error(`getCode failed, code is ${err.code}, message is ${err.message}`); 90e41f4b71Sopenharmony_ci}); 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci## getCodeSync<sup>10+</sup> 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_cigetCodeSync(): number 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ciObtains the result code of an ordered common event. 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**Return value** 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| Type | Description | 106e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 107e41f4b71Sopenharmony_ci| number | Common event code. | 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**Example** 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci```ts 112e41f4b71Sopenharmony_cilet code = subscriber.getCodeSync(); 113e41f4b71Sopenharmony_ciconsole.info("getCodeSync " + JSON.stringify(code)); 114e41f4b71Sopenharmony_ci``` 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci## setCode 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_cisetCode(code: number, callback: AsyncCallback\<void>): void 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciSets the result code of an ordered common event. This API uses an asynchronous callback to return the result. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**Parameters** 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 129e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------- | 130e41f4b71Sopenharmony_ci| code | number | Yes | Common event code. | 131e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**Example** 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci```ts 136e41f4b71Sopenharmony_ci// Callback for result code setting of an ordered common event. 137e41f4b71Sopenharmony_cifunction setCodeCallback(err: BusinessError) { 138e41f4b71Sopenharmony_ci if (err != null) { 139e41f4b71Sopenharmony_ci console.error(`setCode failed, code is ${err.code}, message is ${err.message}`); 140e41f4b71Sopenharmony_ci } else { 141e41f4b71Sopenharmony_ci console.info("setCode success"); 142e41f4b71Sopenharmony_ci } 143e41f4b71Sopenharmony_ci} 144e41f4b71Sopenharmony_cisubscriber.setCode(1, setCodeCallback); 145e41f4b71Sopenharmony_ci``` 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci## setCode 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_cisetCode(code: number): Promise\<void> 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ciSets the result code of an ordered common event. This API uses a promise to return the result. 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**Parameters** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 160e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------ | 161e41f4b71Sopenharmony_ci| code | number | Yes | Common event code. | 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci**Return value** 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci| Type | Description | 166e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 167e41f4b71Sopenharmony_ci| Promise\<void> | Promise used to return the result. | 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**Example** 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci```ts 172e41f4b71Sopenharmony_cisubscriber.setCode(1).then(() => { 173e41f4b71Sopenharmony_ci console.info("setCode success"); 174e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 175e41f4b71Sopenharmony_ci console.error(`setCode failed, code is ${err.code}, message is ${err.message}`); 176e41f4b71Sopenharmony_ci}); 177e41f4b71Sopenharmony_ci``` 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci## setCodeSync<sup>10+</sup> 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_cisetCodeSync(code: number): void 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciSets the result code of an ordered common event. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**Parameters** 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 192e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------ | 193e41f4b71Sopenharmony_ci| code | number | Yes | Common event code. | 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**Error codes** 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci| ID | Error Message | 200e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 201e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**Example** 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci```ts 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_citry { 208e41f4b71Sopenharmony_ci subscriber.setCodeSync(1); 209e41f4b71Sopenharmony_ci} catch (error) { 210e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 211e41f4b71Sopenharmony_ci console.error(`setCodeSync failed, code is ${err.code}, message is ${err.message}`); 212e41f4b71Sopenharmony_ci} 213e41f4b71Sopenharmony_ci``` 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci## getData 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_cigetData(callback: AsyncCallback\<string>): void 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ciObtains the result data of an ordered common event. This API uses an asynchronous callback to return the result. 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**Parameters** 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 228e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | -------------------- | 229e41f4b71Sopenharmony_ci| callback | AsyncCallback\<string> | Yes | Common event data. | 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci**Example** 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci```ts 234e41f4b71Sopenharmony_ci// Callback for result data obtaining of an ordered common event. 235e41f4b71Sopenharmony_cifunction getDataCallback(err: BusinessError, data:string) { 236e41f4b71Sopenharmony_ci if (err != null) { 237e41f4b71Sopenharmony_ci console.error(`getData failed, code is ${err.code}, message is ${err.message}`); 238e41f4b71Sopenharmony_ci } else { 239e41f4b71Sopenharmony_ci console.info("getData " + JSON.stringify(data)); 240e41f4b71Sopenharmony_ci } 241e41f4b71Sopenharmony_ci} 242e41f4b71Sopenharmony_cisubscriber.getData(getDataCallback); 243e41f4b71Sopenharmony_ci``` 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci## getData 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_cigetData(): Promise\<string> 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ciObtains the result data of an ordered common event. This API uses a promise to return the result. 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**Return value** 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| Type | Description | 258e41f4b71Sopenharmony_ci| ---------------- | ------------------ | 259e41f4b71Sopenharmony_ci| Promise\<string> | Common event data. | 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**Example** 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci```ts 264e41f4b71Sopenharmony_cisubscriber.getData().then((data:string) => { 265e41f4b71Sopenharmony_ci console.info("getData " + JSON.stringify(data)); 266e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 267e41f4b71Sopenharmony_ci console.error(`getData failed, code is ${err.code}, message is ${err.message}`); 268e41f4b71Sopenharmony_ci}); 269e41f4b71Sopenharmony_ci``` 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci## getDataSync<sup>10+</sup> 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_cigetDataSync(): string 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ciObtains the result data of an ordered common event. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci**Return value** 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci| Type | Description | 284e41f4b71Sopenharmony_ci| ---------------- | ------------------ | 285e41f4b71Sopenharmony_ci| string | Common event data. | 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci**Example** 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci```ts 290e41f4b71Sopenharmony_cilet data = subscriber.getDataSync(); 291e41f4b71Sopenharmony_ciconsole.info("getDataSync " + JSON.stringify(data)); 292e41f4b71Sopenharmony_ci``` 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci## setData 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_cisetData(data: string, callback: AsyncCallback\<void>): void 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ciSets the result data for an ordered common event. This API uses an asynchronous callback to return the result. 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci**Parameters** 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 307e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | -------------------- | 308e41f4b71Sopenharmony_ci| data | string | Yes | Common event data. | 309e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci**Example** 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci```ts 314e41f4b71Sopenharmony_ci// Callback for result data setting of an ordered common event 315e41f4b71Sopenharmony_cifunction setDataCallback(err: BusinessError) { 316e41f4b71Sopenharmony_ci if (err != null) { 317e41f4b71Sopenharmony_ci console.error(`setData failed, code is ${err.code}, message is ${err.message}`); 318e41f4b71Sopenharmony_ci } else { 319e41f4b71Sopenharmony_ci console.info("setData success"); 320e41f4b71Sopenharmony_ci } 321e41f4b71Sopenharmony_ci} 322e41f4b71Sopenharmony_cisubscriber.setData("publish_data_changed", setDataCallback); 323e41f4b71Sopenharmony_ci``` 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci## setData 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_cisetData(data: string): Promise\<void> 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ciSets the result data for an ordered common event. This API uses a promise to return the result. 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci**Parameters** 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 338e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------- | 339e41f4b71Sopenharmony_ci| data | string | Yes | Common event data. | 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci**Return value** 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci| Type | Description | 344e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 345e41f4b71Sopenharmony_ci| Promise\<void> | Promise used to return the result. | 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**Example** 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci```ts 350e41f4b71Sopenharmony_cisubscriber.setData("publish_data_changed").then(() => { 351e41f4b71Sopenharmony_ci console.info("setData success"); 352e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 353e41f4b71Sopenharmony_ci console.error(`setData failed, code is ${err.code}, message is ${err.message}`); 354e41f4b71Sopenharmony_ci}); 355e41f4b71Sopenharmony_ci``` 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci## setDataSync<sup>10+</sup> 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_cisetDataSync(data: string): void 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ciSets the result data for an ordered common event. 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci**Parameters** 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 370e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------- | 371e41f4b71Sopenharmony_ci| data | string | Yes | Common event data. | 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci**Error codes** 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci| ID | Error Message | 378e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 379e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**Example** 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci```ts 384e41f4b71Sopenharmony_citry { 385e41f4b71Sopenharmony_ci subscriber.setDataSync("publish_data_changed"); 386e41f4b71Sopenharmony_ci} catch (error) { 387e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 388e41f4b71Sopenharmony_ci console.error(`setDataSync failed, code is ${err.code}, message is ${err.message}`); 389e41f4b71Sopenharmony_ci} 390e41f4b71Sopenharmony_ci``` 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci## setCodeAndData 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_cisetCodeAndData(code: number, data: string, callback:AsyncCallback\<void>): void 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ciSets the result code and data of an ordered common event. This API uses an asynchronous callback to return the result. 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci**Parameters** 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 405e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------- | 406e41f4b71Sopenharmony_ci| code | number | Yes | Common event code. | 407e41f4b71Sopenharmony_ci| data | string | Yes | Common event data. | 408e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci**Example** 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci```ts 413e41f4b71Sopenharmony_ci// Callback for code and data setting of an ordered common event. 414e41f4b71Sopenharmony_cifunction setCodeAndDataCallback(err: BusinessError) { 415e41f4b71Sopenharmony_ci if (err != null) { 416e41f4b71Sopenharmony_ci console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`); 417e41f4b71Sopenharmony_ci } else { 418e41f4b71Sopenharmony_ci console.info("setCodeAndData success"); 419e41f4b71Sopenharmony_ci } 420e41f4b71Sopenharmony_ci} 421e41f4b71Sopenharmony_cisubscriber.setCodeAndData(1, "publish_data_changed", setCodeAndDataCallback); 422e41f4b71Sopenharmony_ci``` 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ci## setCodeAndData 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_cisetCodeAndData(code: number, data: string): Promise\<void> 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ciSets the result code and data of an ordered common event. This API uses a promise to return the result. 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci**Parameters** 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 437e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------- | 438e41f4b71Sopenharmony_ci| code | number | Yes | Common event code. | 439e41f4b71Sopenharmony_ci| data | string | Yes | Common event data. | 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci**Return value** 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci| Type | Description | 444e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 445e41f4b71Sopenharmony_ci| Promise\<void> | Promise used to return the result. | 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci**Example** 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci```ts 450e41f4b71Sopenharmony_cisubscriber.setCodeAndData(1, "publish_data_changed").then(() => { 451e41f4b71Sopenharmony_ci console.info("setCodeAndData success"); 452e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 453e41f4b71Sopenharmony_ci console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`); 454e41f4b71Sopenharmony_ci}); 455e41f4b71Sopenharmony_ci``` 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci## setCodeAndDataSync<sup>10+</sup> 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_cisetCodeAndDataSync(code: number, data: string): void 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ciSets the result code and data of an ordered common event. 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**Parameters** 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 470e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------- | 471e41f4b71Sopenharmony_ci| code | number | Yes | Common event code. | 472e41f4b71Sopenharmony_ci| data | string | Yes | Common event data. | 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci**Error codes** 475e41f4b71Sopenharmony_ci 476e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 477e41f4b71Sopenharmony_ci 478e41f4b71Sopenharmony_ci| ID | Error Message | 479e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 480e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci**Example** 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci```ts 485e41f4b71Sopenharmony_citry { 486e41f4b71Sopenharmony_ci subscriber.setCodeAndDataSync(1, "publish_data_changed"); 487e41f4b71Sopenharmony_ci} catch (error) { 488e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 489e41f4b71Sopenharmony_ci console.error(`setCodeAndDataSync failed, code is ${err.code}, message is ${err.message}`); 490e41f4b71Sopenharmony_ci} 491e41f4b71Sopenharmony_ci 492e41f4b71Sopenharmony_ci``` 493e41f4b71Sopenharmony_ci 494e41f4b71Sopenharmony_ci## isOrderedCommonEvent 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ciisOrderedCommonEvent(callback: AsyncCallback\<boolean>): void 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ciChecks whether the current common event is an ordered common event. This API uses an asynchronous callback to return the result. 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ciReturns **true** if the common event is an ordered one; returns **false** otherwise. 501e41f4b71Sopenharmony_ci 502e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci**Parameters** 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 507e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ---------------------------------- | 508e41f4b71Sopenharmony_ci| callback | AsyncCallback\<boolean> | Yes | Whether the current common event is an ordered one. | 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_ci**Example** 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci```ts 513e41f4b71Sopenharmony_ci// Callback for checking whether the current common event is an ordered one. 514e41f4b71Sopenharmony_cifunction isOrderedCommonEventCallback(err: BusinessError, isOrdered:boolean) { 515e41f4b71Sopenharmony_ci if (err != null) { 516e41f4b71Sopenharmony_ci console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`); 517e41f4b71Sopenharmony_ci } else { 518e41f4b71Sopenharmony_ci console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered)); 519e41f4b71Sopenharmony_ci } 520e41f4b71Sopenharmony_ci} 521e41f4b71Sopenharmony_cisubscriber.isOrderedCommonEvent(isOrderedCommonEventCallback); 522e41f4b71Sopenharmony_ci``` 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci## isOrderedCommonEvent 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ciisOrderedCommonEvent(): Promise\<boolean> 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ciChecks whether the current common event is an ordered common event. This API uses a promise to return the result. 529e41f4b71Sopenharmony_ci 530e41f4b71Sopenharmony_ciReturns **true** if the common event is an ordered one; returns **false** otherwise. 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 533e41f4b71Sopenharmony_ci 534e41f4b71Sopenharmony_ci**Return value** 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ci| Type | Description | 537e41f4b71Sopenharmony_ci| ----------------- | -------------------------------- | 538e41f4b71Sopenharmony_ci| Promise\<boolean> | Whether the current common event is an ordered one. | 539e41f4b71Sopenharmony_ci 540e41f4b71Sopenharmony_ci**Example** 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci```ts 543e41f4b71Sopenharmony_cisubscriber.isOrderedCommonEvent().then((isOrdered:boolean) => { 544e41f4b71Sopenharmony_ci console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered)); 545e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 546e41f4b71Sopenharmony_ci console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`); 547e41f4b71Sopenharmony_ci}); 548e41f4b71Sopenharmony_ci``` 549e41f4b71Sopenharmony_ci 550e41f4b71Sopenharmony_ci## isOrderedCommonEventSync<sup>10+</sup> 551e41f4b71Sopenharmony_ci 552e41f4b71Sopenharmony_ciisOrderedCommonEventSync(): boolean 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ciChecks whether the current common event is an ordered common event. 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ciReturns **true** if the common event is an ordered one; returns **false** otherwise. 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci**Return value** 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci| Type | Description | 563e41f4b71Sopenharmony_ci| ----------------- | -------------------------------- | 564e41f4b71Sopenharmony_ci| boolean | Whether the current common event is an ordered one. | 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_ci**Example** 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci```ts 569e41f4b71Sopenharmony_cilet isOrdered = subscriber.isOrderedCommonEventSync(); 570e41f4b71Sopenharmony_ciconsole.info("isOrderedCommonEventSync " + JSON.stringify(isOrdered)); 571e41f4b71Sopenharmony_ci``` 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci## isStickyCommonEvent 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ciisStickyCommonEvent(callback: AsyncCallback\<boolean>): void 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ciChecks whether a common event is a sticky one. This API uses an asynchronous callback to return the result. 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ciReturns **true** if the common event is a sticky one; returns **false** otherwise. 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci**Parameters** 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 586e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ---------------------------------- | 587e41f4b71Sopenharmony_ci| callback | AsyncCallback\<boolean> | Yes | Whether the current common event is a sticky one. | 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci**Example** 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci```ts 592e41f4b71Sopenharmony_ci// Callback for checking whether the current common event is a sticky one. 593e41f4b71Sopenharmony_cifunction isStickyCommonEventCallback(err: BusinessError, isSticky:boolean) { 594e41f4b71Sopenharmony_ci if (err != null) { 595e41f4b71Sopenharmony_ci console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`); 596e41f4b71Sopenharmony_ci } else { 597e41f4b71Sopenharmony_ci console.info("isStickyCommonEvent " + JSON.stringify(isSticky)); 598e41f4b71Sopenharmony_ci } 599e41f4b71Sopenharmony_ci} 600e41f4b71Sopenharmony_cisubscriber.isStickyCommonEvent(isStickyCommonEventCallback); 601e41f4b71Sopenharmony_ci``` 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ci## isStickyCommonEvent 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ciisStickyCommonEvent(): Promise\<boolean> 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ciChecks whether a common event is a sticky one. This API uses a promise to return the result. 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ciReturns **true** if the common event is a sticky one; returns **false** otherwise. 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ci**Return value** 614e41f4b71Sopenharmony_ci 615e41f4b71Sopenharmony_ci| Type | Description | 616e41f4b71Sopenharmony_ci| ----------------- | -------------------------------- | 617e41f4b71Sopenharmony_ci| Promise\<boolean> | Whether the current common event is a sticky one. | 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci**Example** 620e41f4b71Sopenharmony_ci 621e41f4b71Sopenharmony_ci```ts 622e41f4b71Sopenharmony_cisubscriber.isStickyCommonEvent().then((isSticky:boolean) => { 623e41f4b71Sopenharmony_ci console.info("isStickyCommonEvent " + JSON.stringify(isSticky)); 624e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 625e41f4b71Sopenharmony_ci console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`); 626e41f4b71Sopenharmony_ci}); 627e41f4b71Sopenharmony_ci``` 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci## isStickyCommonEventSync<sup>10+</sup> 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ciisStickyCommonEventSync(): boolean 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ciChecks whether a common event is a sticky one. 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ciReturns **true** if the common event is a sticky one; returns **false** otherwise. 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 638e41f4b71Sopenharmony_ci 639e41f4b71Sopenharmony_ci**Return value** 640e41f4b71Sopenharmony_ci 641e41f4b71Sopenharmony_ci| Type | Description | 642e41f4b71Sopenharmony_ci| ----------------- | -------------------------------- | 643e41f4b71Sopenharmony_ci| boolean | Whether the current common event is a sticky one. | 644e41f4b71Sopenharmony_ci 645e41f4b71Sopenharmony_ci**Example** 646e41f4b71Sopenharmony_ci 647e41f4b71Sopenharmony_ci```ts 648e41f4b71Sopenharmony_cilet isSticky = subscriber.isStickyCommonEventSync(); 649e41f4b71Sopenharmony_ciconsole.info("isStickyCommonEventSync " + JSON.stringify(isSticky)); 650e41f4b71Sopenharmony_ci``` 651e41f4b71Sopenharmony_ci 652e41f4b71Sopenharmony_ci## abortCommonEvent 653e41f4b71Sopenharmony_ci 654e41f4b71Sopenharmony_ciabortCommonEvent(callback: AsyncCallback\<void>): void 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_ciAborts this common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. This API uses an asynchronous callback to return the result. 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ci**Parameters** 661e41f4b71Sopenharmony_ci 662e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 663e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | -------------------- | 664e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 665e41f4b71Sopenharmony_ci 666e41f4b71Sopenharmony_ci**Example** 667e41f4b71Sopenharmony_ci 668e41f4b71Sopenharmony_ci```ts 669e41f4b71Sopenharmony_ci// Callback for common event aborting. 670e41f4b71Sopenharmony_cifunction abortCommonEventCallback(err: BusinessError) { 671e41f4b71Sopenharmony_ci if (err != null) { 672e41f4b71Sopenharmony_ci console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 673e41f4b71Sopenharmony_ci } else { 674e41f4b71Sopenharmony_ci console.info("abortCommonEvent success"); 675e41f4b71Sopenharmony_ci } 676e41f4b71Sopenharmony_ci} 677e41f4b71Sopenharmony_cifunction finishCommonEventCallback(err: BusinessError) { 678e41f4b71Sopenharmony_ci if (err != null) { 679e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 680e41f4b71Sopenharmony_ci } else { 681e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 682e41f4b71Sopenharmony_ci } 683e41f4b71Sopenharmony_ci} 684e41f4b71Sopenharmony_cisubscriber.abortCommonEvent(abortCommonEventCallback); 685e41f4b71Sopenharmony_cisubscriber.finishCommonEvent(finishCommonEventCallback); 686e41f4b71Sopenharmony_ci``` 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci## abortCommonEvent 689e41f4b71Sopenharmony_ci 690e41f4b71Sopenharmony_ciabortCommonEvent(): Promise\<void> 691e41f4b71Sopenharmony_ci 692e41f4b71Sopenharmony_ciAborts this common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. This API uses a promise to return the result. 693e41f4b71Sopenharmony_ci 694e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 695e41f4b71Sopenharmony_ci 696e41f4b71Sopenharmony_ci**Return value** 697e41f4b71Sopenharmony_ci 698e41f4b71Sopenharmony_ci| Type | Description | 699e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 700e41f4b71Sopenharmony_ci| Promise\<void> | Promise used to return the result. | 701e41f4b71Sopenharmony_ci 702e41f4b71Sopenharmony_ci**Example** 703e41f4b71Sopenharmony_ci 704e41f4b71Sopenharmony_ci```ts 705e41f4b71Sopenharmony_cisubscriber.abortCommonEvent().then(() => { 706e41f4b71Sopenharmony_ci console.info("abortCommonEvent success"); 707e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 708e41f4b71Sopenharmony_ci console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 709e41f4b71Sopenharmony_ci}); 710e41f4b71Sopenharmony_cisubscriber.finishCommonEvent().then(() => { 711e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 712e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 713e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 714e41f4b71Sopenharmony_ci}); 715e41f4b71Sopenharmony_ci``` 716e41f4b71Sopenharmony_ci 717e41f4b71Sopenharmony_ci## abortCommonEventSync<sup>10+</sup> 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ciabortCommonEventSync(): void 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ciAborts this common event when used with [finishCommonEvent](#finishcommonevent9). After the abort, the common event is not sent to the next subscriber. 722e41f4b71Sopenharmony_ci 723e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 724e41f4b71Sopenharmony_ci 725e41f4b71Sopenharmony_ci**Example** 726e41f4b71Sopenharmony_ci 727e41f4b71Sopenharmony_ci```ts 728e41f4b71Sopenharmony_cisubscriber.abortCommonEventSync(); 729e41f4b71Sopenharmony_cisubscriber.finishCommonEvent().then(() => { 730e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 731e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 732e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 733e41f4b71Sopenharmony_ci}); 734e41f4b71Sopenharmony_ci``` 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci## clearAbortCommonEvent 737e41f4b71Sopenharmony_ci 738e41f4b71Sopenharmony_ciclearAbortCommonEvent(callback: AsyncCallback\<void>): void 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ciClears the aborted state of this common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. This API uses an asynchronous callback to return the result. 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ci**Parameters** 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 747e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | -------------------- | 748e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 749e41f4b71Sopenharmony_ci 750e41f4b71Sopenharmony_ci**Example** 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_ci```ts 753e41f4b71Sopenharmony_ci// Callback for clearing the aborted state of the current common event. 754e41f4b71Sopenharmony_cifunction clearAbortCommonEventCallback(err: BusinessError) { 755e41f4b71Sopenharmony_ci if (err != null) { 756e41f4b71Sopenharmony_ci console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 757e41f4b71Sopenharmony_ci } else { 758e41f4b71Sopenharmony_ci console.info("clearAbortCommonEvent success"); 759e41f4b71Sopenharmony_ci } 760e41f4b71Sopenharmony_ci} 761e41f4b71Sopenharmony_cifunction finishCommonEventCallback(err: BusinessError) { 762e41f4b71Sopenharmony_ci if (err != null) { 763e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 764e41f4b71Sopenharmony_ci } else { 765e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 766e41f4b71Sopenharmony_ci } 767e41f4b71Sopenharmony_ci} 768e41f4b71Sopenharmony_cisubscriber.clearAbortCommonEvent(clearAbortCommonEventCallback); 769e41f4b71Sopenharmony_cisubscriber.finishCommonEvent(finishCommonEventCallback); 770e41f4b71Sopenharmony_ci``` 771e41f4b71Sopenharmony_ci 772e41f4b71Sopenharmony_ci## clearAbortCommonEvent 773e41f4b71Sopenharmony_ci 774e41f4b71Sopenharmony_ciclearAbortCommonEvent(): Promise\<void> 775e41f4b71Sopenharmony_ci 776e41f4b71Sopenharmony_ciClears the aborted state of this common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. This API uses a promise to return the result. 777e41f4b71Sopenharmony_ci 778e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 779e41f4b71Sopenharmony_ci 780e41f4b71Sopenharmony_ci**Return value** 781e41f4b71Sopenharmony_ci 782e41f4b71Sopenharmony_ci| Type | Description | 783e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 784e41f4b71Sopenharmony_ci| Promise\<void> | Promise used to return the result. | 785e41f4b71Sopenharmony_ci 786e41f4b71Sopenharmony_ci**Example** 787e41f4b71Sopenharmony_ci 788e41f4b71Sopenharmony_ci```ts 789e41f4b71Sopenharmony_cisubscriber.clearAbortCommonEvent().then(() => { 790e41f4b71Sopenharmony_ci console.info("clearAbortCommonEvent success"); 791e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 792e41f4b71Sopenharmony_ci console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 793e41f4b71Sopenharmony_ci}); 794e41f4b71Sopenharmony_cisubscriber.finishCommonEvent().then(() => { 795e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 796e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 797e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 798e41f4b71Sopenharmony_ci}); 799e41f4b71Sopenharmony_ci``` 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ci## clearAbortCommonEventSync<sup>10+</sup> 802e41f4b71Sopenharmony_ci 803e41f4b71Sopenharmony_ciclearAbortCommonEventSync(): void 804e41f4b71Sopenharmony_ci 805e41f4b71Sopenharmony_ciClears the aborted state of this common event when used with [finishCommonEvent](#finishcommonevent9). After the clearance, the common event is sent to the next subscriber. 806e41f4b71Sopenharmony_ci 807e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 808e41f4b71Sopenharmony_ci 809e41f4b71Sopenharmony_ci**Example** 810e41f4b71Sopenharmony_ci 811e41f4b71Sopenharmony_ci```ts 812e41f4b71Sopenharmony_cisubscriber.clearAbortCommonEventSync(); 813e41f4b71Sopenharmony_cisubscriber.finishCommonEvent().then(() => { 814e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 815e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 816e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 817e41f4b71Sopenharmony_ci}); 818e41f4b71Sopenharmony_ci``` 819e41f4b71Sopenharmony_ci 820e41f4b71Sopenharmony_ci## getAbortCommonEvent 821e41f4b71Sopenharmony_ci 822e41f4b71Sopenharmony_cigetAbortCommonEvent(callback: AsyncCallback\<boolean>): void 823e41f4b71Sopenharmony_ci 824e41f4b71Sopenharmony_ciChecks whether this ordered common event should be aborted. This API uses an asynchronous callback to return the result. 825e41f4b71Sopenharmony_ci 826e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 827e41f4b71Sopenharmony_ci 828e41f4b71Sopenharmony_ci**Parameters** 829e41f4b71Sopenharmony_ci 830e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 831e41f4b71Sopenharmony_ci| -------- | ----------------------- | ---- | ---------------------------------- | 832e41f4b71Sopenharmony_ci| callback | AsyncCallback\<boolean> | Yes | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise. | 833e41f4b71Sopenharmony_ci 834e41f4b71Sopenharmony_ci**Example** 835e41f4b71Sopenharmony_ci 836e41f4b71Sopenharmony_ci```ts 837e41f4b71Sopenharmony_ci// Callback for checking whether the current common event is in the aborted state. 838e41f4b71Sopenharmony_cifunction getAbortCommonEventCallback(err: BusinessError, abortEvent:boolean) { 839e41f4b71Sopenharmony_ci if (err != null) { 840e41f4b71Sopenharmony_ci console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 841e41f4b71Sopenharmony_ci } else { 842e41f4b71Sopenharmony_ci console.info("getAbortCommonEvent " + JSON.stringify(abortEvent)); 843e41f4b71Sopenharmony_ci } 844e41f4b71Sopenharmony_ci} 845e41f4b71Sopenharmony_cisubscriber.getAbortCommonEvent(getAbortCommonEventCallback); 846e41f4b71Sopenharmony_ci``` 847e41f4b71Sopenharmony_ci 848e41f4b71Sopenharmony_ci## getAbortCommonEvent 849e41f4b71Sopenharmony_ci 850e41f4b71Sopenharmony_cigetAbortCommonEvent(): Promise\<boolean> 851e41f4b71Sopenharmony_ci 852e41f4b71Sopenharmony_ciChecks whether this ordered common event should be aborted. This API uses a promise to return the result. 853e41f4b71Sopenharmony_ci 854e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ci**Return value** 857e41f4b71Sopenharmony_ci 858e41f4b71Sopenharmony_ci| Type | Description | 859e41f4b71Sopenharmony_ci| ----------------- | ---------------------------------- | 860e41f4b71Sopenharmony_ci| Promise\<boolean> | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise. | 861e41f4b71Sopenharmony_ci 862e41f4b71Sopenharmony_ci**Example** 863e41f4b71Sopenharmony_ci 864e41f4b71Sopenharmony_ci```ts 865e41f4b71Sopenharmony_cisubscriber.getAbortCommonEvent().then((abortEvent:boolean) => { 866e41f4b71Sopenharmony_ci console.info("getAbortCommonEvent " + JSON.stringify(abortEvent)); 867e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 868e41f4b71Sopenharmony_ci console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 869e41f4b71Sopenharmony_ci}); 870e41f4b71Sopenharmony_ci``` 871e41f4b71Sopenharmony_ci 872e41f4b71Sopenharmony_ci## getAbortCommonEventSync<sup>10+</sup> 873e41f4b71Sopenharmony_ci 874e41f4b71Sopenharmony_cigetAbortCommonEventSync(): boolean 875e41f4b71Sopenharmony_ci 876e41f4b71Sopenharmony_ciChecks whether this ordered common event should be aborted. 877e41f4b71Sopenharmony_ci 878e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ci**Return value** 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ci| Type | Description | 883e41f4b71Sopenharmony_ci| ----------------- | ---------------------------------- | 884e41f4b71Sopenharmony_ci| boolean | Returns **true** if the ordered common event is in the aborted state; returns **false** otherwise. | 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci**Example** 887e41f4b71Sopenharmony_ci 888e41f4b71Sopenharmony_ci```ts 889e41f4b71Sopenharmony_cilet abortEvent = subscriber.getAbortCommonEventSync(); 890e41f4b71Sopenharmony_ciconsole.info("getAbortCommonEventSync " + JSON.stringify(abortEvent)); 891e41f4b71Sopenharmony_ci``` 892e41f4b71Sopenharmony_ci 893e41f4b71Sopenharmony_ci## getSubscribeInfo 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_cigetSubscribeInfo(callback: AsyncCallback\<CommonEventSubscribeInfo>): void 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_ciObtains the subscriber information. This API uses an asynchronous callback to return the result. 898e41f4b71Sopenharmony_ci 899e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 900e41f4b71Sopenharmony_ci 901e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci**Parameters** 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 906e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ---------------------- | 907e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Yes | Returns the subscriber information. | 908e41f4b71Sopenharmony_ci 909e41f4b71Sopenharmony_ci**Example** 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ci```ts 912e41f4b71Sopenharmony_ci// Callback for subscriber information obtaining. 913e41f4b71Sopenharmony_cifunction getSubscribeInfoCallback(err: BusinessError, subscribeInfo:commonEventManager.CommonEventSubscribeInfo) { 914e41f4b71Sopenharmony_ci if (err != null) { 915e41f4b71Sopenharmony_ci console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`); 916e41f4b71Sopenharmony_ci } else { 917e41f4b71Sopenharmony_ci console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo)); 918e41f4b71Sopenharmony_ci } 919e41f4b71Sopenharmony_ci} 920e41f4b71Sopenharmony_cisubscriber.getSubscribeInfo(getSubscribeInfoCallback); 921e41f4b71Sopenharmony_ci``` 922e41f4b71Sopenharmony_ci 923e41f4b71Sopenharmony_ci## getSubscribeInfo 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_cigetSubscribeInfo(): Promise\<CommonEventSubscribeInfo> 926e41f4b71Sopenharmony_ci 927e41f4b71Sopenharmony_ciObtains the subscriber information. This API uses a promise to return the result. 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 930e41f4b71Sopenharmony_ci 931e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 932e41f4b71Sopenharmony_ci 933e41f4b71Sopenharmony_ci**Return value** 934e41f4b71Sopenharmony_ci 935e41f4b71Sopenharmony_ci| Type | Description | 936e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ---------------------- | 937e41f4b71Sopenharmony_ci| Promise\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Returns the subscriber information. | 938e41f4b71Sopenharmony_ci 939e41f4b71Sopenharmony_ci**Example** 940e41f4b71Sopenharmony_ci 941e41f4b71Sopenharmony_ci```ts 942e41f4b71Sopenharmony_cisubscriber.getSubscribeInfo().then((subscribeInfo:commonEventManager.CommonEventSubscribeInfo) => { 943e41f4b71Sopenharmony_ci console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo)); 944e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 945e41f4b71Sopenharmony_ci console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`); 946e41f4b71Sopenharmony_ci}); 947e41f4b71Sopenharmony_ci``` 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ci## getSubscribeInfoSync<sup>10+</sup> 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_cigetSubscribeInfoSync(): CommonEventSubscribeInfo 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ciObtains the subscriber information. 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 958e41f4b71Sopenharmony_ci 959e41f4b71Sopenharmony_ci**Return value** 960e41f4b71Sopenharmony_ci 961e41f4b71Sopenharmony_ci| Type | Description | 962e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ---------------------- | 963e41f4b71Sopenharmony_ci| [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | Returns the subscriber information. | 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci**Example** 966e41f4b71Sopenharmony_ci 967e41f4b71Sopenharmony_ci```ts 968e41f4b71Sopenharmony_cilet subscribeInfo = subscriber.getSubscribeInfoSync(); 969e41f4b71Sopenharmony_ciconsole.info("getSubscribeInfoSync " + JSON.stringify(subscribeInfo)); 970e41f4b71Sopenharmony_ci``` 971e41f4b71Sopenharmony_ci 972e41f4b71Sopenharmony_ci## finishCommonEvent<sup>9+</sup> 973e41f4b71Sopenharmony_ci 974e41f4b71Sopenharmony_cifinishCommonEvent(callback: AsyncCallback\<void>): void 975e41f4b71Sopenharmony_ci 976e41f4b71Sopenharmony_ciFinishes this ordered common event. This API uses an asynchronous callback to return the result. 977e41f4b71Sopenharmony_ci 978e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 979e41f4b71Sopenharmony_ci 980e41f4b71Sopenharmony_ci**Parameters** 981e41f4b71Sopenharmony_ci 982e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 983e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | -------------------------------- | 984e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | Yes | Callback returned after the ordered common event is finished. | 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci**Example** 987e41f4b71Sopenharmony_ci 988e41f4b71Sopenharmony_ci```ts 989e41f4b71Sopenharmony_ci// Callback for ordered common event finishing. 990e41f4b71Sopenharmony_cifunction finishCommonEventCallback(err: BusinessError) { 991e41f4b71Sopenharmony_ci if (err != null) { 992e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 993e41f4b71Sopenharmony_ci } else { 994e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 995e41f4b71Sopenharmony_ci } 996e41f4b71Sopenharmony_ci} 997e41f4b71Sopenharmony_cisubscriber.finishCommonEvent(finishCommonEventCallback); 998e41f4b71Sopenharmony_ci``` 999e41f4b71Sopenharmony_ci 1000e41f4b71Sopenharmony_ci## finishCommonEvent<sup>9+</sup> 1001e41f4b71Sopenharmony_ci 1002e41f4b71Sopenharmony_cifinishCommonEvent(): Promise\<void> 1003e41f4b71Sopenharmony_ci 1004e41f4b71Sopenharmony_ciFinishes this ordered common event. This API uses a promise to return the result. 1005e41f4b71Sopenharmony_ci 1006e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Notification.CommonEvent 1007e41f4b71Sopenharmony_ci 1008e41f4b71Sopenharmony_ci**Return value** 1009e41f4b71Sopenharmony_ci 1010e41f4b71Sopenharmony_ci| Type | Description | 1011e41f4b71Sopenharmony_ci| ---------------- | -------------------- | 1012e41f4b71Sopenharmony_ci| Promise\<void> | Promise used to return the result. | 1013e41f4b71Sopenharmony_ci 1014e41f4b71Sopenharmony_ci**Example** 1015e41f4b71Sopenharmony_ci 1016e41f4b71Sopenharmony_ci```ts 1017e41f4b71Sopenharmony_cisubscriber.finishCommonEvent().then(() => { 1018e41f4b71Sopenharmony_ci console.info("finishCommonEvent success"); 1019e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 1020e41f4b71Sopenharmony_ci console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 1021e41f4b71Sopenharmony_ci}); 1022e41f4b71Sopenharmony_ci``` 1023