foxriver76 / ioBroker-Visu-App

This is the issue tracker for the "ioBroker Visu" Android and iOS App
7 stars 0 forks source link

Geofencing - Android background process not working #41

Open cavernerg opened 1 month ago

cavernerg commented 1 month ago

Is there an existing issue for this?

Current Behavior

On my Huawei P30Pro I still have problems with geofencing. The background process does not work properly. I watch my home-geofence for testing via a simple script that pushes changes to me via pushover. The status changes only, when I open the app. All battery optimizations are disabled like they are in other apps, that have working background processes.

Expected Behavior

Geofence updates should also be possible without opening the app.

Steps To Reproduce

No response

Which OS are you using?

Android

OS Version

11

Device

P30 Pro

App Version

1.2.0

Anything else?

Maybe there is something to look at from tasker or other apps

foxriver76 commented 1 month ago

Please check with https://dontkillmyapp.com/huawei, there is not much logic the app itself can do from my current understanding.

cavernerg commented 1 month ago

That link was my recommendation ;-) I did all I could, but I still observe different behaviour in comparison to other apps

foxriver76 commented 1 month ago

Ah okay, tbh I currently have no idea where the issue could be. For technical background the app is written with react-native using Expo Location for Geofence and there is unfortunately not much I can find about problems in this direction.

But as they state grafik

The alternative would be using https://github.com/transistorsoft/react-native-background-geolocation but it is not free to use on Android and would need a yearly subscription from my side.

cavernerg commented 1 month ago

What I found was this one https://developer.android.com/develop/sensors-and-location/location/geofencing?hl=de (was searching because I thought that I maybe have to make another app unavailable for battery optimizations) Which is Using the Google play services I think.

If I understand it correctly, your version is actively pulling from locations while the native geolocations are pushed?

foxriver76 commented 1 month ago

No geofences are event based and native functionality of the device. We are not pulling anything regarding geofences, I think it is not even possible to do so with native ones.

cavernerg commented 1 month ago

The problem still persists. https://github.com/android/location-samples/issues/259 Maybe I'm just f*cked with Huawei:-/

cavernerg commented 1 month ago

Ah... Damn Huawei.... Powergenie was Back maybe through update, don't know. Need to test again for a while, sorry!

foxriver76 commented 1 month ago

Thanks for doing all the investigation! 👍

cavernerg commented 3 weeks ago

I'm still facing issues. I also involved a pixel phone, which is marked as perfect for background processes at the dontkillmyapp-site. But on both phones, geofences only get updated, when I open the app. If I find some time, I'll see if Google can help - or maybe chatGPT or Gemini 😃

cavernerg commented 3 weeks ago

I'm still facing issues. I also involved a pixel phone, which is marked as perfect for background processes at the dontkillmyapp-site. But on both phones, geofences only get updated, when I open the app. If I find some time, I'll see if Google can help - or maybe chatGPT or Gemini 😃

Hier Mal Zitat chatGPT 😅

Das Problem, dass Geofence-Updates unter Android nicht im Hintergrund empfangen werden, bis die App geöffnet wird, könnte verschiedene Ursachen haben. Hier sind einige Schritte und Best Practices, die helfen könnten:

1. Hintergrunddienste sicherstellen

Stellen Sie sicher, dass Ihre App die nötigen Berechtigungen hat, um im Hintergrund zu laufen und Standortdienste zu nutzen.

Berechtigungen in AndroidManifest.xml

Fügen Sie die folgenden Berechtigungen hinzu:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2. Battery Optimization ausschalten

Android hat aggressive Energiesparmaßnahmen, die Hintergrundaktivitäten einschränken können. Stellen Sie sicher, dass die Batterieoptimierung für Ihre App deaktiviert ist.

Manuell ausschalten

3. Headless Task für Hintergrunddienste

Expo ermöglicht die Verwendung von Headless-Tasks, um sicherzustellen, dass die App auch im Hintergrund arbeiten kann.

Beispiel:

In Ihrer index.js oder App.js:

import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';

const LOCATION_TASK_NAME = 'background-location-task';

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  if (error) {
    console.error(error);
    return;
  }
  if (data) {
    const { locations } = data;
    console.log('Received new locations:', locations);
    // Hier können Sie die Standortdaten verarbeiten
  }
});

async function startBackgroundUpdate() {
  const { status } = await Location.requestPermissionsAsync();
  if (status === 'granted') {
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
      accuracy: Location.Accuracy.BestForNavigation,
      timeInterval: 10000,
      distanceInterval: 50,
      showsBackgroundLocationIndicator: true,
    });
  }
}

startBackgroundUpdate();

4. Foreground Service

Ein Foreground Service kann verwendet werden, um sicherzustellen, dass Ihre App im Hintergrund Standortaktualisierungen erhält.

Beispiel:

import { Platform } from 'react-native';
import * as Location from 'expo-location';
import * as TaskManager from 'expo-task-manager';

const LOCATION_TASK_NAME = 'background-location-task';

if (Platform.OS === 'android') {
  Location.setNotification({
    title: 'App is using your location',
    body: 'Tap to open',
    android: {
      channelId: 'location',
      color: '#FF0000',
    },
  });
}

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  if (error) {
    console.error(error);
    return;
  }
  if (data) {
    const { locations } = data;
    console.log('Received new locations:', locations);
  }
});

async function startBackgroundUpdate() {
  const { status } = await Location.requestPermissionsAsync();
  if (status === 'granted') {
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
      accuracy: Location.Accuracy.BestForNavigation,
      timeInterval: 10000,
      distanceInterval: 50,
      showsBackgroundLocationIndicator: true,
    });
  }
}

startBackgroundUpdate();

5. Doze Mode und App Standby

Stellen Sie sicher, dass Ihre App korrekt mit Doze Mode und App Standby umgeht. Hierbei kann die Dokumentation von Android hilfreich sein.

Durch die Umsetzung dieser Schritte sollten die Geofence-Updates zuverlässiger im Hintergrund empfangen werden. Wenn das Problem weiterhin besteht, könnten spezifische Logs und Debugging-Informationen weitere Hinweise geben.

cavernerg commented 3 weeks ago

Then there is also this issue https://github.com/expo/expo/issues/25875 , which isn't really promising for android.

I also found these 2 guides, but I would assume, you already implemented it like this https://chafikgharbi.com/expo-location-tracking/ https://medium.com/readytowork-org/geofencing-in-react-native-4d8cf42fe90c