Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exact schedule not working #2512

Open
HidemitsuHashimoto opened this issue Jan 1, 2025 · 2 comments
Open

Exact schedule not working #2512

HidemitsuHashimoto opened this issue Jan 1, 2025 · 2 comments

Comments

@HidemitsuHashimoto
Copy link

HidemitsuHashimoto commented Jan 1, 2025

Describe the bug
I use my app to register alarm to exact weekday and time.
I use this permission:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="32" />

But when i use exactAllowWhileIdle i got this error:

await localNotificationsPlugin.zonedSchedule(
  time.id,
  boss.name,
  'Alarme do boss ${boss.name} - ${time.hour}:${formatterNumber(time.minute)}',
  _nextInstanceOfWeek(
      hour: time.hour, minute: time.minute, weekday: time.weekday),
  NotificationDetails(android: androidDetails),
  androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
  uiLocalNotificationDateInterpretation:
      UILocalNotificationDateInterpretation.absoluteTime,
  matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime,
);
Error:
PlatformException (PlatformException(exact_alarms_not_permitted, Exact alarms are not permitted, null, null))

When i use inexactAllowWhileIdle the alarm just not working.

@Levi-Lesches
Copy link
Contributor

Levi-Lesches commented Jan 1, 2025

You'll need to do some runtime checks and requests:

  • you must request permissions from the user the first time you try to schedule exact alarms
  • even so, the user can revoke permissions at any time, so always check if you don't want the error
// Returns the Android version of the plugin on Android devices, null everywhere else
final androidPlugin = localNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();

// Check if you still have permission from last time you asked (or false if you never asked)
// Null means you're not on Android
var hasPermission = await androidPlugin?.canScheduleExactNotifications() ?? false;
if (!hasPermisssion) {
  // Request permissions again and update the variable
  hasPermission = await androidPlugin?.requestExactAlarmsPermission();
}

// You may have been denied permissions, so check the return value
if (hasPermission ?? false) {
  await localNotificationsPlugin.zonedSchedule(...);
}

See also: this section of the new Android setup guide. If this works for you, please let us know by closing the issue.

@HidemitsuHashimoto
Copy link
Author

Did work.

Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants