calendar fix and update.
This commit is contained in:
@@ -62,9 +62,50 @@ class NotificationService {
|
||||
AndroidFlutterLocalNotificationsPlugin>();
|
||||
await androidPlugin?.createNotificationChannel(_channel);
|
||||
await requestPermissions();
|
||||
await _requestExactAlarmsOnce();
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
// Android 12+ needs "Alarms & reminders" access for on-time delivery.
|
||||
// Ask once; without it we fall back to inexact alarms.
|
||||
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 {
|
||||
final service = GoogleCalendarService();
|
||||
try {
|
||||
final now = DateTime.now();
|
||||
final events = await service.getEvents(
|
||||
startDate: now,
|
||||
endDate: now.add(const Duration(days: 365)),
|
||||
);
|
||||
await scheduleEventNotifications(events);
|
||||
} catch (_) {
|
||||
} finally {
|
||||
service.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> requestPermissions() async {
|
||||
final android = _plugin.resolvePlatformSpecificImplementation<
|
||||
AndroidFlutterLocalNotificationsPlugin>();
|
||||
@@ -96,6 +137,7 @@ class NotificationService {
|
||||
final notifyTekma = prefs.getBool('notifyTekma') ?? true;
|
||||
final isSlovenian = (prefs.getString('language') ?? 'English') == 'Slovenian';
|
||||
final now = DateTime.now();
|
||||
final mode = await _scheduleMode();
|
||||
|
||||
await _plugin.cancelAll();
|
||||
|
||||
@@ -104,9 +146,11 @@ class NotificationService {
|
||||
if (event.startTime == null) continue;
|
||||
|
||||
final summary = (event.summary ?? '').toLowerCase();
|
||||
final isCompetition = summary.contains('liga') || summary.contains('tekmovanje');
|
||||
final isSestanek = summary.contains('sestanek');
|
||||
final isDezuranje = !isCompetition && !isSestanek;
|
||||
// Same classification as the calendar screen; skip anything else
|
||||
final isCompetition = summary.contains('liga') || summary.contains('tekmovanje') || summary.contains('tekma');
|
||||
final isSestanek = !isCompetition && summary.contains('sestanek');
|
||||
final isDezuranje = !isCompetition && !isSestanek && summary.contains('dežurstvo');
|
||||
if (!isCompetition && !isSestanek && !isDezuranje) continue;
|
||||
|
||||
if (isCompetition && !notifyTekma) continue;
|
||||
if (isSestanek && !notifySestanek) continue;
|
||||
@@ -121,6 +165,7 @@ class NotificationService {
|
||||
? (hours == 1 ? 'Začne se čez 1 uro' : 'Začne se čez $hours ur')
|
||||
: (hours == 1 ? 'Starts in 1 hour' : 'Starts in $hours hours'),
|
||||
scheduledDate: notifyAt,
|
||||
mode: mode,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -144,6 +189,7 @@ class NotificationService {
|
||||
title: isSlovenian ? 'Načrtovano testno obvestilo' : 'Scheduled Test',
|
||||
body: isSlovenian ? 'Načrtovana obvestila delujejo!' : 'Scheduled notifications are working!',
|
||||
scheduledDate: in10s,
|
||||
mode: await _scheduleMode(),
|
||||
);
|
||||
} catch (_) {}
|
||||
}
|
||||
@@ -153,6 +199,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(
|
||||
@@ -161,7 +208,7 @@ class NotificationService {
|
||||
body,
|
||||
tzDate,
|
||||
_notifDetails,
|
||||
androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
|
||||
androidScheduleMode: mode,
|
||||
uiLocalNotificationDateInterpretation:
|
||||
UILocalNotificationDateInterpretation.absoluteTime,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user