Hello,
I am using
@capacitor
/geolocation to get the position of the user. Here is the code:
import { ForegroundService } from '@capawesome-team/capacitor-android-foreground-service';
import { Position, Geolocation } from '@capacitor/geolocation';
async startForegroundService() {
await ForegroundService.startForegroundService({
buttons: [
id: 1,
title: 'Stop',
body: 'This is the body of the notification',
title: 'This is the title of the notification',
id: 12398473,
smallIcon: 'push_icon',
}).then(async () => {
this.watchId = await Geolocation.watchPosition(
{ enableHighAccuracy: true, timeout: 5000 },
(position, err) => {
if (err) {
console.log(err);
this.addPosition(position)
It only works when the application is in the foreground, but when I put it in the background, the callback in watchPosition is not called anymore. It works again when I go back in the application.
I also tried to use Geolocation.getCurrentPosition():
async startForegroundService() {
await ForegroundService.startForegroundService({
buttons: [
id: 1,
title: 'Stop',
body: 'This is the body of the notification',
title: 'This is the title of the notification',
id: 12398473,
smallIcon: 'push_icon',
}).then(async () => this.timeout());
timeout() {
console.log("TIMEOUT");
if (this.isCurrentRide$.getValue()) {
setTimeout(async () => {
console.log('INSIDE_TIMEOUT');
try {
const pos = await Geolocation.getCurrentPosition({ timeout: 5000 });
console.log('POS', pos);
} catch (error) {
console.error("ERROR", error);
this.timeout();
}, 1000);
Unlike the first method, this one is more hazardous as it does not work sometimes. But, when it does, it works correctly when the application is open but when I put it in the background, it can’t get the position anymore (until I open the application again). It continues to call getCurrentPosition() but it always returns an error saying that the position is unavailable:
ERROR Error: location unavailable
I have given all the necessary permissions:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
I am testing with an Android Studio emulator (Pixel 7, API 33).
Hello, the only thing you are missing is
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
and enable it to always allow from the configuration of your app so that it is persistent, I have replicated your code and I have executed it successfully.
Although I have used moveToForeground, it is not necessary to implement it.
Point out that the intervals do not work correctly after a while, I will leave that to you. Regards
Coding in Vuejs and JS
import { Capacitor } from '@capacitor/core';
import { ForegroundService } from '@capawesome-team/capacitor-android-foreground-service';
import { Geolocation } from '@capacitor/geolocation';
import { LocalNotifications } from '@capacitor/local-notifications'
const startForegroundService = async () => {
if (Capacitor.getPlatform() !== 'android') {
return false;
await ForegroundService.startForegroundService({
buttons: [
id: 1,
title: 'Stop',
body: 'This is the body of the notification',
title: 'This is the title of the notification',
id: 12398473,
smallIcon: 'push_icon',
}).then(async () => {
await moveToForeground();
const stopForegroundService = async () => {
if (Capacitor.getPlatform() !== 'android') {
return false;
await ForegroundService.stopForegroundService();
const moveToForeground = async () => {
await ForegroundService.moveToForeground()
.then(async () => {
setInterval(async () => {
await printCurrentPosition()
}, 15 * 1000)
const printCurrentPosition = async () => {
const hasPermission = await Geolocation.checkPermissions();
if (hasPermission.location === 'granted') {
const coordinates = await Geolocation.getCurrentPosition();
LocalNotifications.schedule({
notifications: [{
id: 1,
title: 'Coords',
body: `${coordinates.coords.altitude} and ${coordinates.coords.latitude}`
} else {
const permission = await Geolocation.requestPermissions({ askAgain: true });
if (permission.location === 'granted') {
} else {
console.error('Location permission denied');
Hello franchesco12sr,
Thank you for your reply.
I have tried what you advised me to do, but unfortunately, it is still not working.
I have added <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
to the AndroidManifest.xml
file as you said, but to no avail.
I have also tried to use your code as you told me it is working for you, but even with that, nothing works as I would like.
It worked once when I changed the authorisation for the location of the application (from “allow only when using the app” to “allow all the time”). To be sure that it was not just a stroke of luck, I restarted the emulator, but then it was worse.
The getCurrentPosition
method is not working anymore (even in foreground mode), always returning Error: location unavailable
.
As for watchPosition
, it is still the same as before, only working when the application is open and at the forefront.
Would you have any other idea why these functions are not working as intended?
Regards.