- A background/foreground/service/notification for Xamarin Android projects (its what you might consider a background service, but its called a foreground service, and includes a notification that the user can interact with and monitor).
- A Nuget package for Xamarin Forms projects.
Install Nuget package into your Xamarin Forms project...
Install-Package Xamarin.Android.Service
Add the Foreground Service Permission to your Android project's manifest file. (either through the Properties menu item on your Android project, or in the Properties directory of your Android project)
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
There are several ways that you can implement the service.
This is if you just want a simple notification that lives beyond the lifetime of your application.
// Create and start a service...
_simpleService = await SimpleServiceManager.Start("My Service", "Hello world!");
// Stop the service...
_simpleService.StopService(removeNotification: true);
If you want to perform tasks from within the service.
- Implement the ISimpleService interface.
public class MyService : ISimpleService
{
public void OnExecuteAction(string actionName, IList<string> args)
{
// Event handler for actions declared in the "SimpleServiceOptions"
if (actionName == "Action1"){ /* perform task */}
if (actionName == "Action2"){ /* perform task */}
if (actionName == "Action3"){ /* perform task */}
}
public void OnStart(SimpleServiceCore core)
{
// Called when the service starts
}
public void OnStop(IList<string> args)
{
// Called when the service is stopped
}
public void OnTapNotification()
{
// Called when the user taps the notification
}
}
- You can use the "SimpleServiceOptions" object to customize the service...
var options = new SimpleServiceOptions();
// Actions will appear as buttons in the notification.
// Pressing the buttons will raise the "OnExecuteAction" event in your ISimpleService implementation.
// You are allowed 3 actions.
options.ActionNamesAndTitles = new Dictionary<string, string>()
{
{ "Action1", "Play" },
{ "Action2", "Stop" },
{ "Action3", "Exit" }
};
- Pass your service class, and your service options in with the Start method
// Create and start a service...
_service = await ServiceManager.Start<MyService>("My Service", "Hello world!", options);
SampleApp shows you a simple way to start the service, and to update the notification.
SampleApp2 demonstrates a full implementation, and shows communication from the app to the service, and from the service to the app. It also shows examples of how to start the service automatically when the user navigates away from your application.