搜索插件、代码仓与 APP 分类

插件详情v1.5.0

geofence_foreground_service

一个 Flutter 项目,创建一个前台服务来处理地理围栏。

AndroidiOS
近 30 天下载
384
喜欢数
38
Pub 分数
160

安装插件

flutter pub add geofence_foreground_service

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 包,可在 Android 和 iOS 上运行前台服务以实现地理围栏功能。它允许应用程序监控地理边界,并在用户进入或离开定义区域时触发操作,即使应用处于后台状态也能实现。该服务会持续运行并显示持久通知,确保在各平台特定限制下仍能可靠运行。专为实时位置跟踪设计,支持 Android 和 iOS 平台,设置简单。

适合场景

  • 实时位置监控
  • 针对零售或物流的地理围栏警报
  • 基于位置的触发器在健身应用中
  • 快递服务的背景跟踪
  • 基于距离的通知

主要特点

  • 持久的前台服务
  • 跨平台 Android 和 iOS 支持
  • 所需配置最少
  • 与 Flutter 位置服务的集成
  • 支持进入/退出地理围栏事件

适合对象

  • 需要后台位置跟踪的应用程序
  • 位置感知应用
  • 需要可靠地理围栏的服务
  • 开发物流或零售解决方案的开发者
  • 具有严格后台执行需求的项目

使用建议

  • 需要用户权限才能访问位置
  • 持久通知可能影响用户体验
  • 电池使用量随着持续追踪而增加
  • 平台特定的后台执行限制
  • 可能需要为iOS后台模式进行额外配置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

geofence_foreground_service

Flutter Version License Platform Platform

A Flutter plugin that enables you to easily handle geofencing events in your Flutter app by utilizing native OS APIs on Android by creating a foreground service while being battery efficient since it uses the Geofence and WorkManager APIs. And on iOS by utilizing the CLLocationManager

It's important to note that the workmanager and flutter_foreground_task plugins were a great source of inspiration while creating this plugin.

Android iOS
Android Demo iOS Demo

Features

  • Supports geofencing in foreground as well as background 💪
  • Geofence a circular area 🗺️
  • Geofence a polygon 🤯 You can add a geofence using a list of coordinates, the system will calculate the center of them and register it, having full polygon support is a WIP 🚧
  • Notification customization 🔔: ⚠️Android⚠️ Displaying a notification when running a foreground service is mandatory, you can customize what is being displayed on it (title, content or the icon), the plugin displays your app icon by default.
  • Notification responsiveness ⏱️: ⚠️Android⚠️ You can set the responsiveness of the android notifications as per the docs here

Setup

🔧 Android Setup
  • Enable MultiDex, you can check how to do so here
  • Add the service to the AndroidManifest.xml inside the application tag
<service 
    android:name="com.f2fk.geofence_foreground_service.GeofenceForegroundService"
    android:foregroundServiceType="location">
</service>
  • Add the permissions
<!--required-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

<!--at least one of the follwoing-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  • Make sure the minSdkVersion in the app/build.gradle file is 29+
🔧 iOS Setup
  • Navigate to the Podfile and make sure to set the iOS version to 12+
platform :ios, '12.0'
  • Make sure to add the following permission to your Info.plist
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app need your location to provide best feature based on location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app need your location to provide best feature based on location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app need your location to provide best feature based on location</string>
  • Turn on the Location updates and Background fetch capabilities from XCode iOS capabilities

Example

Define the method that will handle the Geofence triggers

import 'package:geofence_foreground_service/exports.dart';
import 'package:geofence_foreground_service/geofence_foreground_service.dart';
import 'package:geofence_foreground_service/models/zone.dart';

// This method is a top level method
@pragma('vm:entry-point')
void callbackDispatcher() async {
  GeofenceForegroundService().handleTrigger(
    backgroundTriggerHandler: (zoneID, triggerType) {
      log(zoneID, name: 'zoneID');

      if (triggerType == GeofenceEventType.enter) {
        log('enter', name: 'triggerType');
      } else if (triggerType == GeofenceEventType.exit) {
        log('exit', name: 'triggerType');
      } else if (triggerType == GeofenceEventType.dwell) {
        log('dwell', name: 'triggerType');
      } else {
        log('unknown', name: 'triggerType');
      }

      return Future.value(true);
    },
  );
}

Then create an instance of the plugin to initiate it and assign GeoFences to it

final List<LatLng> timesSquarePolygon = [
  const LatLng(40.758078, -73.985640),
  const LatLng(40.757983, -73.985417),
  const LatLng(40.757881, -73.985493),
  const LatLng(40.757956, -73.985688),
];

Future<void> initPlatformState() async {
  // Remember to handle permissions before initiating the plugin

  bool hasServiceStarted = await GeofenceForegroundService().startGeofencingService(
    contentTitle: 'Test app is running in the background',
    contentText: 'Test app will be running to ensure seamless integration with ops team',
    notificationChannelId: 'com.app.geofencing_notifications_channel',
    serviceId: 525600,
    callbackDispatcher: callbackDispatcher,
  );

  if (hasServiceStarted) {
    await GeofenceForegroundService().addGeofenceZone(
      zone: Zone(
        id: 'zone#1_id',
        radius: 10000, // measured in meters
        coordinates: timesSquarePolygon,
        triggers: [
          GeofenceEventType.dwell,
          GeofenceEventType.enter,
          GeofenceEventType.exit
        ], // Currently, only available on Android
        expirationDuration: const Duration(days: 1), // Currently, only available on Android
        dwellLoiteringDelay: const Duration(hours: 1), // Currently, only available on Android
        initialTrigger: GeofenceEventType.enter, // Currently, only available on Android
      ),
    );
  }
}

Something important to point out is the callbackDispatcher method will run in an entirely different isolate than the actual app, so if you were to handle UI related code inside of it you'll need to use Ports, you can find more information here

You can pass a custom icon to the foreground service notification if you wish while initializing the service, this icon will be placed inside the android/app/src/main/res folder, you can check the example for more information, by default, it will take the app icon

const NotificationIconData(
  resType: ResourceType.mipmap,
  resPrefix: ResourcePrefix.ic,
  name: 'launcher',
)

Notes

Handling permissions is not a part of the package, so please refer to permission_handler plugin to grant the required permissions (it's used in the example too)

  • location
  • locationAlways
  • notification

Contributing

We welcome contributions. Please read CONTRIBUTING.md for pull request expectations (including small PRs and a maximum of 15 files per PR), testing, changelog updates, and review practices.

所属分类

依赖

依赖清单 2 项

  • latlng^2.0.7
  • plugin_platform_interface^2.1.8

源码仓库

使用此插件的项目

暂无相关数据。