是否可以在没有 react-native 的情况下在 Android and/or iOS 的后台任务中收集地理位置信息?

Is it possible without react-native to gather geolocation information in a background task for Android and/or iOS?

即使 phone 被锁定,是否可以在 react-native 中执行监控 GPS 位置的后台任务(没有 运行 整个应用程序在后台)? 当 phone 处于某个位置时,我想在屏幕上显示一些信息(通知)。

我在学习中需要声明一个项目的主题,但我不知道是否可以实现这个功能。

肯定是...如果您只有一个给定职位(或少于 20 个职位),您最好使用 CoreLocation 的 geofencing API!

您可以像这样设置区域:

if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
    // Register the region.
    let region = CLCircularRegion(
        center: center, 
        radius: maxDistance, identifier: identifier
    )
    region.notifyOnEntry = true

    locationManager.startMonitoring(for: region)
}

(假设您已经请求并检查了用户的位置权限)!

只要发生区域监控事件,您的应用程序就会在后台启动,因此您必须确保设置一个新的 CLLocationManager 并给它一个委托来接收您 [=12= 中的更新]函数!

然后您可以使用委托回调来触发本地通知。

这里有一个小警告,如果用户为您的应用禁用了后台应用刷新,这将不起作用!

幸运的是,已经有一个 react-native 库允许您设置这些区域:https://github.com/martijndeh/react-native-region-monitor。不幸的是,我确实认为它会在收到通知时完全启动您的 JavaScript 应用程序(当然是在后台),但这不是什么大问题。

GitHub 上可能还提供了有关如何在 Android 上实现此功能的信息!