fluttercommunity / plus_plugins

Flutter Community Plus Plugins
BSD 3-Clause "New" or "Revised" License
1.56k stars 941 forks source link

[Bug]: exact android alarm has 18 hour window of error #3262

Open insertokname opened 2 days ago

insertokname commented 2 days ago

Platform

Android 14

Plugin

android_alarm_manager_plus

Version

4.0.4

Flutter SDK

3.22.2

Steps to reproduce

Add the package to the project, and in the main file schedule an exact alarm to run in 1 minute. I have also set battery optimizations to unrestricted through android settings.

  await AndroidAlarmManager.initialize();

  final startAt = DateTime.now().add(const Duration(minutes: 1));

  await AndroidAlarmManager.periodic(
    const Duration(days: 1),
    0,
    callbackDispatcher,
    startAt: startAt,
    exact: true,
    wakeup: true,
    rescheduleOnReboot: true,
    allowWhileIdle: true,
  );

callbackDispatcher is a simple function that just logs something. the alarm will not run in exactly 1 minute, and looking at the output of adb shell dumpsys alarm

RTC_WAKEUP #1: Alarm{37037cd type 0 origWhen 1726819223832 whenElapsed 2140998 com.insertokname.sala_management}
      tag=*walarm*:com.insertokname.sala_management/dev.fluttercommunity.plus.androidalarmmanager.AlarmBroadcastReceiver
      type=RTC_WAKEUP origWhen=2024-09-20 11:00:23.832 window=+18h0m0s0ms repeatInterval=86400000 count=0 flags=0x8
      policyWhenElapsed: requester=-6m29s581ms app_standby=-7m29s545ms device_idle=-- battery_saver=-6m15s240ms tare=-7m29s545ms gms_manager=--
      whenElapsed=-6m15s240ms maxWhenElapsed=+17h53m30s419ms
      operation=PendingIntent{d566882: PendingIntentRecord{1870c8d com.insertokname.sala_management broadcastIntent}}

I can see a window of 18 hours. Does this mean that the alarm can have an error of up to 18 hours? If so is there any way of working around it?

Code Sample

await AndroidAlarmManager.initialize();

  final startAt = DateTime.now().add(const Duration(minutes: 1));

  await AndroidAlarmManager.periodic(
    const Duration(days: 1),
    0,
    callbackDispatcher,
    startAt: startAt,
    exact: true,
    wakeup: true,
    rescheduleOnReboot: true,
    allowWhileIdle: true,
  );

Logs

since logs are 6k lines:

https://justpaste.it/7tvwd

Flutter Doctor

[✓] Flutter (Channel stable, 3.22.2, on NixOS 24.11 (Vicuna) 6.10.1, locale en_US.UTF-8)
    • Flutter version 3.22.2 on channel stable at
      /nix/store/c9fxvvl8n4v0w679p82wpgspzn4kyqbr-flutter-wrapped-3.22.2-sdk-links
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision nixpkgs000 (), 1970-01-01 00:00:00
    • Engine revision edd8546116
    • Dart version 3.4.3
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at
      /nix/store/v8zpb7k7604z4zyxfcvbqa3ksb68lw0g-androidsdk/libexec/android-sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME =
      /nix/store/v8zpb7k7604z4zyxfcvbqa3ksb68lw0g-androidsdk/libexec/android-sdk
    • ANDROID_SDK_ROOT =
      /nix/store/v8zpb7k7604z4zyxfcvbqa3ksb68lw0g-androidsdk/libexec/android-sdk
    • Java binary at:
      /nix/store/l7pwy1rxdrb13svqandm720d06fycd8c-openjdk-17.0.11+9/lib/openjdk/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+9-nixos)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE =
      /nix/store/rf8lcgpmylhd19cwzs18cvn8r7mxv7f9-chromedriver-unwrapped-127.0.6533.88/b
      in/chromedriver

[✓] Linux toolchain - develop for Linux desktop
    • clang version 18.1.8
    • cmake version 3.29.6
    • ninja version 1.12.1
    • pkg-config version 0.29.2

[!] Android Studio (not installed)
    • Android Studio not found; download from
      https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for
      detailed instructions).

[✓] Connected device (3 available)
    • SM F721B (mobile) • R5CT819305F • android-arm64  • Android 14 (API 34)
    • Linux (desktop)   • linux       • linux-x64      • NixOS 24.11 (Vicuna) 6.10.1
    • Chrome (web)      • chrome      • web-javascript • ChromeDriver 127.0.6533.88
      (a2d0cb026721e4644e489b8ebb07038ca4e4351c-refs/branch-heads/6533@{#1846})

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Checklist before submitting a bug

vbuberen commented 2 days ago

Add the package to the project, and in the main file schedule an exact alarm to run in 1 minute.

Did you configure your project to get EXACT_ALARM permission as it is required in last Android versions? It is mentioned in the README of the package as well: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/android_alarm_manager_plus#getting-started https://developer.android.com/about/versions/14/changes/schedule-exact-alarms

insertokname commented 2 days ago

Did you configure your project to get EXACT_ALARM permission as it is required in last Android versions? It is mentioned in the README of the package as well: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/android_alarm_manager_plus#getting-started https://developer.android.com/about/versions/14/changes/schedule-exact-alarms

Thank you for your response!

Yes i think i have adequately requested the permission.

i have put

<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

in my manifest

and used the permission_handler package to request the permission via Permission.scheduleExactAlarm.request()

Sorry for not mentioning this in the original report!