Skip to content

Instantly share code, notes, and snippets.

@bbenetskyy
Created April 19, 2022 06:28
Show Gist options
  • Save bbenetskyy/f5c2ff8a1d69f76e58454d9864ba2f22 to your computer and use it in GitHub Desktop.
Save bbenetskyy/f5c2ff8a1d69f76e58454d9864ba2f22 to your computer and use it in GitHub Desktop.
/// <summary>
/// Android Native Background Service
/// </summary>
[Service]
public class BackgroundService : Service
{
#region Fields
/// <summary>
/// The Id of the Service
/// </summary>
private const int SERVICE_ID = 7070;
/// <summary>
/// The string identifier for the service notification
/// </summary>
private const string SERVICE_NOTIFICATION_CHANNEL_ID = "7071";
/// <summary>
/// Background Worker
/// </summary>
private readonly IBackgroundWorker _backgroundWorker;
#endregion Fields
#region Contstoctors
public BackgroundService()
{
_backgroundWorker = Mvx.IoCProvider.Resolve<IBackgroundWorker>();
}
#endregion Contstoctors
#region Public Methods
public override IBinder OnBind(Intent intent)
{
// This service is not bindable by an external app so
// return null
return null;
}
/// <summary>
/// On Start Service Command
/// </summary>
/// <returns>Start Command Result</returns>
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
_backgroundWorker.WorkerStopped += BackgroundWorkerOnWorkerStopped;
// Build the notification for the foreground service
var notification = BuildNotification();
StartForeground(SERVICE_ID, notification);
_ = Task.Run(_backgroundWorker.BackgroundWork);
// Return a sticky result so that the service remains running
return StartCommandResult.Sticky;
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// Background Worker on Worker Stopped
/// </summary>
private void BackgroundWorkerOnWorkerStopped(object sender, EventArgs e)
{
_backgroundWorker.WorkerStopped -= BackgroundWorkerOnWorkerStopped;
StopForeground(removeNotification:true);
StopSelf();
}
/// <summary>
/// Build Service Notification Tile
/// </summary>
/// <returns>Service Notification Tile</returns>
private Notification BuildNotification()
{
// Building intent
var intent = new Intent(Application.Context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.NoUserAction);
intent.PutExtra("Title", "Message");
var pendingIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent);
var notificationBuilder = new NotificationCompat.Builder(Application.Context, SERVICE_NOTIFICATION_CHANNEL_ID)
.SetContentTitle(AppResources.Notification_Title)
.SetContentText(AppResources.Notification_Text)
.SetSmallIcon(Resource.Drawable.icon)
.SetColor(ResourcesCompat.GetColor(Application.Context.Resources, Resource.Color.colorAccent, null))
.SetOngoing(true)
.SetContentIntent(pendingIntent);
// Building channel if API version is 26 or above
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var notificationChannel = new NotificationChannel(SERVICE_NOTIFICATION_CHANNEL_ID, "MaczDostawca", NotificationImportance.Low);
notificationChannel.Importance = NotificationImportance.Low;
notificationChannel.EnableLights(true);
notificationChannel.EnableVibration(false);
notificationChannel.SetShowBadge(true);
if (Application.Context.GetSystemService(NotificationService) is NotificationManager notificationManager)
{
notificationBuilder.SetChannelId(SERVICE_NOTIFICATION_CHANNEL_ID);
notificationManager.CreateNotificationChannel(notificationChannel);
}
}
return notificationBuilder.Build();
}
#endregion Private Methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment