React native 与iOS相比,android上的地理围栏速度较慢
我正在进行一个裸体的世博会项目,该项目使用Location SDK()对一个区域进行地理隔离 GeoFenging在iOS上运行良好,但在Android上,它在后台的运行速度可能非常慢(或者当应用程序被强制退出时)。当退出或进入地理围栏时,有时运行注册的任务可能需要15分钟以上 我还注意到,如果我打开一个应用程序(如谷歌地图)并按下GPS按钮,我可以强制触发注册的地理围栏任务运行 是否有可能以某种方式加快位置更新,或者在Android Studio中配置一些东西 package.json包括:React native 与iOS相比,android上的地理围栏速度较慢,react-native,expo,React Native,Expo,我正在进行一个裸体的世博会项目,该项目使用Location SDK()对一个区域进行地理隔离 GeoFenging在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",
},
});
import React,{useffect,useState}来自“React”;
从“react native”导入{样式表、文本、视图};
进口{
getForegroundPermissionsAsync,
requestBackgroundPermissionsAsync,
requestForegroundPermissionsAsync,
startGeofencingAsync,
}从“世博会地点”;
从“世博会通知”导入*作为通知;
从“世博会地点”导入{LocationGeofencingEventType};
从“expo任务管理器”导入*作为任务管理器;
Notifications.setNotificationHandler({
handleNotification:async()=>({
shouldshowart:是的,
声音:错,
应该是B:错,
}),
});
TaskManager.defineTask(
“地理围栏任务”,
({data:{eventType,region},error})=>{
如果(错误){
//有关详细信息,请查看“error.message”。
返回;
}
if(eventType==LocationGeofencingEventType.Enter){
log(“您已经输入了region:”,region);
Notifications.scheduleNotificationAsync({
内容:{
标题:“进入地球围栏”,
正文:region.identifier,
},
触发器:null,
});
}else if(eventType==LocationGeofencingEventType.Exit){
log(“您已离开区域:”,区域);
Notifications.scheduleNotificationAsync({
内容:{
标题:“退出地球围栏”,
正文:region.identifier,
},
触发器:null,
});
}
}
);
导出默认函数App(){
const[isLoading,setIsLoading]=useState(true);
useffect(()=>{
const setUp=async()=>{
const{grated:notificationsGranted}=
等待通知。getPermissionsAsync();
如果(!notificationsGranted){
等待通知。requestPermissionsAsync();
}
const{grated:fgrated}=wait getForegroundPermissionsAsync();
如果(!fg已授予){
等待requestForegroundPermissionsAsync();
Wait requestBackgroundPermissionsAsync();
}
const Geofenses=[
{
标识符:“斯德哥尔摩”,
纬度:59.332598,
经度:18.035258,
半径:100,
是的,
notifyOnExit:对,
},
];
等待启动Geofencingasync(“地理围栏任务”,地理围栏);
};
设置();
}, []);
返回(
{isLoading?应用程序正在加载:加载完成}
);
}
const styles=StyleSheet.create({
容器:{
弹性:1,
背景颜色:“fff”,
对齐项目:“中心”,
辩护内容:“中心”,
},
});
我发现了同样的“问题”,为了缓解它,我使用前台通知来改进地理围栏服务
好消息是,该技巧在“应用程序正在使用”许可下运行良好。(但后台位置权限是强制性的)
关于。您在前台通知中使用了什么软件包/sdk?注意到任何明显的电池消耗吗?disclamer,我在HEROW工作,我们提供基于地理围栏的SDK。为此,我编写了一个简单的前台服务。(). 当然,电池电量会更耗尽,但只要告知使用,这是可以接受的。