Sourav Singh
Sourav Singh

Reputation: 95

React Native - Geolocation without timeout in Foreground service

Facing this issue and not able to find a solution for this. My objective is to track the user's location every 30 seconds to 60 seconds with a foreground service running (with a notification) so that the user knows his location is being tracked.

The foreground service is working fine, I used '@supersami/rn-foreground-service' npm packagae https://www.npmjs.com/package/@supersami/rn-foreground-service. the foreground task is running very consistently even from the app is killed or closed.

So I thought of adding the geolocation fetch inside this task to get the location of the device. I am using 'react-native-geolocation-service' npm package https://www.npmjs.com/package/react-native-geolocation-service.

After probably 30-40 seconds of killing the app. The geolocation API stops fetching the location and starts throwing an error Location request timed out. Why is this happening? And how can i stop this from happening?

Why do we have a timeout for fetching the GPS location? And with no timeout, the onError callback function doesn't get called, below is the code.

ReactNativeForegroundService.add_task(
          () => {
            Geolocation.getCurrentPosition(
              (position) => {
                console.log(position);
              },
              (error) => {
                // See error code charts below.
                console.log(error.code, error.message);
              },
              {enableHighAccuracy: true, timeout: 1000, maximumAge: 10000},
            );
          },
          {
            delay: 100,
            onLoop: true,
            taskId: 'taskid',
            onError: (e) => console.log('Error logging:', e),
          },
        );

My objective is to get the GPS coordinates with a foreground service running in any Android Device. Please help me out here, if I am doing anything wrong or missing out anything vital.

Upvotes: 3

Views: 2661

Answers (1)

Takayuki
Takayuki

Reputation: 41

Try to add android:foregroundServiceType="location" at <service> in AndroidManifest.xml

<service android:name="com.supersami.foregroundservice.ForegroundService" android:foregroundServiceType="location" ></service>

<service android:name="com.supersami.foregroundservice.ForegroundServiceTask" android:foregroundServiceType="location" ></service>

Upvotes: 4

Related Questions