【发布时间】:2021-05-20 14:15:43
【问题描述】:
我正在开发一个使用 Location SDK (https://docs.expo.io/versions/latest/sdk/location/) 对区域进行地理围栏的裸露的 Expo 项目。
地理围栏在 iOS 上运行良好,但在 Android 上在后台(或当应用程序被强制退出时)可能非常慢。退出或进入地理围栏时,运行注册任务有时可能需要 15 分钟以上。
我还注意到,如果我打开谷歌地图等应用并点击 GPS 按钮,我可以强制触发已注册的地理围栏任务运行。
是否有可能以某种方式加速位置更新,或者在 Android Studio 中配置一些东西?
package.json 包括:
- “展会地点”:“^12.0.4”,
- “展会通知”:“^0.11.5”,
- "expo-permissions": "^12.0.1",
- "expo-task-manager": "^9.0.0",
- “反应”:“17.0.1”,
- “react-native”:“0.64.1”,
如果您还需要什么,请告诉我。
import React, { useEffect, useState } from "react";
import { StyleSheet, Text, View } from "react-native";
import {
getForegroundPermissionsAsync,
requestBackgroundPermissionsAsync,
requestForegroundPermissionsAsync,
startGeofencingAsync,
} from "expo-location";
import * as Notifications from "expo-notifications";
import { LocationGeofencingEventType } from "expo-location";
import * as TaskManager from "expo-task-manager";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
TaskManager.defineTask(
"GEOFENCE_TASK",
({ data: { eventType, region }, error }) => {
if (error) {
// check `error.message` for more details.
return;
}
if (eventType === LocationGeofencingEventType.Enter) {
console.log("You've entered region:", region);
Notifications.scheduleNotificationAsync({
content: {
title: "ENTERED GEOFENCE",
body: region.identifier,
},
trigger: null,
});
} else if (eventType === LocationGeofencingEventType.Exit) {
console.log("You've left region:", region);
Notifications.scheduleNotificationAsync({
content: {
title: "EXITED GEOFENCE",
body: region.identifier,
},
trigger: null,
});
}
}
);
export default function App() {
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const setUp = async () => {
const { granted: notificationsGranted } =
await Notifications.getPermissionsAsync();
if (!notificationsGranted) {
await Notifications.requestPermissionsAsync();
}
const { granted: fgGranted } = await getForegroundPermissionsAsync();
if (!fgGranted) {
await requestForegroundPermissionsAsync();
await requestBackgroundPermissionsAsync();
}
const geofences = [
{
identifier: "Stockholm",
latitude: 59.332598,
longitude: 18.035258,
radius: 100,
notifyOnEnter: true,
notifyOnExit: true,
},
];
await startGeofencingAsync("GEOFENCE_TASK", geofences);
};
setUp();
}, []);
return (
<View style={styles.container}>
{isLoading ? <Text>App is Loading</Text> : <Text>Loading done</Text>}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
【问题讨论】:
标签: react-native expo