michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.28k stars 220 forks source link

Does not work in Headless JS #243

Closed valery-lavrik closed 10 months ago

valery-lavrik commented 1 year ago

Good afternoon! In the simple mode of the application, everything works well.
Permissions have already been requested and received.

Now I want the Headless (https://reactnative.dev/docs/headless-js-android) script to work with coordinates, but I always get null

Here are excerpts from the code:

index.js

...
import { BackgroundTask } from './src/utils/BackgroundHeadlessJSTask';

AppRegistry.registerHeadlessTask('Sync', () => BackgroundTask);
AppRegistry.registerComponent(appName, () => App);

BackgroundHeadlessJSTask.js

export const BackgroundTask = () => {
    Geolocation.setRNConfiguration({
        skipPermissionRequests: true,
        locationProvider: 'playServices',
    });

    Geolocation.getCurrentPosition(
        (position) => {
            console.log('position', position);
        },
        (error) => {
            // mReactContext.getCurrentActivity() returned null but should be non-null in getCurrentLocationData
            console.log(error);
        },
        {
            timeout: (1000 * timeout),
            maximumAge: (1000 * 60 * 2),
            enableHighAccuracy: true,
        }
    );
};

After analyzing the situation, I began to receive an error:

mReactContext.getCurrentActivity() returned null but should be non-null in getCurrentLocationData

Error in file: node_modules@react-native-community\geolocation\android\src\main\java\com\reactnativecommunity\geolocation\PlayServicesLocationManager.java

image

I do not know what to do...

valery-lavrik commented 1 year ago

any ideas how to fix the problem?

JB712 commented 1 year ago

I edited the file you suggested to change in your PR #254 but it still doesn't work when the app is not focused (Location request timed out).

I don't directly use headless-js-android but I use react-native-background-actions which relies on it. With all the permissions checked I launch a background task that chronically check for geolocalisation, but as soon as I leave my app in background I can't collect it. I use this option set: enableHighAccuracy: true, timeout: 20000, maximumAge: 10000,

Do you have any clue on that problem ?

valery-lavrik commented 1 year ago

Everything worked for me. To do this, I made my own fork https://github.com/valery-lavrik/react-native-geolocation

But, in order for everything to work properly, I had to: 1) enable autorun of the application in the android settings (my application should start itself after reboot) 2) for my application, in the android settings you need to specify "allow access to geolocation in any mode" (I have Redme 10C)

It is this switch from point 2 that prevents the application from using GPS when the application is not running

image

valery-lavrik commented 1 year ago

and more. So that the timeout does not fall (sometimes this happens) I made a wrapper with my artificial timeout

image

JB712 commented 1 year ago

My bad, I did forgot to ask for background geolocation permission. You're correction is working great once this one is collected 😅 But thank you for your response and time (it still might help someone else 😃)

michalchudziak commented 10 months ago

Shipped in version 3.1.0