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

Location not being shared when app is killed #256

Closed
ahunter135 opened this issue Jun 25, 2024 · 2 comments
Closed

Location not being shared when app is killed #256

ahunter135 opened this issue Jun 25, 2024 · 2 comments
Labels

Comments

@ahunter135
Copy link

ahunter135 commented Jun 25, 2024

Hello,

I followed the setup steps perfectly, but on iOS when I kill my app; it's not sending location updates to firebase.

Am I mistaken? Does this not run in the background when I kill my app? I'm assuming it does and doesn't. Like it does run, but my JavaScript listeners don't.
Would a solution be to use the post to server option and update firebase that way?


import { NgZone, Injectable } from '@angular/core';
import BackgroundGeolocation, {
  Location,
  Subscription,
} from '@transistorsoft/capacitor-background-geolocation';
import { FirebaseService } from './firebase.service';
import { Subject } from 'rxjs';
@Injectable({
  providedIn: 'root',
})
/**
 * This is the location service for the CURRENT phone user
 */
export class LocationService {
  events: any = [];
  subscriptions: Subscription[] = [];
  lastLocation: Location | null = null;
  isListening = false;
  private locationUpdateSubject = new Subject<Location>();
  public locationUpdate$ = this.locationUpdateSubject.asObservable();

  constructor(private firebaseService: FirebaseService) {}

  async prepareAndStart() {
    try {
      this.subscriptions.push(
        BackgroundGeolocation.onLocation((location) =>
          this.locationReceived(location)
        )
      );

      /// Step 2:  Ready the plugin.
      await BackgroundGeolocation.ready({
        // url: 'localhost:3000', // put a firebase on request function url here
        // params: {
        //   user_id: 1234, // put a firebase user id here so we know who's location is being pinged
        // },
        // Geolocation Config
        desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
        distanceFilter: 10,
        // Activity Recognition
        stopTimeout: 5,
        // Application config
        debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
        logLevel: BackgroundGeolocation.LOG_LEVEL_OFF,
        stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
        startOnBoot: true, // <-- Auto start tracking when device is powered-up.
      });

      BackgroundGeolocation.start().then(() => {
        this.isListening = true;
      });
    } catch (error) {
      alert(error);
    }
  }

  /**
   * Every time the location is received, we want to store it locally on lastLocation and also in the DB.
   * We are using the realtime database here. See storeUser.
   * @param location
   */
  locationReceived(location: Location) {
    this.firebaseService.storeUser(
      location.coords.latitude,
      location.coords.longitude
    );
    this.lastLocation = location;
    this.locationUpdateSubject.next(location);
  }

  getCurrentLocation() {
    BackgroundGeolocation.getCurrentPosition(
      {},
      (location) => {
        this.locationReceived(location);
      },
      (error) => {}
    );
  }
}

Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Jul 26, 2024
Copy link

github-actions bot commented Aug 9, 2024

This issue was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions bot closed this as completed Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant