1# Subscribing to Common Events in Static Mode (for System Applications Only) 2<!--Kit: Basic Services Kit--> 3<!--Subsystem: Notification--> 4<!--Owner: @michael_woo888--> 5<!--Designer: @dongqingran; @wulong158--> 6<!--Tester: @wanghong1997--> 7<!--Adviser: @huipeizi--> 8 9## When to Use 10 11A static subscriber is started once it receives a target event published by the system or application. At the same time, [onReceiveEvent()](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md#staticsubscriberextensionabilityonreceiveevent) is triggered, in which you can implement the service logic. 12 13For example, if an application needs to execute some initialization tasks during device power-on, the application can subscribe to the power-on event in static mode. After receiving the power-on event, the application is started to execute the initialization tasks. 14 15Subscribing to a common event in static mode is achieved by configuring a declaration file and implementing a class that inherits from [StaticSubscriberExtensionAbility](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md). 16 17> **NOTE** 18> 19> Static subscription to common events affects system power consumption. Therefore, exercise caution when using this function. 20 21## Lifecycle 22 23The [StaticSubscriberExtensionAbility](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md) module is destroyed 15 seconds after [`onReceiveEvent()`](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md#staticsubscriberextensionabilityonreceiveevent) is executed. 24 25## How to Develop 26 271. Declare a static subscriber. 28 29 Create an ExtensionAbility inherited from [StaticSubscriberExtensionAbility](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md) in the project. 30 31 You can implement service logic using [onReceiveEvent()](../../reference/apis-basic-services-kit/js-apis-application-staticSubscriberExtensionAbility-sys.md#staticsubscriberextensionabilityonreceiveevent). 32 33 ```ts 34 import { commonEventManager, StaticSubscriberExtensionAbility } from '@kit.BasicServicesKit'; 35 import { hilog } from '@kit.PerformanceAnalysisKit'; 36 37 const TAG: string = 'StaticSubscriber'; 38 const DOMAIN_NUMBER: number = 0xFF00; 39 40 export default class StaticSubscriber extends StaticSubscriberExtensionAbility { 41 onReceiveEvent(event: commonEventManager.CommonEventData): void { 42 hilog.info(DOMAIN_NUMBER, TAG, 'onReceiveEvent, event: ' + event.event); 43 //... 44 } 45 } 46 ``` 47 482. Configure static subscriber settings. 49 50 After writing the static subscriber code, configure the subscriber in the [module.json5](../../quick-start/module-configuration-file.md) file. 51 52 ```json 53 { 54 "module": { 55 // ... 56 "extensionAbilities": [ 57 { 58 "name": "StaticSubscriber", 59 "srcEntry": "./ets/staticsubscriber/StaticSubscriber.ets", 60 "description": "$string:StaticSubscriber_desc", 61 "icon": "$media:app_icon", 62 "label": "$string:StaticSubscriber_label", 63 "type": "staticSubscriber", 64 "exported": false, 65 "metadata": [ 66 { 67 "name": "ohos.extension.staticSubscriber", 68 "resource": "$profile:subscribe" 69 } 70 ] 71 } 72 ], 73 // ... 74 } 75 } 76 ``` 77 78 Some fields in the file are described as follows: 79 80 - **srcEntry**: entry file path of the ExtensionAbility, that is, the file path of the static subscriber declared in Step 2. 81 82 - **type**: ExtensionAbility type. For a static subscriber, set this field to **staticSubscriber**. 83 84 - **metadata**: level-2 configuration file information of the ExtensionAbility. The configuration information varies according to the ExtensionAbility type. Therefore, you must use different config files to indicate the specific configuration. 85 - **name**: name of the ExtensionAbility. For a static subscriber, declare the name as **ohos.extension.staticSubscriber** for successful identification. 86 - **resource**: path that stores the ExtensionAbility configuration, which is customizable. In this example, the path is **resources/base/profile/subscribe.json**. 87 88 893. Configure the level-2 configuration file **subscribe.json** to which the metadata points. 90 91 > **NOTE** 92 > 93 > If the level-2 configuration file is not declared in this format, the file cannot be identified. 94 95 ```json 96 { 97 "commonEvents": [ 98 { 99 "name": "StaticSubscriber", 100 "permission": "", 101 "events": [ 102 "usual.event.AIRPLANE_MODE" 103 ], 104 "filter": [ 105 { 106 "event": "usual.event.AIRPLANE_MODE", 107 "conditions": { 108 "code": 0, 109 "data": "send common event data", 110 "parameters": { 111 "bundleType": 1, 112 "userId": 100 113 } 114 } 115 } 116 ] 117 } 118 ] 119 } 120 ``` 121 122 The **commonEvents** tag identifies a static subscription event configured by a subscriber. Lightweight devices do not support this tag and its subtags. The tag value is of an object array and contains four subtags: **name**, **permission**, **events**, and **filter**. 123 124 **Table 1** commonEvents 125 126 | **Name**| **Description** | **Data Type**| **Initial Value Allowed** | 127 | ------------ | ------------------------------------------------------------ | ------------ | -------------------------- | 128 | name | Name of the ExtensionAbility, which must be the same as the name of **extensionAbility** declared in **module.json5**.| String | No | 129 | permission | Permissions required by the publisher. | String | Yes (initial value: left empty)| 130 | events | List of subscribed target events. | String array | No | 131 | filter | Filter criteria for static events. This attribute is supported since API version 18.<br>For details about the values, see the following table.| Object array | Yes (initial value: left empty)| 132 133 The **filter** tag identifies the static subscription events that can be filtered by the subscriber as required. The tag value is of the object array type and contains two subtags: **event** and **conditions**. 134 135 **Table 2** filter 136 137 | **Name**| **Description** | **Data Type**| **Initial Value Allowed** | 138 | ------------ | ------------------------------------------------------------ | ------------ | ---------------- | 139 | event | Name of the event to be filtered out for static subscription events. The event name should match with the event list; otherwise, the configuration is invalid.| String | No| 140 | conditions | Filter criteria of an event, which is obtained from [CommonEventPublishData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventPublishData.md). | Object | No| 141 142 The **conditions** tag identifies the condition configuration for filtering static subscription events. The tag value is of the object type and contains three subtags: **code**, **data**, and **parameters**. 143 144 **Table 3** conditions 145 146 | **Name**| **Description** | **Data Type** | **Initial Value Allowed** | 147 | ------------ | ------------------------------------------ | ------------------ | -------------------------- | 148 | code | Result code to filter. | Integer | Yes (initial value: left empty)| 149 | data | Custom result data to filter.| String | Yes (initial value: left empty)| 150 | parameters | Additional information to filter for a static subscription event. Only data of the Boolean, number, or string type can be configured.| Object| Yes (initial value: left empty)| 151 152 1534. Modify the [preset configuration file](https://gitee.com/openharmony/vendor_hihope/blob/master/rk3568/preinstall-config/install_list_capability.json) of the device, that is, the **/system/variant/phone/base/etc/app/install_list_capability.json** file. When the device is started, this file is read. During application installation, the common event type specified by **allowCommonEvent** in the file is authorized. The **install_list_capability.json** file contains the following fields: 154 155 - **bundleName**: bundle name of the application. 156 - **app_signature**: fingerprint information of the application. For details about how to configure fingerprint information, see [Application Privilege Configuration](https://gitee.com/openharmony/docs/blob/master/en/device-dev/subsystems/subsys-app-privilege-config-guide.md#configuration-in-install_list_capabilityjson), or obtain and enter the application ID using [Bundle Manager](https://gitee.com/openharmony/docs/blob/master/en/application-dev/tools/bm-tool.md). 157 - **allowCommonEvent**: type of common event that can be started by static broadcast. 158 159 ```json 160 [ 161 // ... 162 { 163 "bundleName": "com.samples.stageprocessthread", // Bundle name. 164 "app_signature": ["****"], // Fingerprint information. 165 "allowCommonEvent": ["usual.event.AIRPLANE_MODE"] // Type of common event that can be started by static broadcast. 166 } 167 ] 168 ``` 169 170 > **NOTE** 171 > 172 > The **install_list_capability.json** file is available only for preinstalled applications. 173