1# FenceExtensionAbility
2
3## Overview
4[FenceExtensionAbility](../reference/apis-location-kit/js-apis-app-ability-FenceExtensionAbility.md) is an ExtensionAbility of the geofence type. It enables you to efficiently implement geofencing functionalities.
5
6## How to Use
7
81. Subscribe to geofence status change events through the [geoLocationManager.on('gnssFenceStatusChange')](../reference/apis-location-kit/js-apis-geoLocationManager.md#geolocationmanagerongnssfencestatuschange) API of the location service, and pass the FenceExtensionAbility parameters in the **want** parameter.
92. After the event is triggered, the system reports the geofence event and data through the **onFenceStatusChange** API. Based on the event received, the application can perform corresponding service processing, for example, sending notifications.
10
11## Available APIs
12For details about the APIs, see [FenceExtensionAbility](../reference/apis-location-kit/js-apis-app-ability-FenceExtensionAbility.md).
13| API| Description|
14| ---- | ---- |
15| onFenceStatusChange(transition: geoLocationManager.GeofenceTransition, additions: Record<string, string>): void  | Callback invoked when a geofence status change event is received. Service processing is then performed based on the event type and data.|
16| onDestroy(): void | Callback invoked when a FenceExtensionAbility destruction event is received.|
17
18## How to Develop
19
20### Implementing a FenceExtensionAbility
21
22Implement the capability of the [FenceExtensionAbility](../reference/apis-location-kit/js-apis-app-ability-FenceExtensionAbility.md) provider.
23
24To manually create a FenceExtensionAbility in a project in DevEco Studio, perform the following steps:
25
261. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **fenceextensionability**.
272. Right-click the **fenceextensionability** directory, and choose **New > File** to create a file named **MyFenceExtensionAbility.ets**.
283. Open the **MyFenceExtensionAbility.ets** file and import its dependencies. Customize a class that inherits from **FenceExtensionAbility** and implement the **onFenceStatusChange** and **onDestroy** APIs.
29
30   The code snippet is as follows:
31
32   ```ts
33   import { notificationManager } from '@kit.NotificationKit';
34   import { wantAgent } from '@kit.AbilityKit';
35   import { FenceExtensionAbility, geoLocationManager } from '@kit.LocationKit';
36
37   export default class MyFenceExtensionAbility extends FenceExtensionAbility {
38     async onFenceStatusChange(transition: geoLocationManager.GeofenceTransition,
39       additions: Record<string, string>): Promise<void> {
40       // Receive the geofence status change event and process the service logic.
41       console.log(`on geofence transition,id:${transition.geofenceId},event:${transition.transitionEvent},additions:${JSON.stringify(additions)}`);
42
43       // Send a geofence notification.
44       let wantAgentInfo: wantAgent.WantAgentInfo = {
45         wants: [
46           {
47             bundleName: 'com.example.myapplication',
48             abilityName: 'EntryAbility',
49             parameters:
50             {
51               "geofenceId": transition?.geofenceId,
52               "transitionEvent": transition?.transitionEvent,
53             }
54           } as Want
55         ],
56         actionType: wantAgent.OperationType.START_ABILITY,
57         requestCode: 100
58       };
59       let wantAgentMy = await wantAgent.getWantAgent(wantAgentInfo);
60       let notificationRequest: notificationManager.NotificationRequest = {
61         id: 1,
62         content: {
63           notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
64           normal: {
65             title: `Geofence Notification`,
66             text: `on geofence transition,id:${transition.geofenceId},event:${transition.transitionEvent},additions:${JSON.stringify(additions)}`,
67           }
68         },
69         notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
70         wantAgent: wantAgentMy
71       };
72       notificationManager.publish(notificationRequest);
73     }
74
75     onDestroy(): void {
76       // Process the FenceExtensionAbility destruction event.
77       console.log(`on ability destroy`);
78     }
79   }
80   export default MyFenceExtensionAbility;
81   ```
82
834. Register the FenceExtensionAbility in the [module.json5 file](../quick-start/module-configuration-file.md) of the module in the project. Set **type** to **"fence"** and **srcEntry** to the code path of the FenceExtensionAbility component.
84
85    ```json
86    {
87      "module": {
88        "extensionAbilities": [
89          {
90            "name": "MyFenceExtensionAbility",
91            "srcEntry": "./ets/fenceExtensionability/MyFenceExtensionAbility.ets",
92            "description": "MyFenceExtensionAbility",
93            "type": "fence",
94            "exported": true
95          },
96        ]
97      }
98    }
99    ```