Search code examples
angulargeolocationnativescriptupdatesbackground-process

How to make a nativescript-angular app run in the background?


I am building a hybrid-app using nativescript and angular that, among other functionalities, should be able to send information about the current location of the user to a server using the geolocation plugin.

Everything works just fine, but I can't figure out how to keep it sending the locations (5-15 seconds updates) when the users (which will be mainly drivers) take the whole application to the background (to enter a music app for example).

I have tried a lot of different things, but I can't seem to figure out what I am looking for. More precisely, I don't know if the options I found solve the aforementioned problem or if they just take the geolocation service and process it in the background (while the app is in the foreground) in order not to interfere with the user interface.

I'd much appreciate if someone could point me in the right direction for both Android and iOS (as in what I should implement so that it works in Android and what to implement so that it works in iOS).

Thank you!

Options I found (didn't actually try some of them as I didn't understand exactly which ones make one process work in the background and which ones can help me make my app run its processes while in the background): 1. https://github.com/NativeScript/worker-loader

2. https://docs.nativescript.org/core-concepts/multithreading-model?_ga=2.61764908.1834822381.1567423754-1194537488.1566893752

3. https://github.com/NativeScript/nativescript-geolocation/blob/master/README.md

4. https://www.nativescript.org/blog/using-android-background-services-in-nativescript (i think this is deprecated because of the implementation of worker option)

5. https://github.com/NativeScript/sample-android-background-services

-


Solution

  • A Worker is different from Background Service. A work just helps you to move your load from main / UI thread into a different thread. But you need a Background Service when you want to fetch location & upload data in background even when your app is minimised / killed.

    nativescript-geolocation plugin itself has an example for fetching location in background, you may use the similar approach and upload data to server instead of showing it in a toast as in the example.

    For iOS, you should use the geo location plugin and subscribe for updates in background using Background Fetch. Instead of UIBackgroundModes as fetch you should use location.