It’s not well documented and I had to scrape stuff together from several sources and post on a couple of different forums but I have a working foreground service that puts up a notification in the notification tray that is tappable to reopen the app after you close it. Here is the code to enable it by file in your project:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
They are out of order for some reason but first, you want to create the interface in the root of your project called IService.cs with two methods Start and Stop. Just copy the example and adjust the namespace. After that is done, in your android folder create the foreground class, name it what you want but use the inherited interface and Service class. Once that is done, edit MauiProgram.cs to add the entries I have in the example for the service, the property, and the two builder services, and then end as I have it where it calls those services and then calls app. On your home page make a property referencing the service and then also a reference to them in the constructor as well to initialize the service.
At this point, you can either call it through a button or have it launch automatically however you want it from that page or whichever page you choose.
Now I am assuming you are only targeting Android in your project, if you are not then your need to create dummy classes in your other project folders that reference the IService interface so that there are no issues and then in whichever page you choose to launch the service from use:
#if ANDROID #ENDIF around the Service.Start()/Service.Stop() to isolate to just Android or if you were smart and blackhole the interface references in your other classes’ implementations then don’t worry about it 🙂
John
Edit!
You need one more thing if you’re targeting Android 13 and up. That would be the Post Notification permission and getting that from the user. Currently .NET MAUI essentials doesn’t have that built in but if you dig into the docs on Microsoft you’ll find out how to create an extension class to add it. Also, in your AndroidManifest.xml you need to add the following:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Then just do like normal before starting the service, check for the notification permission and request it if it’s not granted.
Edit 2!
If you’re building a Shell App and you see a crash when bringing the app off the back stack then here is the work around!
In your MainActivity.cs add Launchmode = Launchmode.SingleTop where I have it in the example below and it should go away.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters