114 lines
3.4 KiB
C#
114 lines
3.4 KiB
C#
using Plugin.LocalNotification;
|
|
|
|
namespace WguApp.Services;
|
|
|
|
public static class LocalNotificationService
|
|
{
|
|
private static readonly Random Rng = new();
|
|
|
|
public static async Task<int?> ScheduleNotification(string title, string description, DateTime time)
|
|
{
|
|
var permCheck = await PermissionsService.CheckNotificationPermissions();
|
|
if (!permCheck)
|
|
{
|
|
LoggerService.LogToFile("Notification Service: Notification Permission Not Granted");
|
|
return null;
|
|
}
|
|
|
|
var newId = Rng.Next();
|
|
|
|
var notif = new NotificationRequest
|
|
{
|
|
NotificationId = newId,
|
|
Title = title,
|
|
Description = description,
|
|
Schedule =
|
|
{
|
|
NotifyTime = time,
|
|
RepeatType = NotificationRepeat.No
|
|
}
|
|
};
|
|
|
|
try
|
|
{
|
|
await LocalNotificationCenter.Current.Show(notif);
|
|
LoggerService.LogToFile($"Notification Service: {notif.NotificationId}:{notif.Title} set");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LoggerService.LogToFile($"Notification Service: {e.Message}");
|
|
throw;
|
|
}
|
|
|
|
return notif.NotificationId;
|
|
}
|
|
|
|
public static async Task<int> ScheduleNotification(NotificationRequest notif)
|
|
{
|
|
try
|
|
{
|
|
await LocalNotificationCenter.Current.Show(notif);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LoggerService.LogToFile(e.Message);
|
|
throw;
|
|
}
|
|
return notif.NotificationId;
|
|
}
|
|
|
|
public static async Task<bool> UpdateNotification(int notificationId, string? title = null, string? description = null, DateTime? time = null)
|
|
{
|
|
var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList();
|
|
|
|
var notif = pendingNotifs.FirstOrDefault(n => n.NotificationId == notificationId);
|
|
|
|
if (notif is null) return false;
|
|
|
|
await CancelNotification(notif);
|
|
|
|
notif.Title = title ?? notif.Title;
|
|
notif.Description = description ?? notif.Description;
|
|
notif.Schedule = new NotificationRequestSchedule()
|
|
{
|
|
NotifyTime = time ?? notif.Schedule.NotifyTime,
|
|
RepeatType = NotificationRepeat.No
|
|
};
|
|
|
|
await ScheduleNotification(notif);
|
|
return true;
|
|
}
|
|
|
|
public static async Task<bool> CancelNotification(int notificationId)
|
|
{
|
|
var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList();
|
|
|
|
var match = pendingNotifs.FirstOrDefault(n => n.NotificationId == notificationId);
|
|
|
|
if (match is null) return false;
|
|
|
|
LocalNotificationCenter.Current.Cancel(notificationId);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static async Task<bool> CancelNotification(NotificationRequest notif)
|
|
{
|
|
var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList();
|
|
|
|
if (pendingNotifs.Contains(notif))
|
|
{
|
|
notif.Cancel();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static async Task<bool> DoesNotificationAlreadyExist(int notificationId)
|
|
{
|
|
var pendingNotifs = await LocalNotificationCenter.Current.GetPendingNotificationList();
|
|
var match = pendingNotifs.FirstOrDefault(n => n.NotificationId == notificationId);
|
|
|
|
return match is not null;
|
|
}
|
|
} |