From 74722b5e811ddc215019cbf8aba33b4e274fe678 Mon Sep 17 00:00:00 2001 From: Jurij Date: Fri, 27 Mar 2026 22:27:44 +0100 Subject: [PATCH] Push vibrations update --- .../Android/AndroidNotificationScheduler.cs | 52 +++++++++++++++---- .../Windows/WindowsNotificationScheduler.cs | 14 +++++ Services/INotificationScheduler.cs | 4 +- Services/NoOpNotificationScheduler.cs | 16 +++++- ViewModels/OptionsViewModel.cs | 10 +++- Views/OptionsPage.xaml | 14 +++++ 6 files changed, 97 insertions(+), 13 deletions(-) diff --git a/Platforms/Android/AndroidNotificationScheduler.cs b/Platforms/Android/AndroidNotificationScheduler.cs index d013674..a28388b 100644 --- a/Platforms/Android/AndroidNotificationScheduler.cs +++ b/Platforms/Android/AndroidNotificationScheduler.cs @@ -1,4 +1,4 @@ -using Android.App; +using Android.App; using Android.Content; using AndroidX.Core.App; using cfs_tracker.Data; @@ -6,20 +6,26 @@ using cfs_tracker.Helpers; using cfs_tracker.Models; using cfs_tracker.Services; using Microsoft.Maui.ApplicationModel; +using Microsoft.Maui.Storage; namespace cfs_tracker.Platforms.Android; public sealed class AndroidNotificationScheduler : INotificationScheduler { - private const string ChannelId = "cfs_tracker_reminders"; - private const string ChannelName = "Erinnerungen"; + private const string ChannelIdSilent = "cfs_tracker_reminders_silent"; + private const string ChannelIdVibrate = "cfs_tracker_reminders_vibrate"; + private const string ChannelNameSilent = "Erinnerungen"; + private const string ChannelNameVibrate = "Erinnerungen mit Vibration"; private const string ChannelDescription = "Wie geht es dir? Erinnerungen"; private const string PrefScheduledIds = "notification_schedule_ids"; + private const string PrefVibrationEnabled = "notification_vibration_enabled"; private const string PrefYellowLabel = "notification_label_yellow"; private const string PrefRedLabel = "notification_label_red"; private const string PrefGrayLabel = "notification_label_gray"; + private static readonly long[] NotificationVibrationPattern = new long[] { 0, 250, 150, 250 }; + public const string ActionShowNotification = "cfs_tracker.action.SHOW_NOTIFICATION"; public const string ActionSaveStatus = "cfs_tracker.action.SAVE_STATUS"; public const string ActionOpenQuickEntry = "cfs_tracker.action.OPEN_QUICK_ENTRY"; @@ -56,10 +62,19 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler return status == PermissionStatus.Granted; } + public Task GetVibrationEnabledAsync() => + Task.FromResult(Preferences.Default.Get(PrefVibrationEnabled, false)); + + public Task SetVibrationEnabledAsync(bool isEnabled) + { + Preferences.Default.Set(PrefVibrationEnabled, isEnabled); + return Task.CompletedTask; + } + public async Task SyncAsync() { var context = Platform.AppContext; - EnsureChannel(context); + EnsureChannels(context); CancelAllScheduled(context); @@ -141,7 +156,10 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler string title, string message) { - EnsureChannel(context); + EnsureChannels(context); + + var vibrationEnabled = Preferences.Default.Get(PrefVibrationEnabled, false); + var channelId = GetChannelId(vibrationEnabled); var today = DateTime.Today; var todayText = today.ToString("yyyy-MM-dd"); @@ -158,7 +176,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler var smallIcon = context.ApplicationInfo?.Icon ?? global::Android.Resource.Drawable.IcDialogInfo; - var builder = new NotificationCompat.Builder(context, ChannelId) + var builder = new NotificationCompat.Builder(context, channelId) .SetSmallIcon(smallIcon) .SetContentTitle(title) .SetContentText(message) @@ -170,6 +188,9 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler .AddAction(0, redLabel, redPendingIntent) .AddAction(0, grayLabel, grayPendingIntent); + if (vibrationEnabled && !OperatingSystem.IsAndroidVersionAtLeast(26)) + builder.SetVibrate(NotificationVibrationPattern); + NotificationManagerCompat.From(context).Notify(notificationId, builder.Build()); } @@ -206,7 +227,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler BuildPendingIntentFlags()); } - internal static void EnsureChannel(Context context) + internal static void EnsureChannels(Context context) { if (!OperatingSystem.IsAndroidVersionAtLeast(26)) return; @@ -215,12 +236,20 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler if (manager is null) return; - var channel = new NotificationChannel(ChannelId, ChannelName, NotificationImportance.Default) + var silentChannel = new NotificationChannel(ChannelIdSilent, ChannelNameSilent, NotificationImportance.Default) { Description = ChannelDescription }; + silentChannel.EnableVibration(false); + manager.CreateNotificationChannel(silentChannel); - manager.CreateNotificationChannel(channel); + var vibrationChannel = new NotificationChannel(ChannelIdVibrate, ChannelNameVibrate, NotificationImportance.Default) + { + Description = ChannelDescription + }; + vibrationChannel.EnableVibration(true); + vibrationChannel.SetVibrationPattern(NotificationVibrationPattern); + manager.CreateNotificationChannel(vibrationChannel); } private void CancelAllScheduled(Context context) @@ -287,6 +316,9 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler return target; } + private static string GetChannelId(bool vibrationEnabled) => + vibrationEnabled ? ChannelIdVibrate : ChannelIdSilent; + private static int BuildAlarmRequestCode(int scheduleId, DayOfWeek day) { return scheduleId * 10 + (int)day; @@ -320,4 +352,4 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler if (flags.HasFlag(NotificationWeekdays.Saturday)) yield return DayOfWeek.Saturday; if (flags.HasFlag(NotificationWeekdays.Sunday)) yield return DayOfWeek.Sunday; } -} \ No newline at end of file +} diff --git a/Platforms/Windows/WindowsNotificationScheduler.cs b/Platforms/Windows/WindowsNotificationScheduler.cs index e1932a8..eea1134 100644 --- a/Platforms/Windows/WindowsNotificationScheduler.cs +++ b/Platforms/Windows/WindowsNotificationScheduler.cs @@ -1,6 +1,7 @@ using cfs_tracker.Data; using cfs_tracker.Models; using cfs_tracker.Services; +using Microsoft.Maui.Storage; using Microsoft.Toolkit.Uwp.Notifications; using Windows.UI.Notifications; @@ -9,6 +10,8 @@ namespace cfs_tracker.Platforms.Windows; public sealed class WindowsNotificationScheduler : INotificationScheduler { private const string GroupName = "cfs_tracker_weekly"; + private const string PrefVibrationEnabled = "notification_vibration_enabled"; + private readonly NotificationScheduleRepository _notificationScheduleRepository; public WindowsNotificationScheduler(NotificationScheduleRepository notificationScheduleRepository) @@ -21,6 +24,17 @@ public sealed class WindowsNotificationScheduler : INotificationScheduler return Task.FromResult(true); } + public Task GetVibrationEnabledAsync() + { + return Task.FromResult(Preferences.Default.Get(PrefVibrationEnabled, false)); + } + + public Task SetVibrationEnabledAsync(bool isEnabled) + { + Preferences.Default.Set(PrefVibrationEnabled, isEnabled); + return Task.CompletedTask; + } + public async Task SyncAsync() { try diff --git a/Services/INotificationScheduler.cs b/Services/INotificationScheduler.cs index 69032b7..147533f 100644 --- a/Services/INotificationScheduler.cs +++ b/Services/INotificationScheduler.cs @@ -3,5 +3,7 @@ namespace cfs_tracker.Services; public interface INotificationScheduler { Task EnsurePermissionAsync(); + Task GetVibrationEnabledAsync(); + Task SetVibrationEnabledAsync(bool isEnabled); Task SyncAsync(); -} \ No newline at end of file +} diff --git a/Services/NoOpNotificationScheduler.cs b/Services/NoOpNotificationScheduler.cs index 8666cf1..9eb5024 100644 --- a/Services/NoOpNotificationScheduler.cs +++ b/Services/NoOpNotificationScheduler.cs @@ -1,7 +1,21 @@ +using Microsoft.Maui.Storage; + namespace cfs_tracker.Services; public sealed class NoOpNotificationScheduler : INotificationScheduler { + private const string PrefVibrationEnabled = "notification_vibration_enabled"; + public Task EnsurePermissionAsync() => Task.FromResult(true); + + public Task GetVibrationEnabledAsync() => + Task.FromResult(Preferences.Default.Get(PrefVibrationEnabled, false)); + + public Task SetVibrationEnabledAsync(bool isEnabled) + { + Preferences.Default.Set(PrefVibrationEnabled, isEnabled); + return Task.CompletedTask; + } + public Task SyncAsync() => Task.CompletedTask; -} \ No newline at end of file +} diff --git a/ViewModels/OptionsViewModel.cs b/ViewModels/OptionsViewModel.cs index da8af14..6dbc6fe 100644 --- a/ViewModels/OptionsViewModel.cs +++ b/ViewModels/OptionsViewModel.cs @@ -42,6 +42,9 @@ public partial class OptionsViewModel : ObservableObject [ObservableProperty] private string currentDatabasePath = string.Empty; + [ObservableProperty] + private bool notificationVibrationEnabled; + [ObservableProperty] private OptionsTab selectedTab = OptionsTab.Notifications; @@ -139,6 +142,7 @@ public partial class OptionsViewModel : ObservableObject await LoadSymptomsAsync(); await LoadSchedulesAsync(); + NotificationVibrationEnabled = await _notificationScheduler.GetVibrationEnabledAsync(); UseExternalDatabase = _databaseLocationService.UseExternalDatabase; ExternalDatabasePath = _databaseLocationService.ExternalDatabasePath; CurrentDatabasePath = _appDatabase.CurrentPath; @@ -438,6 +442,8 @@ public partial class OptionsViewModel : ObservableObject private async Task SaveSchedulesAsync() { + await _notificationScheduler.SetVibrationEnabledAsync(NotificationVibrationEnabled); + var granted = await _notificationScheduler.EnsurePermissionAsync(); if (!granted) { @@ -464,7 +470,9 @@ public partial class OptionsViewModel : ObservableObject await _notificationScheduler.SyncAsync(); await LoadSchedulesAsync(); - StatusText = "Benachrichtigungszeiten gespeichert."; + StatusText = NotificationVibrationEnabled + ? "Benachrichtigungszeiten gespeichert. Vibration ist aktiv." + : "Benachrichtigungszeiten gespeichert. Vibration ist aus."; } private Task DeleteScheduleAsync(NotificationScheduleItemViewModel? item) diff --git a/Views/OptionsPage.xaml b/Views/OptionsPage.xaml index a5e73e3..25b82cd 100644 --- a/Views/OptionsPage.xaml +++ b/Views/OptionsPage.xaml @@ -153,6 +153,20 @@