当应用程序位于后台时,我试图跟踪我的用户位置,它在前台工作正常,即使我将前台服务参数添加到这里是我的代码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中弹出
您是否有可能使用 iOS 并使用 Expo Go 应用程序进行开发? 我刚刚遇到了类似的问题,如果没有开发版本,TaskManager 似乎无法在 iOS 上工作。
引用文档:
TaskManager 在 Android 上的 Expo Go 应用程序中开箱即用,但是,在 iOS 上,您需要使用开发版本。
虽然我在这里迟到了,但我也在做同样的工作。所以发生这种情况有两个原因:-