~phroa/phone-app

61842a204eb6b12febfd101510f130d58e307d5d — Jack Stratton 4 months ago 31f8f2d
Set up a background service for notifications
3 files changed, 77 insertions(+), 4 deletions(-)

A phms/BackgroundService.cs
M phms/MainActivity.cs
M phms/phms.csproj
A phms/BackgroundService.cs => phms/BackgroundService.cs +66 -0
@@ 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

M phms/MainActivity.cs => phms/MainActivity.cs +10 -4
@@ 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

M phms/phms.csproj => phms/phms.csproj +1 -0
@@ 57,6 57,7 @@
        <Reference Include="Mono.Android" />
    </ItemGroup>
    <ItemGroup>
        <Compile Include="BackgroundService.cs" />
        <Compile Include="MainActivity.cs" />
        <Compile Include="Resources\Resource.Designer.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />