Forum Xamarin.Android

Hide "<AppName> is using battery" notification

arturmullerarturmuller Member ✭✭✭

I have an app which are running a background service. The background service polls some values each 30 seconds. When I run my app (and in turn the service) I get the notification from Android that " is using battery" notification.

Can I suppress or hide this notification? If yes, how?

I do realize that my background service is using battery in the background but judging from my benchmarks and the intended app usage, its a relatively low amount of battery usage.

Thank you

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Starting in Android 8.0 (API level 26), an Android application no longer have the ability to run freely in the background. When an application moves into the background, Android will grant the app a certain amount of time to start and use services. Once that time has elapsed, the app can no longer start any services and any services that were started will be terminated. See the tutorial.

    If you want to keep the app running in background, please use Foreground Service. But a foreground service must provide a Notification that Android will display as long as the service is running.

    If you want to perform scheduled background work, try using the Firebase Job Dispatcher library.

  • arturmullerarturmuller Member ✭✭✭

    Hi @Jarvan

    What I am actually using is this: https://github.com/xamarin/mobile-samples/tree/master/BackgroundLocationDemo

    So there is no way to modify that sample to remove/hide the notification that the background service is using battery?

    Best regards

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited March 25

    So there is no way to modify that sample to remove/hide the notification that the background service is using battery?

    We can see that the sample uses a foregroundService on Android 8 or higher. Since Android 8, it is not possible to automatically hide a foreground service notification. To hide the notification, we can guide the users to open the notification channel configuration interface to hide it manually.

    public static void StartLocationService()
    {
        // Starting a service like this is blocking, so we want to do it on a background thread
        new Task(() =>
        {
            // Start our main service.
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                Application.Context.StartForegroundService(new Intent(Application.Context, typeof(LocationService)));
            }
            else // For older versions, use the traditional StartService() method
            {
                Application.Context.StartService(new Intent(Application.Context, typeof(LocationService)));
            }
            ...
        }).Start();
    }
    

    Check the link:
    https://stackoverflow.com/questions/10962418/how-to-startforeground-without-showing-notification

  • arturmullerarturmuller Member ✭✭✭

    And there is no way to modify that sample to use another service technique, other location API or something in order to hide the notification? I saw that these samples are actually from 2018, is it still the preferred way to track/poll GPS position of an user? Or is there some newer way?

  • JarvanJarvan Member, Xamarin Team Xamurai

    Have you tried the Firebase Job Dispatcher library which could help perform scheduled background work?

    Check the tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/platform/firebase-job-dispatcher

  • arturmullerarturmuller Member ✭✭✭

    No, I did not look into that. But I suppose that is not available for iOS (or well integrated in Xamarin). Would I get rid of the notification by using that?

  • JarvanJarvan Member, Xamarin Team Xamurai

    there is no way to modify that sample to use another service technique, other location API or something in order to hide the notification?

    Since Android 8, it is not possible to automatically hide a foreground service notification. To hide the notification, we can
    guide the users to open the notification channel configuration interface to hide it manually.

    Check the link:
    https://stackoverflow.com/questions/10962418/how-to-startforeground-without-showing-notification/

Sign In or Register to comment.