Foreground services do not work in Expo Go nor Emulator

Hello,

I have been following the documentation here, relating to running a foreground service in Expo. This is for the gathering of location on Android devices when an app is minimised every X seconds.

From Android 10 + this requires a foreground service.

The expo documentation above states running a foreground service will work in the Android Emulator and an Android device.

Yet foreground services written using the code in the documentation above do not work when an app is launched using the android emulator, via the command expo start, for devices running on Android 10 or above. Minimise the app, no foreground notification, and no GPS updates are logged.

On Android 9 or lower, there is no requirement for a foreground service, and so location will continue to be gathered when an app is minimised.

The codebase above does work when the app is compiled however - either using npx expo run:android or using Expo EAS and downloading the APK to run locally on a physical device.

This leads me to think there is something about the expo package which is not compatible with foreground services, at least prior to there being a real ‘build’ of the android project with the /android/ folder etc.

Is this a known limitation or bug? I note a similar forum comment here, no replies.

Final comment: I suspect the probable cause (given it only affects Android 10+) is that the requirement for a foreground service is not compatible with the ExpoGo app - or Expo’s Managed Workflow - whatever the terminology is to refer to the stage prior to the compilation of a separate /android/ folder via expo pre-build and expo run (*Expo eject is now deprecated as of Jan 2023 so I will not use that terminology). I do not have enough familiarity with Expo to be using the right words here.

Photos are clearer than my words:

I am attaching a screenshot of the app. On the left I am running in an emulator via expo start, minimised the app, no foreground service, no gps updates.

On the right is the same codebase, running on a real device and composed using npx expo run:android - as you can see, minimise the app, and a foreground service exists, GPS continues to work when app is minimised.

Any comments appreciated.

This is not intended to start a discussion about background services.

Update for anyone else stuck on this: appears to work correctly once expo prebuild and expo run are used in conjunction. While in managed workflow foreground service did not work on emulator nor on phone. Once /android/ folder is created via expo prebuild then expo run, both devices foreground service began working with a notification.

Hi @corkysurprise

I suspect you were trying this in the Expo Go app? I haven’t tried using a foreground service in Expo Go myself, but you might need to create a development build and use that instead of Expo Go.

I think you might be right, except that I still consider creating a development build to use instead of Expo Go to be part of the managed workflow. There should be no need to prebuild or use npx expo run.

npx expo run actually runs npx expo prebuild in the background. But sometimes you might need to use npx expo prebuild --clean to regenerate the native projects from scratch.

Try: eas build -p platform --profile development and use the resulting APK instead of Expo Go.

It should work without having to generate the native projects. i.e. should work in the managed workflow as long as you create a dev build.

My opinion is that generating the native projects with npx expo prebuild (or npx expo run) complicates things because now changes to app.json don’t take effect unless you regenerate the native project, etc.

See expo.fyi/prebuild-cleanup if you want to revert to the managed workflow. tl;dr delete your android and ios directories.