• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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## When 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| Available APIs| 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
30The sample code is as follows:
31
32```ts
33import { FenceExtensionAbility, geoLocationManager } from '@kit.LocationKit';
34import { notificationManager } from '@kit.NotificationKit';
35import { Want, wantAgent } from '@kit.AbilityKit';
36
37export class MyFenceExtensionAbility extends FenceExtensionAbility {
38  onFenceStatusChange(transition: geoLocationManager.GeofenceTransition, additions: Record<string, string>): void {
39    // Receive the geofence status change event and process the service logic.
40    console.info(`on geofence transition,id:${transition.geofenceId},event:${transition.transitionEvent},additions:${JSON.stringify(additions)}`);
41
42    // Send a geofence notification.
43    let wantAgentInfo: wantAgent.WantAgentInfo = {
44      wants: [
45        {
46          bundleName: 'com.example.myapplication',
47          abilityName: 'EntryAbility',
48          parameters:
49          {
50            "geofenceId": transition?.geofenceId,
51            "transitionEvent": transition?.transitionEvent,
52          }
53        } as Want
54      ],
55      actionType: wantAgent.OperationType.START_ABILITY,
56      requestCode: 100
57    };
58    wantAgent.getWantAgent(wantAgentInfo).then((wantAgentMy) => {
59      let notificationRequest: notificationManager.NotificationRequest = {
60        id: 1,
61        content: {
62          notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
63          normal: {
64            title: `Geofence Notification`,
65            text: `on geofence transition,id:${transition.geofenceId},event:${transition.transitionEvent},additions:${JSON.stringify(additions)}`,
66          }
67        },
68        notificationSlotType: notificationManager.SlotType.SOCIAL_COMMUNICATION,
69        wantAgent: wantAgentMy
70      };
71      notificationManager.publish(notificationRequest);
72    });
73  }
74}
75```
76
774. 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.
78
79```json
80{
81  "module": {
82    "extensionAbilities": [
83      {
84        "name": "MyFenceExtensionAbility",
85        "srcEntry": "./ets/fenceExtensionability/MyFenceExtensionAbility.ets",
86        "description": "MyFenceExtensionAbility",
87        "type": "fence",
88        "exported": true
89      },
90    ]
91  }
92}
93```
94