1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Background API Calls Stopping When Screen Is Off on Some Android Devices Despite...

Discussão em 'Mobile' iniciado por Stack, Outubro 7, 2024 às 09:52.

  1. Stack

    Stack Membro Participativo

    I am working on an flutter app, where we need to send the user's location (latitude and longitude) to our server every 20-30 seconds in the background. The server checks if the user is within a certain range. This works well on some devices but has inconsistent behavior on others.

    The issue:

    On some phones, background API calls stop when the screen is turned off. On these devices, the app only works as expected if we manually tweak multiple settings such as background restrictions, network restrictions, and battery optimizations.

    Here’s a summary of my setup:

    Platform: Android Background process: Running in a foreground service with a periodic WorkManager for API calls.

    Any insights or solutions to ensure more consistent background behavior across different devices would be appreciated!

    What I've tried:

    • Ensured the app has all the necessary permissions for background location access (ACCESS_FINE_LOCATION, ACCESS_BACKGROUND_LOCATION, etc.).
    • Enabled foreground services to improve reliability of background tasks.
    • Requested users to disable battery optimizations manually.
    • The problem: Even after applying these fixes, some devices still stop the API calls when the screen is turned off. Devices like Pixel and Samsung work smoothly, but devices like Xiaomi, Oppo, and Vivo require multiple manual tweaks for it to work, making it inconsistent.

    My questions are:

    • What is the recommended way to ensure reliable background API calls across different devices, even when the screen is off?
    • How can I ensure the app does not rely on users having to tweak their device settings like disabling battery optimizations or network restrictions?
    • Are there any best practices or workarounds to handle background API calls in a more consistent way across different Android versions and manufacturers?

    Continue reading...

Compartilhe esta Página