react-native - 与 iOS 相比,Android 上的地理围栏速度较慢
问题描述
我正在开发一个使用 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”,
- “世博权限”:“^12.0.1”,
- “世博任务管理器”:“^9.0.0”,
- “反应”:“17.0.1”,
- “反应原生”:“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",
},
});
解决方案
我发现了同样的“问题”并为了缓解它,我使用前台通知来改进地理围栏服务。
好消息是,该技巧适用于“应用程序正在使用”权限。(但后台位置权限是强制性的)
问候。
推荐阅读
- task - 为什么我的 leetcode 任务不是从数字 1 开始的?
- javascript - Promise.resolve() 什么时候触发 then() 方法?
- javascript - 为什么 React 源代码是用 JavaScript 而不是 TypeScript 编写的,但仍然有类型?
- swift - 如果默认设置为 true,为什么我需要设置 Analytics.setAnalyticsCollectionEnabled(true)?Firebase + iOS
- sql - 查询以显示类似的项目组,其中包含单个主要项目和多个用逗号分隔的组项目
- python - 从 SFTP 服务器读取图像 EXIF 数据而不下载文件
- javascript - 使用 javascript selenium 清除和发送密钥
- c - xmlsec lib 分离签名
- python - Python & Sqlite3 更改未保存到数据库
- haskell - 两个列表的 Haskell 映射函数