You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter如何实现基于位置触发的本地通知(应用关闭时仍生效)

Flutter中基于位置触发本地通知(应用关闭时仍生效)

可以实现,无需依赖持续后台定位,只需借助Flutter插件对接iOS和Android的系统级位置监测能力,就能实现应用完全关闭时触发位置相关的本地通知。

实现方案

1. 插件选择

推荐使用flutter_local_notifications——它直接支持iOS的UNLocationNotificationTrigger,同时可结合Android的地理围栏(Geofence)功能实现需求;无需额外的后台定位插件持续运行。

2. iOS端实现

配置准备

在Info.plist中添加位置权限描述,需包含:

  • NSLocationAlwaysAndWhenInUseUsageDescription(或NSLocationWhenInUseUsageDescription,根据需求选择)
  • 开启Xcode中项目的后台模式(Background Modes),勾选"Location Updates"(仅用于系统监听地理围栏,非应用后台定位)

代码示例

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();

Future<void> scheduleLocationTriggeredNotification() async {
  // 初始化通知插件
  const IOSInitializationSettings iosInitSettings = IOSInitializationSettings();
  await _notificationsPlugin.initialize(
    const InitializationSettings(iOS: iosInitSettings),
  );

  // 定义触发通知的地理区域:旧金山某坐标,半径100米,仅进入时触发
  const targetLocation = IOSNotificationLocationCenter(37.7749, -122.4194);
  const locationTrigger = IOSLocationTrigger(
    center: targetLocation,
    radius: 100,
    notifyOnEntry: true,
    notifyOnExit: false,
  );

  // 配置通知详情
  const iosNotificationDetails = IOSNotificationDetails(
    locationTrigger: locationTrigger,
  );
  const notificationDetails = NotificationDetails(iOS: iosNotificationDetails);

  // 调度通知(时间参数仅为占位,实际由位置触发)
  await _notificationsPlugin.schedule(
    0,
    "到达目标地点",
    "您已接近设定的位置",
    DateTime.now(),
    notificationDetails,
    androidAllowWhileIdle: true,
  );
}

iOS端由系统负责监听地理围栏,即便应用完全关闭,用户进入指定区域时系统会自动触发本地通知,无需应用后台运行。

3. Android端实现

配置准备

在AndroidManifest.xml中添加权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Android 10+ 需要后台位置权限 -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

同时确保系统位置服务开启,且应用已获得后台位置权限(部分国产ROM需手动开启应用后台权限)。

代码示例

Android需通过地理围栏实现,可结合geolocator申请权限,再通过插件或原生代码注册地理围栏,触发时发送通知:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:geolocator/geolocator.dart';

final FlutterLocalNotificationsPlugin _notificationsPlugin = FlutterLocalNotificationsPlugin();

Future<void> setupGeofenceNotification() async {
  // 申请位置权限
  LocationPermission permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) return;
  }
  if (permission == LocationPermission.deniedForever) return;

  // 注册地理围栏(注:geolocator不直接支持注册围栏,可使用`location`插件或原生代码实现)
  // 假设围栏触发时调用以下方法发送通知
  await _sendLocationNotification();
}

Future<void> _sendLocationNotification() async {
  const androidNotificationDetails = AndroidNotificationDetails(
    'location_notification_channel',
    '位置通知',
    channelDescription: '当到达指定位置时触发的通知',
    importance: Importance.max,
    priority: Priority.high,
  );
  const notificationDetails = NotificationDetails(android: androidNotificationDetails);
  
  await _notificationsPlugin.show(
    0,
    "到达目标地点",
    "您已接近设定的位置",
    notificationDetails,
  );
}

Android端地理围栏触发后,即便应用关闭,系统也会唤醒应用进程发送通知(需确保后台权限正常)。

关键注意事项

  • iOS:UNLocationNotificationTrigger是系统原生功能,稳定性高,无需担心后台被杀死的问题。
  • Android:不同厂商的ROM对后台限制不同,需测试适配,部分机型可能需要用户手动开启应用的"自启动"或"后台活动"权限。
  • 插件版本:确保使用最新版的flutter_local_notifications,以获得完整的位置触发支持。

内容的提问来源于stack exchange,提问作者Bartek Uchański

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:45:41