resolveAndroidScheduleMode static method

  1. @visibleForTesting
Future<AndroidScheduleMode> resolveAndroidScheduleMode(
  1. FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
  2. AndroidScheduleMode androidScheduleMode
)

Returns androidScheduleMode, or its inexact equivalent when the app isn't allowed to schedule exact alarms.

Exact modes need the SCHEDULE_EXACT_ALARM permission, which Android 14+ denies by default, and zonedSchedule throws exact_alarms_not_permitted without it.

Implementation

@visibleForTesting
static Future<AndroidScheduleMode> resolveAndroidScheduleMode(
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
  AndroidScheduleMode androidScheduleMode,
) async {
  final AndroidScheduleMode inexactMode = switch (androidScheduleMode) {
    AndroidScheduleMode.exact => AndroidScheduleMode.inexact,
    AndroidScheduleMode.exactAllowWhileIdle ||
    AndroidScheduleMode.alarmClock =>
      AndroidScheduleMode.inexactAllowWhileIdle,
    _ => androidScheduleMode,
  };
  if (inexactMode == androidScheduleMode) return androidScheduleMode;

  final bool canScheduleExact =
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin
          >()
          ?.canScheduleExactNotifications() ??
      true;
  if (canScheduleExact) return androidScheduleMode;

  NyLogger.warning(
    'Exact alarms are not permitted, scheduling with ${inexactMode.name} instead',
  );
  return inexactMode;
}