Push vibrations update
This commit is contained in:
@@ -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<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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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()
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user