6

I'm trying to create a foreground service in MAUI (using .NET 6) for an Android app, but currently, there are no tutorials (that I could find) on achieving this.

What would be the best starting point to add a foreground service or how would you create it?

bluscience
  • 223
  • 3
  • 7

2 Answers2

10

You can create the ForegroundService \Platform\Android and then start it in the page.cs.

I have done a sample and start it successfully, you can have a try.

In the \Platform\Android\ForegroundServiceDemo:

namespace MauiAppTest.Platform.Android
{
[Service]
public class ForegroundServiceDemo : Service
{
    private string NOTIFICATION_CHANNEL_ID = "1000";
    private int NOTIFICATION_ID = 1;
    private string NOTIFICATION_CHANNEL_NAME = "notification";

    private void startForegroundService()
    {
        var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            createNotificationChannel(notifcationManager);
        }

        var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notification.SetAutoCancel(false);
        notification.SetOngoing(true);
        notification.SetSmallIcon(Resource.Mipmap.appicon);
        notification.SetContentTitle("ForegroundService");
        notification.SetContentText("Foreground Service is running");
        StartForeground(NOTIFICATION_ID, notification.Build());
    }

    private void createNotificationChannel(NotificationManager notificationMnaManager)
    {
        var channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
        NotificationImportance.Low);
        notificationMnaManager.CreateNotificationChannel(channel);
    }

    public override IBinder OnBind(Intent intent)
    {
        return null;
    }


    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        startForegroundService();
        return StartCommandResult.NotSticky;
    }
}
}

And in the page.cs:

 private void OnStartServiceClicked(object sender, EventArgs e)
{
#if ANDROID
    Android.Content.Intent intent = new Android.Content.Intent(Android.App.Application.Context,typeof(ForegroundServiceDemo));
    Android.App.Application.Context.StartForegroundService(intent);
#endif
}

Finally, add the foreground service permission into the AndroidManifest.xml:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
3

First you need to understand that MAUI it's multiplatform, so you need to know that you need to configure each specific platform that you will run your foreground service.

Understanding that you can start reading this in microsoft docs of Xamarin, and adapt those in dotnet maui (generally if we dont found info in maui, we check on xamarin docs and on github maui issues if someone get a similar issue).

Beside that, in this link it's an example of how to implement foreground service on android with maui (in spanish).

Leandro Toloza
  • 1,655
  • 1
  • 6
  • 24
  • Can I use same approach to periodically query REST API and bring application to focus even it was in background? – Vladimir B Nov 17 '22 at 20:32
  • Wonder what does TypeDataSync means in `[Service(ForegroundServiceType = Android.Content.PM.ForegroundService.TypeDataSync)]`? I couldn't find any description anywhere. – Artur Wyszomirski Dec 07 '22 at 18:21
  • Thanks for the link to the spanish example. It is simple, clear and it has the code in GitHub. A very good point to start. – Álvaro García May 04 '23 at 18:20