Push vibrations update

This commit is contained in:
2026-03-27 22:27:44 +01:00
parent 7b824b6f0c
commit 74722b5e81
6 changed files with 97 additions and 13 deletions
@@ -1,4 +1,4 @@
using Android.App; using Android.App;
using Android.Content; using Android.Content;
using AndroidX.Core.App; using AndroidX.Core.App;
using cfs_tracker.Data; using cfs_tracker.Data;
@@ -6,20 +6,26 @@ using cfs_tracker.Helpers;
using cfs_tracker.Models; using cfs_tracker.Models;
using cfs_tracker.Services; using cfs_tracker.Services;
using Microsoft.Maui.ApplicationModel; using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Storage;
namespace cfs_tracker.Platforms.Android; namespace cfs_tracker.Platforms.Android;
public sealed class AndroidNotificationScheduler : INotificationScheduler public sealed class AndroidNotificationScheduler : INotificationScheduler
{ {
private const string ChannelId = "cfs_tracker_reminders"; private const string ChannelIdSilent = "cfs_tracker_reminders_silent";
private const string ChannelName = "Erinnerungen"; 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 ChannelDescription = "Wie geht es dir? Erinnerungen";
private const string PrefScheduledIds = "notification_schedule_ids"; private const string PrefScheduledIds = "notification_schedule_ids";
private const string PrefVibrationEnabled = "notification_vibration_enabled";
private const string PrefYellowLabel = "notification_label_yellow"; private const string PrefYellowLabel = "notification_label_yellow";
private const string PrefRedLabel = "notification_label_red"; private const string PrefRedLabel = "notification_label_red";
private const string PrefGrayLabel = "notification_label_gray"; 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 ActionShowNotification = "cfs_tracker.action.SHOW_NOTIFICATION";
public const string ActionSaveStatus = "cfs_tracker.action.SAVE_STATUS"; public const string ActionSaveStatus = "cfs_tracker.action.SAVE_STATUS";
public const string ActionOpenQuickEntry = "cfs_tracker.action.OPEN_QUICK_ENTRY"; public const string ActionOpenQuickEntry = "cfs_tracker.action.OPEN_QUICK_ENTRY";
@@ -56,10 +62,19 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
return status == PermissionStatus.Granted; return status == PermissionStatus.Granted;
} }
public Task<bool> 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() public async Task SyncAsync()
{ {
var context = Platform.AppContext; var context = Platform.AppContext;
EnsureChannel(context); EnsureChannels(context);
CancelAllScheduled(context); CancelAllScheduled(context);
@@ -141,7 +156,10 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
string title, string title,
string message) string message)
{ {
EnsureChannel(context); EnsureChannels(context);
var vibrationEnabled = Preferences.Default.Get(PrefVibrationEnabled, false);
var channelId = GetChannelId(vibrationEnabled);
var today = DateTime.Today; var today = DateTime.Today;
var todayText = today.ToString("yyyy-MM-dd"); 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 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) .SetSmallIcon(smallIcon)
.SetContentTitle(title) .SetContentTitle(title)
.SetContentText(message) .SetContentText(message)
@@ -170,6 +188,9 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
.AddAction(0, redLabel, redPendingIntent) .AddAction(0, redLabel, redPendingIntent)
.AddAction(0, grayLabel, grayPendingIntent); .AddAction(0, grayLabel, grayPendingIntent);
if (vibrationEnabled && !OperatingSystem.IsAndroidVersionAtLeast(26))
builder.SetVibrate(NotificationVibrationPattern);
NotificationManagerCompat.From(context).Notify(notificationId, builder.Build()); NotificationManagerCompat.From(context).Notify(notificationId, builder.Build());
} }
@@ -206,7 +227,7 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
BuildPendingIntentFlags()); BuildPendingIntentFlags());
} }
internal static void EnsureChannel(Context context) internal static void EnsureChannels(Context context)
{ {
if (!OperatingSystem.IsAndroidVersionAtLeast(26)) if (!OperatingSystem.IsAndroidVersionAtLeast(26))
return; return;
@@ -215,12 +236,20 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
if (manager is null) if (manager is null)
return; return;
var channel = new NotificationChannel(ChannelId, ChannelName, NotificationImportance.Default) var silentChannel = new NotificationChannel(ChannelIdSilent, ChannelNameSilent, NotificationImportance.Default)
{ {
Description = ChannelDescription 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) private void CancelAllScheduled(Context context)
@@ -287,6 +316,9 @@ public sealed class AndroidNotificationScheduler : INotificationScheduler
return target; return target;
} }
private static string GetChannelId(bool vibrationEnabled) =>
vibrationEnabled ? ChannelIdVibrate : ChannelIdSilent;
private static int BuildAlarmRequestCode(int scheduleId, DayOfWeek day) private static int BuildAlarmRequestCode(int scheduleId, DayOfWeek day)
{ {
return scheduleId * 10 + (int)day; return scheduleId * 10 + (int)day;
@@ -1,6 +1,7 @@
using cfs_tracker.Data; using cfs_tracker.Data;
using cfs_tracker.Models; using cfs_tracker.Models;
using cfs_tracker.Services; using cfs_tracker.Services;
using Microsoft.Maui.Storage;
using Microsoft.Toolkit.Uwp.Notifications; using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications; using Windows.UI.Notifications;
@@ -9,6 +10,8 @@ namespace cfs_tracker.Platforms.Windows;
public sealed class WindowsNotificationScheduler : INotificationScheduler public sealed class WindowsNotificationScheduler : INotificationScheduler
{ {
private const string GroupName = "cfs_tracker_weekly"; private const string GroupName = "cfs_tracker_weekly";
private const string PrefVibrationEnabled = "notification_vibration_enabled";
private readonly NotificationScheduleRepository _notificationScheduleRepository; private readonly NotificationScheduleRepository _notificationScheduleRepository;
public WindowsNotificationScheduler(NotificationScheduleRepository notificationScheduleRepository) public WindowsNotificationScheduler(NotificationScheduleRepository notificationScheduleRepository)
@@ -21,6 +24,17 @@ public sealed class WindowsNotificationScheduler : INotificationScheduler
return Task.FromResult(true); return Task.FromResult(true);
} }
public Task<bool> 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() public async Task SyncAsync()
{ {
try try
+2
View File
@@ -3,5 +3,7 @@ namespace cfs_tracker.Services;
public interface INotificationScheduler public interface INotificationScheduler
{ {
Task<bool> EnsurePermissionAsync(); Task<bool> EnsurePermissionAsync();
Task<bool> GetVibrationEnabledAsync();
Task SetVibrationEnabledAsync(bool isEnabled);
Task SyncAsync(); Task SyncAsync();
} }
+14
View File
@@ -1,7 +1,21 @@
using Microsoft.Maui.Storage;
namespace cfs_tracker.Services; namespace cfs_tracker.Services;
public sealed class NoOpNotificationScheduler : INotificationScheduler public sealed class NoOpNotificationScheduler : INotificationScheduler
{ {
private const string PrefVibrationEnabled = "notification_vibration_enabled";
public Task<bool> EnsurePermissionAsync() => Task.FromResult(true); public Task<bool> EnsurePermissionAsync() => Task.FromResult(true);
public Task<bool> 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; public Task SyncAsync() => Task.CompletedTask;
} }
+9 -1
View File
@@ -42,6 +42,9 @@ public partial class OptionsViewModel : ObservableObject
[ObservableProperty] [ObservableProperty]
private string currentDatabasePath = string.Empty; private string currentDatabasePath = string.Empty;
[ObservableProperty]
private bool notificationVibrationEnabled;
[ObservableProperty] [ObservableProperty]
private OptionsTab selectedTab = OptionsTab.Notifications; private OptionsTab selectedTab = OptionsTab.Notifications;
@@ -139,6 +142,7 @@ public partial class OptionsViewModel : ObservableObject
await LoadSymptomsAsync(); await LoadSymptomsAsync();
await LoadSchedulesAsync(); await LoadSchedulesAsync();
NotificationVibrationEnabled = await _notificationScheduler.GetVibrationEnabledAsync();
UseExternalDatabase = _databaseLocationService.UseExternalDatabase; UseExternalDatabase = _databaseLocationService.UseExternalDatabase;
ExternalDatabasePath = _databaseLocationService.ExternalDatabasePath; ExternalDatabasePath = _databaseLocationService.ExternalDatabasePath;
CurrentDatabasePath = _appDatabase.CurrentPath; CurrentDatabasePath = _appDatabase.CurrentPath;
@@ -438,6 +442,8 @@ public partial class OptionsViewModel : ObservableObject
private async Task SaveSchedulesAsync() private async Task SaveSchedulesAsync()
{ {
await _notificationScheduler.SetVibrationEnabledAsync(NotificationVibrationEnabled);
var granted = await _notificationScheduler.EnsurePermissionAsync(); var granted = await _notificationScheduler.EnsurePermissionAsync();
if (!granted) if (!granted)
{ {
@@ -464,7 +470,9 @@ public partial class OptionsViewModel : ObservableObject
await _notificationScheduler.SyncAsync(); await _notificationScheduler.SyncAsync();
await LoadSchedulesAsync(); await LoadSchedulesAsync();
StatusText = "Benachrichtigungszeiten gespeichert."; StatusText = NotificationVibrationEnabled
? "Benachrichtigungszeiten gespeichert. Vibration ist aktiv."
: "Benachrichtigungszeiten gespeichert. Vibration ist aus.";
} }
private Task DeleteScheduleAsync(NotificationScheduleItemViewModel? item) private Task DeleteScheduleAsync(NotificationScheduleItemViewModel? item)
+14
View File
@@ -153,6 +153,20 @@
<Label Text="Ein Zeitblock kann auf mehreren Wochentagen aktiv sein." <Label Text="Ein Zeitblock kann auf mehreren Wochentagen aktiv sein."
TextColor="{DynamicResource TextMuted}" /> TextColor="{DynamicResource TextMuted}" />
<Grid ColumnDefinitions="Auto,*"
ColumnSpacing="8">
<CheckBox IsChecked="{Binding NotificationVibrationEnabled}" />
<VerticalStackLayout Grid.Column="1"
Spacing="2">
<Label Text="Vibration für Push-Nachrichten"
VerticalOptions="Center"
TextColor="{DynamicResource TextPrimary}" />
<Label Text="Auf Android wird dafür automatisch der passende Benachrichtigungskanal verwendet."
FontSize="12"
TextColor="{DynamicResource TextMuted}" />
</VerticalStackLayout>
</Grid>
<Button Text="Zeit hinzufügen" <Button Text="Zeit hinzufügen"
Style="{StaticResource PrimaryButtonStyle}" Style="{StaticResource PrimaryButtonStyle}"
Command="{Binding AddScheduleCommand}" /> Command="{Binding AddScheduleCommand}" />