startLocationUpdatesAsync 不适用于后台反应原生博览会

问题描述 投票:0回答:2

当应用程序位于后台时,我试图跟踪我的用户位置,它在前台工作正常,即使我将前台服务参数添加到这里是我的代码startLocationUpdatesAsync,这里是我的代码,也不会显示通知,这是我的代码

 import React from "react";
import { StyleSheet, Text, View, Button } from "react-native";
import * as BackgroundFetch from "expo-background-fetch";
import * as TaskManager from "expo-task-manager";
import * as Location from "expo-location";

const BACKGROUND_FETCH_TASK = "background-fetch";

TaskManager.defineTask(
  BACKGROUND_FETCH_TASK,
  ({ data: { locations }, error }) => {
    if (error) {
      // check `error.message` for more details.
      return;
    }
    console.log("Received new locations", locations);
  }
);



export default function BackgroundFetchScreen() {
  const [isRegistered, setIsRegistered] = React.useState(false);
  const [status, setStatus] = React.useState(null);

  React.useEffect(() => {
    checkStatusAsync();
  }, []);

  const checkStatusAsync = async () => {
    await Location.requestForegroundPermissionsAsync();
    await Location.requestBackgroundPermissionsAsync();
    const status = await BackgroundFetch.getStatusAsync();
    const isRegistered = await TaskManager.isTaskRegisteredAsync(
      BACKGROUND_FETCH_TASK
    );
    setStatus(status);
    setIsRegistered(isRegistered);
  };

  const toggleFetchTask = async () => {
    if (isRegistered) {
      Location.hasStartedLocationUpdatesAsync(BACKGROUND_FETCH_TASK).then(
        (value) => {
          if (value) {
            Location.stopLocationUpdatesAsync(BACKGROUND_FETCH_TASK);
          }
        }
      );

    } else {
      Location.startLocationUpdatesAsync(BACKGROUND_FETCH_TASK, {
        accuracy: Location.Accuracy.BestForNavigation,
        distanceInterval: 1, // minimum change (in meters) betweens updates
        deferredUpdatesInterval: 1000, // minimum interval (in milliseconds) between updates
        foregroundService: {
          notificationTitle: "En ligne ... ",
          notificationBody: "Mise à jour de votre position en cours ...",
        },

        showsBackgroundLocationIndicator: true,
      });
    }

    checkStatusAsync();
  };

  return (
    <View style={styles.screen}>
      <View style={styles.textContainer}>
        <Text>
          Background fetch status:{" "}
          <Text style={styles.boldText}>
            {status && BackgroundFetch.BackgroundFetchStatus[status]}
          </Text>
        </Text>
        <Text>
          Background fetch task name:{" "}
          <Text style={styles.boldText}>
            {isRegistered ? BACKGROUND_FETCH_TASK : "Not registered yet!"}
          </Text>
        </Text>
      </View>
      <View style={styles.textContainer}></View>
      <Button
        title={
          isRegistered
            ? "Unregister BackgroundFetch task"
            : "Register BackgroundFetch task"
        }
        onPress={toggleFetchTask}
      />
    </View>
  );
}
const styles = StyleSheet.create({});

我使用的是expo SDK 46,我根本无法访问后台的位置,我需要一个解决方案,即使这意味着从expo中弹出

reactjs react-native expo location react-native-maps
2个回答
0
投票

您是否有可能使用 iOS 并使用 Expo Go 应用程序进行开发? 我刚刚遇到了类似的问题,如果没有开发版本,TaskManager 似乎无法在 iOS 上工作。

引用文档:

TaskManager 在 Android 上的 Expo Go 应用程序中开箱即用,但是,在 iOS 上,您需要使用开发版本。

https://docs.expo.dev/versions/latest/sdk/task-manager/


0
投票

虽然我在这里迟到了,但我也在做同样的工作。所以发生这种情况有两个原因:-

  1. 应用程序已经进行了电池优化,因此一段时间后它们会在后台休眠。尝试在设置中关闭应用程序的优化 了解更多信息 - https://developer.android.com/training/monitoring-device-state/doze-standby。 使用 React Native 中的 Linking.openSettings() 方法,它将直接为用户打开应用程序设置 我已经拉了足够多的头发才能得出这个结论:P
  2. 您可以将选项中的精度标志更改为最高,即6(Location.Accuracy.BestForNavigation)。尝试这样做,它将更新最高值。低精度意味着GPS将在更长的时间内触发。
© www.soinside.com 2019 - 2024. All rights reserved.