From 05778ee8eafa3b789b9f29d7b40490c93e2a19f6 Mon Sep 17 00:00:00 2001 From: Kaustav Chaudhuri Date: Tue, 16 May 2023 18:40:47 +0530 Subject: [PATCH] Notification services Implemented --- .../Platforms/Android/AndroidAlarmReceiver.cs | 79 +++++++++++++++++++ .../Platforms/Android/AndroidManifest.xml | 25 +++++- .../Services/NotificationChannelService.cs | 70 ++++++++++++++++ .../Views/HomePage.xaml.cs | 4 +- 4 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 GMCabsDriverAssistantSolution/Platforms/Android/AndroidAlarmReceiver.cs create mode 100644 GMCabsDriverAssistantSolution/Platforms/Android/Services/NotificationChannelService.cs diff --git a/GMCabsDriverAssistantSolution/Platforms/Android/AndroidAlarmReceiver.cs b/GMCabsDriverAssistantSolution/Platforms/Android/AndroidAlarmReceiver.cs new file mode 100644 index 0000000..7aeda18 --- /dev/null +++ b/GMCabsDriverAssistantSolution/Platforms/Android/AndroidAlarmReceiver.cs @@ -0,0 +1,79 @@ +using Android.App; +using Android.Content; +using Android.OS; +using GMCabsDriverAssistant.Services; +using GMCabsDriverAssistantSolution.Platforms.Android; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +[assembly: Dependency(typeof(AndroidAlarmReceiver))] +namespace GMCabsDriverAssistantSolution.Platforms.Android +{ + [BroadcastReceiver(Name = "au.com.gmcabs.driverassistant.app.AndroidAlarmReceiver", Enabled = true, Exported = true, DirectBootAware = true)] + public class AndroidAlarmReceiver : BroadcastReceiver, IAlarmService + { + [Obsolete] + public override void OnReceive(Context context, Intent intent) + { + string foregroundChannelId = "GMCabsForeGroundServiceRunningChannel"; + var message = "Foreground Service Is On"; + var title = "Service Status"; + var resultIntent = new Intent(context, typeof(MainActivity)); + resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask); + var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S) + ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable + : PendingIntentFlags.UpdateCurrent; + + var pending = PendingIntent.GetActivity(context, 0, + resultIntent, + pendingIntentFlags); + + var builder = + new Notification.Builder(context) + .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification) + //.SetColor(global::Android.App.Application.Context.GetColor(Resource.Color.color_notification_icon)) + .SetContentTitle(title) + .SetContentText(message) + .SetPriority((int)NotificationImportance.Max) + .SetAutoCancel(true) + .SetTimeoutAfter(60000); + + builder.SetContentIntent(pending); + var notification = builder.Build(); + var notifManager = global::Android.App.Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager; + + if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O) + { + var notificationChannel = new NotificationChannel(foregroundChannelId, "GMCabsAlarmManagerNotification", NotificationImportance.Low); + notificationChannel.Importance = NotificationImportance.Low; + notificationChannel.EnableLights(true); + notificationChannel.EnableVibration(true); + notificationChannel.SetShowBadge(true); + notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300 }); + if (builder != null) + { + builder.SetChannelId(foregroundChannelId); + notifManager.CreateNotificationChannel(notificationChannel); + } + } + notifManager.Notify(DateTime.Now.Millisecond, notification); + } + + public void StartMyAlarmService() + { + var alarmIntent = new Intent(global::Android.App.Application.Context, typeof(AndroidAlarmReceiver)); + var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S) + ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable + : PendingIntentFlags.UpdateCurrent; + + var pending = PendingIntent.GetBroadcast(global::Android.App.Application.Context, 0, alarmIntent, pendingIntentFlags); + var alarmManager = (AlarmManager)global::Android.App.Application.Context.GetSystemService(Context.AlarmService); + alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 1 * 1000, pending); + + } + } +} diff --git a/GMCabsDriverAssistantSolution/Platforms/Android/AndroidManifest.xml b/GMCabsDriverAssistantSolution/Platforms/Android/AndroidManifest.xml index 399b5f2..893fa25 100644 --- a/GMCabsDriverAssistantSolution/Platforms/Android/AndroidManifest.xml +++ b/GMCabsDriverAssistantSolution/Platforms/Android/AndroidManifest.xml @@ -1,9 +1,30 @@  + - - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/GMCabsDriverAssistantSolution/Platforms/Android/Services/NotificationChannelService.cs b/GMCabsDriverAssistantSolution/Platforms/Android/Services/NotificationChannelService.cs new file mode 100644 index 0000000..e690601 --- /dev/null +++ b/GMCabsDriverAssistantSolution/Platforms/Android/Services/NotificationChannelService.cs @@ -0,0 +1,70 @@ +using Android.App; +using Android.OS; +using GMCabsDriverAssistant.Services; +using GMCabsDriverAssistantSolution.Platforms.Android.Services; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Application = Android.App.Application; + +[assembly: Dependency(typeof(NotificationChannelService))] +namespace GMCabsDriverAssistantSolution.Platforms.Android.Services +{ + public class NotificationChannelService : INotificationChannelService + { + #region Constants + public const string TAG = nameof(GMCabsDriverAssistantApplication); + public static string SILENT_NOTIFICATION_CHANNEL_ID = "GMCabsDriverAssistantSilentNotificationChannel"; + public static string SILENT_NOTIFICATION_CHANNEL_NAME = "GM Cabs Silent Notification Channel"; + public static string SOUND_NOTIFICATION_CHANNEL_ID = "GMCabsDriverAssistantChannel"; + public static string SOUND_NOTIFICATION_CHANNEL_NAME = "GM Cabs Notification Channel"; + public static string SYSTEM_SOUND_NOTIFICATION_CHANNEL_ID = "GMCabsDriverAssistantSystemChannel"; + public static string SYSTEM_SOUND_NOTIFICATION_CHANNEL_NAME = "GM Cabs System Notification Channel"; + #endregion + + public void ManageNotificationChannelService() + { + if (Build.VERSION.SdkInt >= BuildVersionCodes.O) + { + var silentNotificationImportance = NotificationImportance.Low; + var soundNotificationImportance = NotificationImportance.Max; + var soundFileName = "newbooking"; + + + NotificationManager silentNotificationManager = Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager; + NotificationManager customNotificationManager = Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager; + NotificationManager systemNotificationManager = Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager; + + /* Delete Notification Channel */ + silentNotificationManager.DeleteNotificationChannel(SILENT_NOTIFICATION_CHANNEL_ID); + customNotificationManager.DeleteNotificationChannel(SOUND_NOTIFICATION_CHANNEL_ID); + systemNotificationManager.DeleteNotificationChannel(SYSTEM_SOUND_NOTIFICATION_CHANNEL_ID); + + /* Silent Notification Channel */ + + NotificationChannel silentNotificationchannel = new NotificationChannel(SILENT_NOTIFICATION_CHANNEL_ID, SILENT_NOTIFICATION_CHANNEL_NAME, silentNotificationImportance); + silentNotificationchannel.EnableVibration(true); + silentNotificationchannel.LockscreenVisibility = NotificationVisibility.Public; + silentNotificationManager.CreateNotificationChannel(silentNotificationchannel); + + /* Custom Sound Notification Channel */ + + NotificationChannel soundNotificationChannel = new NotificationChannel(SOUND_NOTIFICATION_CHANNEL_ID, SOUND_NOTIFICATION_CHANNEL_NAME, soundNotificationImportance); + soundNotificationChannel.EnableVibration(true); + soundNotificationChannel.LockscreenVisibility = NotificationVisibility.Public; + soundNotificationChannel.SetSound(global::Android.Net.Uri.Parse("android.resource://au.com.gmcabs.driverassistant/raw/" + soundFileName), null); + customNotificationManager.CreateNotificationChannel(soundNotificationChannel); + + /* System Sound Notification Channel */ + + NotificationChannel systemSoundNotificationChannel = new NotificationChannel(SYSTEM_SOUND_NOTIFICATION_CHANNEL_ID, SYSTEM_SOUND_NOTIFICATION_CHANNEL_NAME, soundNotificationImportance); + systemSoundNotificationChannel.EnableVibration(true); + systemSoundNotificationChannel.LockscreenVisibility = NotificationVisibility.Public; + systemNotificationManager.CreateNotificationChannel(systemSoundNotificationChannel); + } + + } + } +} diff --git a/GMCabsDriverAssistantSolution/Views/HomePage.xaml.cs b/GMCabsDriverAssistantSolution/Views/HomePage.xaml.cs index d28b332..7bafdce 100644 --- a/GMCabsDriverAssistantSolution/Views/HomePage.xaml.cs +++ b/GMCabsDriverAssistantSolution/Views/HomePage.xaml.cs @@ -174,11 +174,11 @@ public partial class HomePage : ContentPage private async void OnNotificationViewClicked(object sender, EventArgs e) { - // await Navigation.PushAsync(new NotificationsPage()); + await Navigation.PushAsync(new NotificationsPage()); } private async void OnBookingViewClicked(object sender, EventArgs e) { - // await Navigation.PushAsync(new BookingsPage(_viewModel.CurrentLocation.Latitude, _viewModel.CurrentLocation.Longitude)); + await Navigation.PushAsync(new BookingsPage(_viewModel.CurrentLocation.Latitude, _viewModel.CurrentLocation.Longitude)); } [Obsolete]