【问题标题】:Is it possible without react-native to gather geolocation information in a background task for Android and/or iOS?没有 react-native 是否可以在 Android 和/或 iOS 的后台任务中收集地理位置信息?
【发布时间】:2019-03-11 15:38:32
【问题描述】:

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

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

【问题讨论】:

    标签: android ios react-native geolocation react-native-android


    【解决方案1】:

    肯定是...如果您只有一个给定职位(或少于 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 并为其分配一个代理以在您的 application(_application:didFinishLaunchingWithOptions) 函数中接收更新!

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 2012-05-23
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 2019-06-04
      • 1970-01-01
      相关资源
      最近更新 更多