From 61842a204eb6b12febfd101510f130d58e307d5d Mon Sep 17 00:00:00 2001 From: Jack Stratton Date: Tue, 20 Jul 2021 16:42:50 -0700 Subject: [PATCH] Set up a background service for notifications --- phms/BackgroundService.cs | 66 +++++++++++++++++++++++++++++++++++++++ phms/MainActivity.cs | 14 ++++++--- phms/phms.csproj | 1 + 3 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 phms/BackgroundService.cs diff --git a/phms/BackgroundService.cs b/phms/BackgroundService.cs new file mode 100644 index 0000000..8db720c --- /dev/null +++ b/phms/BackgroundService.cs @@ -0,0 +1,66 @@ +using System; +using Android.App; +using Android.Content; +using Android.OS; + +namespace phms +{ + using R = Resource; + + [Service(Enabled = true)] + public class BackgroundService : Service + { + public const string UpdateNotificationAction = "phms.backgroundservice.updatenotification"; + + private const int NotificationId = 39; + + private NotificationChannel? _notificationChannel; + private Binder? _binder; + + public override IBinder OnBind(Intent? intent) + { + return _binder ??= new Binder(); + } + + public override void OnCreate() + { + base.OnCreate(); + _notificationChannel ??= new NotificationChannel("phms", "phms", NotificationImportance.High); + + Notify("Service OnCreate"); + } + + public override StartCommandResult OnStartCommand(Intent? intent, StartCommandFlags flags, int startId) + { + var ret = base.OnStartCommand(intent, flags, startId); + + if (intent?.Action == UpdateNotificationAction) + { + Notify(intent.Extras?.GetString("message") ?? "Something important!"); + } + + return ret; + } + + private void Notify(string message) + { + if (!GetNotificationManager().NotificationChannels.Contains(_notificationChannel)) + { + GetNotificationManager().CreateNotificationChannel(_notificationChannel); + } + + var n = new Notification.Builder(this, _notificationChannel?.Id) + .SetContentTitle("phms") + .SetContentText(message) + .SetSmallIcon(Android.Resource.Drawable.SymDefAppIcon) + .Build(); + + GetNotificationManager().Notify(NotificationId, n); + } + + private NotificationManager GetNotificationManager() + { + return (NotificationManager) (GetSystemService(NotificationService) ?? throw new Exception("Couldn't get a notification service")); + } + } +} \ No newline at end of file diff --git a/phms/MainActivity.cs b/phms/MainActivity.cs index 49dbc37..a76c362 100644 --- a/phms/MainActivity.cs +++ b/phms/MainActivity.cs @@ -49,15 +49,21 @@ namespace phms conn.Open(); using var cmd = new NpgsqlCommand("SHOW server_version", conn); var result = cmd.ExecuteScalar(); - Snackbar.Make(connect, $"Success, {result}", Snackbar.LengthShort).Show(); + SendNotification($"Success, {result}"); } catch { - Console.WriteLine(); - Snackbar.Make(connect, "Failure", Snackbar.LengthShort).Show(); - throw; + SendNotification("Failed!"); } }; } + + private void SendNotification(string message) + { + var intent = new Intent(this, typeof(BackgroundService)); + intent.SetAction(BackgroundService.UpdateNotificationAction); + intent.PutExtra("message", message); + StartService(intent); + } } } \ No newline at end of file diff --git a/phms/phms.csproj b/phms/phms.csproj index c211677..a59ab08 100644 --- a/phms/phms.csproj +++ b/phms/phms.csproj @@ -57,6 +57,7 @@ + -- 2.45.2