Writing

Flutter exact alarm permission on Android 14: stop the jump to Settings

By Mohamed Gado · · 4 min read

Hadeed, my offline gym log, has a rest timer. You finish a set, tap "Set done", put the phone in your pocket, and it rings when rest is over. On my Android 14 phone, the first "Set done" of every session did something else: it opened Android's Alarms & reminders settings page and threw me out of the app mid-set. The call behind it, requestExactAlarmsPermission() from flutter_local_notifications, had been there since the first commit. Fixing it uncovered a second bug underneath. Without that permission, an alarmClock schedule never rang at all.

Why requestExactAlarmsPermission opens Settings#

Exact alarms are a special permission on Android 12 and later. There's no runtime dialog for SCHEDULE_EXACT_ALARM. The only way to "request" it is to send the user to a system settings page where they flip a switch for your app. On Android 14, apps that aren't alarm clocks or calendars start with this permission denied.

flutter_local_notifications exposes this as requestExactAlarmsPermission(). It reads like a permission prompt, but on a real phone it opens Settings. My rest timer asked for it every time it scheduled the alarm, so every session started with a trip to Settings.

The unit tests never caught it. They all mocked the plugin's platform channel, so a call to requestExactAlarmsPermission just returned a value. Nothing checked what that call does on a device.

Why USE_EXACT_ALARM wasn't an option#

Android also has USE_EXACT_ALARM, which is granted at install and needs no switch. Google Play restricts it to apps whose core function is an alarm clock or a calendar, and the Play Console declaration offers exactly those two choices. A gym log is neither, so I removed USE_EXACT_ALARM before the first closed test. That left SCHEDULE_EXACT_ALARM, which Play allows but the user must turn on, so for most users it's off.

The second bug: alarmClock without the permission#

The rest alarm was scheduled with AndroidScheduleMode.alarmClock, which needs exact-alarm permission. Without it, zonedSchedule throws. My schedule() caught and logged errors, so nothing crashed. The alarm just never got scheduled.

So for everyone who declined the Settings page, or backed out of it, the rest timer was silent once the screen was off. The first bug hid the second: the Settings jump at least gave people a chance to grant the permission. Removing it without fixing the schedule mode would have made the alarm silent for nearly everyone.

The fix: check, then fall back to an inexact alarm#

Three changes:

  1. Never call requestExactAlarmsPermission() inside a flow. The permission request now asks for notification permission only.
  2. Check before scheduling. canScheduleExactNotifications() tells you whether exact alarms are allowed, without leaving the app.
  3. Pick the schedule mode from that answer. Use alarmClock when it's allowed, and inexactAllowWhileIdle when it isn't.
Future<bool> requestPermission() async {
  // No requestExactAlarmsPermission here: on Android 14 it opens system
  // Settings. schedule() falls back to an inexact alarm instead.
  return await android?.requestNotificationsPermission() ?? true;
}
 
Future<bool> canExact() async {
  try {
    return await android?.canScheduleExactNotifications() ?? false;
  } catch (_) {
    return false;
  }
}
 
AndroidScheduleMode restAlarmMode({required bool canExact}) =>
    canExact ? AndroidScheduleMode.alarmClock : AndroidScheduleMode.inexactAllowWhileIdle;

And in the schedule call:

await plugin.zonedSchedule(
  id,
  title,
  body,
  when,
  details,
  androidScheduleMode: restAlarmMode(canExact: await canExact()),
);

An inexact alarm can fire a little late when the phone is in Doze. For a 90-second rest timer that's a fair trade against never ringing. People who want it exact can still grant the permission from the app's settings screen, where opening Settings is what they asked for.

Hadeed's weekly training reminder already used this "exact if allowed, otherwise inexact" pattern. The rest alarm was the one place that didn't.

Test what must NOT be called#

The mocked platform channel was the reason this survived so long, so the new tests use the same mock to check the reverse. One asserts that a method is never sent:

test('asking for permission never opens system Settings', () async {
  await RestAlarm.instance.requestPermission();
  expect(sentMethods(), isNot(contains('requestExactAlarmsPermission')));
});
 
test('without exact-alarm permission it still schedules, inexact', () async {
  exactAllowed = false;
  await RestAlarm.instance.schedule(const Duration(seconds: 90));
  expect(scheduledArgs()['scheduleMode'], 'inexactAllowWhileIdle');
});

Both failed against the old code and pass with the fix.

Checklist for exact alarms in Flutter#

  • Don't call requestExactAlarmsPermission() from a flow the user is in the middle of. It opens a settings page, not a dialog.
  • Call canScheduleExactNotifications() before every schedule, and choose alarmClock or inexactAllowWhileIdle from the result.
  • Don't declare USE_EXACT_ALARM unless your app really is an alarm clock or a calendar. Play checks.
  • Make sure a failed zonedSchedule is visible somewhere. A caught-and-logged error here means a silent alarm.
  • Run permission flows on a real Android 14 phone once. A mocked channel can't show you a settings page.

Takeaways#

  • On Android 14, exact-alarm permission starts denied for most apps, and asking for it means leaving your app.
  • alarmClock without that permission doesn't fall back by itself. It throws, and the alarm never rings.
  • Check, then degrade: exact when allowed, inexactAllowWhileIdle otherwise.
  • When a test mocks a platform call, also assert the calls that must never be made.