From fe26fea450bb6e85d0a90ef348b3be12d660fc05 Mon Sep 17 00:00:00 2001 From: Kaustav Chaudhuri Date: Mon, 8 May 2023 18:49:27 +0530 Subject: [PATCH] notification helpers --- .../Android/Helpers/INotification.cs | 14 +++++ .../Android/Helpers/NotificationHelper.cs | 59 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 GMCabsDriverAssistantSolution/Platforms/Android/Helpers/INotification.cs create mode 100644 GMCabsDriverAssistantSolution/Platforms/Android/Helpers/NotificationHelper.cs diff --git a/GMCabsDriverAssistantSolution/Platforms/Android/Helpers/INotification.cs b/GMCabsDriverAssistantSolution/Platforms/Android/Helpers/INotification.cs new file mode 100644 index 0000000..0cfb6ae --- /dev/null +++ b/GMCabsDriverAssistantSolution/Platforms/Android/Helpers/INotification.cs @@ -0,0 +1,14 @@ +using Android.App; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GMCabsDriverAssistantSolution.Platforms.Android.Helpers +{ + public interface INotification + { + Notification ReturnNotif(); + } +} diff --git a/GMCabsDriverAssistantSolution/Platforms/Android/Helpers/NotificationHelper.cs b/GMCabsDriverAssistantSolution/Platforms/Android/Helpers/NotificationHelper.cs new file mode 100644 index 0000000..12e9e7c --- /dev/null +++ b/GMCabsDriverAssistantSolution/Platforms/Android/Helpers/NotificationHelper.cs @@ -0,0 +1,59 @@ +using Android.App; +using Android.Content; +using Android.OS; +using AndroidX.Core.App; +using GMCabsDriverAssistantSolution.Platforms.Android.Helpers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +[assembly: Dependency(typeof(NotificationHelper))] +namespace GMCabsDriverAssistantSolution.Platforms.Android.Helpers +{ + internal class NotificationHelper : INotification + { + private static string foregroundChannelId = "9001"; + private static Context context = global::Android.App.Application.Context; + + public Notification ReturnNotif() + { + var intent = new Intent(context, typeof(MainActivity)); + intent.AddFlags(ActivityFlags.SingleTop); + intent.PutExtra("Title", "Message"); + + var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S) + ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable + : PendingIntentFlags.UpdateCurrent; + var pendingActivityIntent = PendingIntent.GetActivity(context, 0, intent, pendingIntentFlags); + var pendingIntent = PendingIntent.GetBroadcast(context, 0, intent, pendingIntentFlags); + + var notifBuilder = new NotificationCompat.Builder(context, foregroundChannelId) + .SetContentTitle("Location") + .SetContentText("Update location") + .SetOngoing(true) + .SetContentIntent(pendingIntent); + + + if (global::Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O) + { + NotificationChannel notificationChannel = new NotificationChannel(foregroundChannelId, "Title", NotificationImportance.High); + notificationChannel.Importance = NotificationImportance.High; + notificationChannel.EnableLights(true); + notificationChannel.EnableVibration(true); + notificationChannel.SetShowBadge(true); + notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300 }); + + var notifManager = context.GetSystemService(Context.NotificationService) as NotificationManager; + if (notifManager != null) + { + notifBuilder.SetChannelId(foregroundChannelId); + notifManager.CreateNotificationChannel(notificationChannel); + } + } + + return notifBuilder.Build(); + } + } +}