google addin

This commit is contained in:
Jan Pleterski
2026-11-16 11:46:38 +01:00
parent 9463974813
commit d0c44157f1
7 changed files with 905 additions and 18 deletions
+29 -2
View File
@@ -63,9 +63,34 @@ class NotificationService {
AndroidFlutterLocalNotificationsPlugin>();
await androidPlugin?.createNotificationChannel(_channel);
await requestPermissions();
await _requestExactAlarmsOnce();
} catch (_) {}
}
// Android 12+ needs "Alarms & reminders" access for on-time delivery
// (denied by default on Android 14+). Ask once; without it we fall back
// to inexact alarms, which Android may delay by an hour or more.
Future<void> _requestExactAlarmsOnce() async {
final android = _plugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
if (android == null) return;
if (await android.canScheduleExactNotifications() ?? true) return;
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('exactAlarmAsked') ?? false) return;
await prefs.setBool('exactAlarmAsked', true);
await android.requestExactAlarmsPermission();
}
Future<AndroidScheduleMode> _scheduleMode() async {
final android = _plugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
final canExact = await android?.canScheduleExactNotifications() ?? true;
return canExact
? AndroidScheduleMode.exactAllowWhileIdle
: AndroidScheduleMode.inexactAllowWhileIdle;
}
/// Fetches the calendar and reschedules all reminders. Call on app start
/// and whenever notification settings change.
Future<void> rescheduleFromCalendar() async {
@@ -118,6 +143,7 @@ class NotificationService {
final notifyTekma = prefs.getBool('notifyTekma') ?? true;
final language = prefs.getString('language') ?? 'Slovenian';
final now = DateTime.now();
final mode = await _scheduleMode();
await _plugin.cancelAll();
@@ -143,6 +169,7 @@ class NotificationService {
title: event.summary ?? 'Event',
body: _startsIn(hours, language),
scheduledDate: notifyAt,
mode: mode,
);
}
}
@@ -167,6 +194,7 @@ class NotificationService {
required String title,
required String body,
required DateTime scheduledDate,
required AndroidScheduleMode mode,
}) async {
final tzDate = tz.TZDateTime.from(scheduledDate, tz.local);
await _plugin.zonedSchedule(
@@ -175,8 +203,7 @@ class NotificationService {
body,
tzDate,
_notifDetails,
// Inexact: avoids the SCHEDULE_EXACT_ALARM permission (Play policy).
androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
androidScheduleMode: mode,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);