Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is called foreground #125

Open
Rossella-Mascia-Neosyn opened this issue Oct 9, 2024 · 3 comments
Open

Is called foreground #125

Rossella-Mascia-Neosyn opened this issue Oct 9, 2024 · 3 comments

Comments

@Rossella-Mascia-Neosyn
Copy link

Describe the bug
When the app starts I call the watch but it is called in the foreground and not in the background

To Reproduce

import { useRef } from 'react';

import { BackgroundGeolocationPlugin, Location } from '@capacitor-community/background-geolocation';

import { registerPlugin } from '@capacitor/core';

import { GeolocationData, IGeolocation } from '../geolocation.type';

const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>('BackgroundGeolocation');

export const useNativeGeolocationBackground = (): IGeolocation => {
  const watchIdRef = useRef<string | null>(null);

  const startGeolocation = async () => {
    watchIdRef.current = await BackgroundGeolocation.addWatcher(
      {
        backgroundMessage: 'App is tracking your location in background',
        backgroundTitle: 'Background Tracking',
        requestPermissions: true,
        stale: false,
        distanceFilter: 50,
      },
      (position, error) => {
        if (error) {
          if (error.code === 'NOT_AUTHORIZED') {
            if (window.confirm('Location access is denied. Open settings to grant access?')) {
              BackgroundGeolocation.openSettings();
            }
          }
          console.error('Error during background tracking', error);
        }

        if (position) {
          const { latitude, longitude, accuracy, time } = position;
          console.log('Background position:', latitude, longitude);
          return {
            latitude,
            longitude,
            accuracy,
            timestamp: time,
          };
        } else {
          throw new Error('Position is undefined');
        }
      },
    );
  };

  const startTracking = async () => {
    try {
      console.info('Starting background geolocation');
      startGeolocation();
    } catch (error) {
      console.error('Error starting background geolocation', error);
    }
  };

  const stopTracking = async () => {
    try {
      if (watchIdRef.current !== null) {
        console.info('Stopping background geolocation');
        await BackgroundGeolocation.removeWatcher({ id: watchIdRef.current });
        watchIdRef.current = null;
      }
    } catch (error) {
      console.error('Error stopping background geolocation', error);
    }
  };

  const getCurrentPosition = async (): Promise<GeolocationData> => {
    let lastLocation: Location | undefined;

    try {
      const id = await BackgroundGeolocation.addWatcher(
        {
          requestPermissions: false,
          stale: true,
        },
        location => {
          lastLocation = location || undefined;
        },
      );

      if (lastLocation) {
        await new Promise(resolve => setTimeout(resolve, 10000));
        await BackgroundGeolocation.removeWatcher({ id });
        return {
          coords: {
            ...lastLocation,
          },
          timestamp: Date.now(),
        };
      }
      throw new Error(`Error current position is: ${lastLocation}`);
    } catch (error) {
      throw new Error(`Error while getting current position: ${error}`);
    }
  };

  return { startTracking, stopTracking, getCurrentPosition };
};

Expected behavior
the expected behavior should be that it is only called in the background

Screenshots

issue-background.1.mp4

Desktop (please complete the following information):

  • OS: IOS
  • Browser: chrome
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: Moto (motorola) g73 5g (XT2237-2)
  • OS: Android 14
  • Browser chrome
  • Version [e.g. 22]
@diachedelic
Copy link
Collaborator

That is correct. When the backgroundMessage options is specified, location updates are delivered in both the foreground and the background. You could call App.getState() and ignore location updates that arrive in the foreground?

@Rossella-Mascia-Neosyn
Copy link
Author

That is correct. When the backgroundMessage options is specified, location updates are delivered in both the foreground and the background. You could call App.getState() and ignore location updates that arrive in the foreground?

ahh okok very clear, thank you.
I have other questions:

  1. This plugin is not a background runner right? and consequently does not have the limitation that sends the location every 15 minutes?
    https://capacitorjs.com/docs/apis/background-runner#limitations-of-background-tasks

  2. I can’t figure out why there is no local notification, what could be my mistake?

@diachedelic
Copy link
Collaborator

  1. No.
  2. Probably the notification icon is misconfigured. Make sure you follow the Android configuration instructions exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants