byCedric / office-marathon

Example app for background location tracking in Expo
MIT License
61 stars 15 forks source link

Errors running in expo Go - seems to work for the version in the app store #14

Open josh-gree opened 3 years ago

josh-gree commented 3 years ago

Description of the bug

Unable to get the app to work via expoGo

To Reproduce

npm install
expo start

Expected behavior

Not to get errors

Actual behavior

Permission screen only asks for foreground permissions then moves to the tracking screen - but gets Not authorized to use background location services. on press

Android Bundling complete 87ms
Android Running app on POT-LX1

expo-permissions is now deprecated — the functionality has been moved to other expo packages that directly use these permissions (e.g. expo-location, expo-camera). The package will be removed in the upcoming releases.
at node_modules/expo-permissions/build/Permissions.js:42:4 in getAsync
at node_modules/expo-permissions/build/Permissions.js:41:7 in getAsync
at node_modules/expo-permissions/build/PermissionsHooks.js:32:45 in useCallback$argument_0
at node_modules/expo-permissions/build/PermissionsHooks.js:42:26 in useEffect$argument_0

expo-permissions is now deprecated — the functionality has been moved to other expo packages that directly use these permissions (e.g. expo-location, expo-camera). The package will be removed in the upcoming releases.
at node_modules/expo-permissions/build/Permissions.js:49:4 in askAsync
at node_modules/expo-permissions/build/Permissions.js:48:7 in askAsync
at node_modules/expo-permissions/build/PermissionsHooks.js:27:45 in useCallback$argument_0
at node_modules/react-native/Libraries/Pressability/Pressability.js:691:17 in _performTransitionSideEffects
at node_modules/react-native/Libraries/Pressability/Pressability.js:628:6 in _receiveSignal
at node_modules/react-native/Libraries/Pressability/Pressability.js:524:8 in responderEventHandlers.onResponderRelease

[Unhandled promise rejection: Error: Not authorized to use background location services.]
at node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:103:50 in promiseMethodWrapper
at node_modules/@unimodules/react-native-adapter/build/NativeModulesProxy.native.js:15:23 in moduleName.methodInfo.name
at node_modules/expo-location/build/Location.js:172:11 in hasStartedLocationUpdatesAsync
at node_modules/expo-location/build/Location.js:170:7 in hasStartedLocationUpdatesAsync
at src/services/location/track.ts:16:15 in isTracking
at src/services/location/track.ts:15:7 in isTracking
at src/services/location/index.ts:58:4 in useEffect$argument_0

Additional context

Have also downloaded from app store and it does seem to work there - what commit is the app store version associated to?

josh-gree commented 3 years ago

@byCedric FYI - if I use the QR from (https://staging.expo.dev/@bycedrictest/office-marathon - which I found in the logcat output for the app store app it works)

josh-gree commented 3 years ago

Needs cleaning up some UI nastyness - but it works in terms of permissions etc

diff --git a/app.json b/app.json
index e0f477e..2a39e9f 100644
--- a/app.json
+++ b/app.json
@@ -28,8 +28,7 @@
       "package": "com.bycedric.officemarathon",
       "permissions": [
         "ACCESS_COARSE_LOCATION",
-        "ACCESS_FINE_LOCATION",
-        "ACCESS_BACKGROUND_LOCATION"
+        "ACCESS_FINE_LOCATION"
       ]
     },
     "web": {
diff --git a/src/screens/onboarding.tsx b/src/screens/onboarding.tsx
index 78f56fe..e0f09c9 100644
--- a/src/screens/onboarding.tsx
+++ b/src/screens/onboarding.tsx
@@ -1,38 +1,12 @@
 import { useNavigation } from '@react-navigation/native';
-import * as Location from 'expo-location';
-import React, { useCallback, useEffect, useState } from 'react';
-import { View } from 'react-native';
+import { LOCATION_FOREGROUND, usePermissions } from 'expo-permissions';
+import React, { useCallback, useEffect } from 'react';

 import { Box, Button, Spinner, Title, Paragraph } from '../providers/theme';

-
-const getfg = async (setfg) => {
-  let { status } = await Location.requestForegroundPermissionsAsync();
-  console.log(`FG PERMISSIONS -> ${status}`);
-  if (status === "granted") {
-    console.log(`SETTING STATE -> ${true}`);
-    setfg(true);
-  } else {
-    console.log("FG PERMISSIONS DENIED");
-  }
-};
-
-const getbg = async (setbg) => {
-  let { status } = await Location.requestBackgroundPermissionsAsync();
-  console.log(`BG PERMISSIONS -> ${status}`);
-
-  if (status === "granted") {
-    console.log(`SETTING STATE -> ${true}`);
-    setbg(true);
-  } else {
-    console.log("BG PERMISSIONS DENIED");
-  }
-};
-
 export const OnboardingScreen: React.FC = () => {
   const navigation = useNavigation();
-  const [fg_permission, setfg] = useState(false)
-  const [bg_permission, setbg] = useState(false)
+  const [permission, askPermission] = usePermissions(LOCATION_FOREGROUND);

   const onContinue = useCallback(() => {
     navigation.navigate('Distance');
@@ -41,12 +15,12 @@ export const OnboardingScreen: React.FC = () => {
   useEffect(() => {
     // Only redirect on first render or permission change,
     // not when users go back to this screen.
-    if (fg_permission && bg_permission) {
+    if (permission?.granted) {
       onContinue();
     }
-  }, [fg_permission,bg_permission]);
+  }, [permission?.granted]);

-  if (fg_permission && bg_permission) {
+  if (permission?.granted) {
     return (
       <Box variant='page'>
         <Box>
@@ -64,12 +38,9 @@ export const OnboardingScreen: React.FC = () => {
         <Title>We need your permission</Title>
         <Paragraph>To monitor your office marathon, we need access to your location.</Paragraph>
       </Box>
-      {!(fg_permission && bg_permission)
-        ? <View>
-          <Button onPress={() => getfg(setfg)}>Grant permission FG</Button>
-          <Button onPress={() => getbg(setbg)}>Grant permission BG</Button>
-          </View> : null
-
+      {!permission
+        ? <Spinner />
+        : <Button onPress={askPermission}>Grant permission</Button>
       }
     </Box>
   );
josh-gree commented 3 years ago

NB: It doesn't actually work anymore with the above changes :-( - This is exactly the issue I have faced in my own development and it is soooo annoying I wish someone from expo could help! The background work now never happens - the app starts the correct permission dialogues come up - settings shows the app has always for location permission - while the app is open there is a push notification saying locations are being collected - as soon as the app is closed the notification goes away - do I need to go back to an older version of the expo sdk? I am using 4.10.1 - this clearly works somehow (since the app store version does) but as it stands I am at my wits end getting background locations to work!