Location.startLocationUpdatesAsync don't start in real device with apk

Please provide the following:

  1. SDK Version: 35.0.0 (Expo)
  2. Platforms(Android/iOS/web/all): Android real device

Hello guys…
I’m stuck and needing assistance with TaskManager.defineTask & Location.startLocationUpdatesAsync I’ve follow the instructions in docs, putting defineTask in root App.js, and the startLocation in the right place (I think), and all works fine when the app runs in debug via usb on real device or emulator. The locations are displayed, the task runs fine, BUT when I generate the apk, install in my cellphone (xiaomi redmi 4x android 7) and the cell of a friend (MOTO G5 android 8), nothing happens about this background task. Its like TaskManager.defineTask and Location.startLocationUpdatesAsync never been called…
Anyone passed by this bug?

My codes:
root App.js:

import React from 'react';
import { StatusBar, ToastAndroid } from 'react-native';

import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import * as TaskManager from 'expo-task-manager';
import { format } from 'date-fns';

import { store, persistor } from './src/store';
import App from './src/App';

export default function Index() {
  return (
    <>
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <StatusBar barStyle="light-content" backgroundColor="#3d3f4a" />
          <App />
        </PersistGate>
      </Provider>
    </>
  );
}

console.disableYellowBox = true;

if (!TaskManager.isTaskDefined('locationTask')) {
  TaskManager.defineTask('locationTask', async ({ data: { locations }, error }) => {
    if (error) {
      // check `error.message` for more details.
      console.log(error.message);
      return;
    }
    // console.log(format(1570647632481, 'dd/MM/yyyy HH:mm:ss'));
    // console.log(await TaskManager.getTaskOptionsAsync('locationTask'));

    ToastAndroid.show(
      `GPS lat/lon: ${locations[0].coords.latitude}/${locations[0].coords.longitude} - ${format(
        locations[0].timestamp,
        'HH:mm:ss'
      )}`,
      ToastAndroid.SHORT
    );

    console.log('TaskManager.defineTask');
    console.log('Received new locations', locations);
    console.log('#####################################');
  });
}

src/App.js:

import React, { useEffect } from 'react';
import { useSelector } from 'react-redux';
import createRouter from './routes';
import { format } from 'date-fns';

import { useDispatch } from 'react-redux';

import setGPS from './store/modules/gps/actions'; // ../../store/modules/nfs/actions';
// import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
import * as Permissions from 'expo-permissions';

export default function App() {
  const dispatch = useDispatch();

  async function getLocationAsync() {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    if (status !== 'granted') {
      console.error('Permission to access location was denied');
      return;
    }
    console.log('getting local');
    console.clear();
    const location = await Location.startLocationUpdatesAsync('locationTask', {
      accuracy: Location.Accuracy.High,
      timeInterval: 3000,
      showsBackgroundLocationIndicator: true,
      foregroundService: {
        notificationTitle: 'helloww bg geoloc',
        notificationBody: 'body teste nofif',
        notificationColor: '#EEE',
      },
    });
    console.log('loc 1', location);
    const location2 = await Location.getCurrentPositionAsync({});
    console.log('current->', location2);

    dispatch(setGPS(location2));
  }

  // getLocationAsync();
  useEffect(() => {
    getLocationAsync();
    return () => {
      TaskManager.unregisterAllTasksAsync();
    };
  }, []);

  const signed = useSelector(state => state.user.auth);
  const Routes = createRouter(signed);
  return <Routes />;
}

i’d tested the cod in other 2 emulators with android 7.11 and 6.0 and this bug occurs again, but a little bit diff in 6.0… the location works but not in background.

any idea to do this BG task in android 7+?

with adb logcat in android 8.1 emulator, i’d catched this logs when I ‘mannually’ run the bg task + geoloc:


10-14 20:57:01.891  2479  2864 I ReactNativeJS: getting local
10-14 20:57:01.895  2479  2865 D i       : WARNING: getPackageName called on ScopedContext
10-14 20:57:01.903  2479  2550 W GooglePlayServicesUtil: Google Play Store is missing.
--------- beginning of system
10-14 20:57:01.908  1643  1655 W ActivityManager: Unable to start service Intent { cmp=host.exp.exponent/abi35_0_0.expo.modules.location.services.LocationTaskService (has extras) } U=0: not found
10-14 20:57:01.919  2479  2550 I chatty  : uid=10059(host.exp.exponent) GoogleApiHandle identical 1 line
10-14 20:57:01.936  2479  2550 W GooglePlayServicesUtil: Google Play Store is missing.
10-14 20:57:01.975  1398  1398 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 40960
10-14 20:57:01.982  1398  1398 I chatty  : uid=1000(system) allocator@2.0-s identical 1 line
10-14 20:57:01.986  1398  1398 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 40960
10-14 20:57:02.002  2479  2751 D EGL_emulation: eglMakeCurrent: 0x756e7183dae0: ver 3 1 (tinfo 0x756e7180dd40)
10-14 20:57:02.067  1408  1408 D SurfaceFlinger: duplicate layer name: changing host.exp.exponent/host.exp.exponent.experience.ExperienceActivity to host.exp.exponent/host.exp.exponent.experience.ExperienceActivity#1
10-14 20:57:02.077  1398  1398 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1736704
10-14 20:57:02.156  1398  1398 I chatty  : uid=1000(system) allocator@2.0-s identical 1 line
10-14 20:57:02.216  1398  1398 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1736704
10-14 20:57:02.279  2479  2751 D EGL_emulation: eglMakeCurrent: 0x756e7183dae0: ver 3 1 (tinfo 0x756e7180dd40)
10-14 20:57:01.910  1643  1655 W ActivityManager: Unable to start service Intent { cmp=host.exp.exponent/abi35_0_0.expo.modules.location.services.LocationTaskService (has extras) } U=0: not found
10-14 20:57:03.978  1643  1643 W WindowManager: removeWindowToken: Attempted to remove non-existing token: android.os.Binder@2177142
10-14 20:57:02.320  2479  2751 D EGL_emulation: eglMakeCurrent: 0x756e7183dae0: ver 3 1 (tinfo 0x756e7180dd40)
10-14 20:57:05.086  1391  1555 W audio_hw_generic: Not supplying enough data to HAL, expected position 1981147 , only wrote 1828800

And when I try to getCurrentLocation:


10-14 21:00:40.972  1406  1406 I boot-pipe: done populating /dev/random
10-14 21:00:46.311  1391  2668 W audio_hw_generic: Not supplying enough data to HAL, expected position 1982392 , only wrote 1982160
10-14 21:00:46.353  1398  1398 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 139264
10-14 21:00:46.359  1398  1398 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 139264
10-14 21:00:46.365  1398  1447 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 139264
10-14 21:00:46.379  1408  1408 D SurfaceFlinger: duplicate layer name: changing host.exp.exponent/host.exp.exponent.experience.ExperienceActivity to host.exp.exponent/host.exp.exponent.experience.ExperienceActivity#1
10-14 21:00:46.416  1398  1447 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1736704
10-14 21:00:46.423  1398  1447 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1736704
10-14 21:00:46.429  1398  1398 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1736704
10-14 21:00:46.473  2479  2751 D EGL_emulation: eglMakeCurrent: 0x756e7183dae0: ver 3 1 (tinfo 0x756e7180dd40)
--------- beginning of system
10-14 21:00:48.346  1643  1643 W WindowManager: removeWindowToken: Attempted to remove non-existing token: android.os.Binder@d1783ec
10-14 21:00:46.724  2479  2751 I chatty  : uid=10059(host.exp.exponent) RenderThread identical 3 lines
10-14 21:00:47.951  2479  2751 D EGL_emulation: eglMakeCurrent: 0x756e7183dae0: ver 3 1 (tinfo 0x756e7180dd40)
10-14 21:00:49.492  1391  1555 W audio_hw_generic: Not supplying enough data to HAL, expected position 2286738 , only wrote 2134800

FWIW I tried to get this working yesterday and failed. I wondered if maybe Toasts don’t work from an app that’s in the background, so I tried using network requests. Now that I think about it, BackgroundFetch probably exists for a reason :laughing: so maybe just using fetch() was not a valid way to test this.

Are you sure that ToastAndroid works from a backgrounded app?