I refereed this answer and created the foreground service like below:
ForegroundServiceDemo.cs
using Android.App;
using Android.Content;
using Android.OS;
using AndroidX.Core.App; // Make sure you have Xamarin.AndroidX.Core NuGet package
using Resource = Microsoft.Maui.Resource;
namespace ProjectName.Platforms.Android
{
[Service(Exported = true, Name = "com.ProjectName.ForegroundServiceDemo")]
public class ForegroundServiceDemo : Service
{
private const string NOTIFICATION_CHANNEL_ID = "1000";
private const int NOTIFICATION_ID = 1;
private const string NOTIFICATION_CHANNEL_NAME = "notification";
public override void OnCreate()
{
base.OnCreate();
}
private void StartForegroundServiceCompat()
{
var notificationManager = GetSystemService(NotificationService) as NotificationManager;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
CreateNotificationChannel(notificationManager);
}
var notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetAutoCancel(false)
.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.ic_notification) // make sure appicon exists
.SetContentTitle("Foreground Service")
.SetContentText("Foreground service is running");
if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
{
StartForeground(NOTIFICATION_ID, notification, ForegroundService.TypeLocation);
}
else
{
StartForeground(NOTIFICATION_ID, notification);
}
}
private void CreateNotificationChannel(NotificationManager notificationManager)
{
if (notificationManager == null) return;
var channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
NotificationImportance.Low)
{
Description = "Foreground service notification"
};
notificationManager.CreateNotificationChannel(channel);
}
public override IBinder OnBind(Intent intent)
{
return null; // Not a bound service
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
StartForegroundServiceCompat();
return StartCommandResult.NotSticky;
}
}
}
Calling it from a ContentPage:
#if ANDROID
using Android.Content;
using Android.OS;
using ProjectName.Platforms.Android; // Replace with the actual namespace containing ForegroundServiceDemo
#endif
#if ANDROID
var intent = new Intent(Android.App.Application.Context, typeof(ForegroundServiceDemo));
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
Android.App.Application.Context.StartForegroundService(intent);
}
else
{
Android.App.Application.Context.StartService(intent);
}
#endif
AndroidManifest:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<application
---------
<service
android:name="com.ProjectName.ForegroundServiceDemo"
android:exported="true"
android:foregroundServiceType="location" />
</application>
Important:
Set the Name
in the Service Attribute of class which must be the same android:name
in AndroidManifest.xml.
<service android:name="com.ProjectName.ForegroundServiceDemo"
[Service(Exported = true, Name = "com.AlertBuddies.ForegroundServiceDemo")]
Otherwise you will face below exception:
Java.Lang.IllegalArgumentException: 'foregroundServiceType 0x00000008 is not a subset of foregroundServiceType attribute 0x00000000 in service element of manifest file'