Search code examples
flutterflutter-pluginflutter-packages

android_alarm_manager_plus not triggering callback


Followed the example instructions for this plugin successfully but only the FIRST debug session... So I know it CAN work. Now, I get no error but the alarm is never triggered ?!? Traced into the plugin and no error there. You are my last resort, google did not provide solution and I don't have any hair left to pull...

Debuging on my connected device: Samsung model SM-J120W Android version 6.0.1

android_alarm_manager_plus 1.0.1

AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
...
<service
  android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
  android:permission="android.permission.BIND_JOB_SERVICE"
  android:exported="false"/>
<receiver
  android:name="io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver"
  android:exported="false"/>
<receiver
  android:name="io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver"
  android:enabled="false">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
  </intent-filter>
</receiver>

in main.dart:

// top level function is never called :-(
void callback() {
  final DateTime now = DateTime.now();
  final int isolateId = Isolate.current.hashCode;
  print("[$now] Hello, world! isolate=$isolateId function='$callback'");
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  bool test = await AndroidAlarmManager.initialize();
  print('initialize test = $test'); // **_returns true_**
  ...
}

in my home page:

Center(
  child: Switch(
  value: isOn,
  onChanged: (value) {
    setState(() {
      isOn = value;
    });
    if (isOn == true) {
      AndroidAlarmManager.periodic(Duration(seconds: 10), alarmId, **callback**, startAt: DateTime.now(), exact: true, wakeup: true)
          .then((value) => print('Alarm Timer Started = $value')); // **_returns true_**
    } else {
      AndroidAlarmManager.cancel(alarmId).then((value) => print('Alarm Timer Canceled = $value'));
    }
  },
)),

flutter doctor:

[√] Flutter (Channel stable, 2.2.1, on Microsoft Windows [Version 10.0.19042.985], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Chrome - develop for the web
[√] Android Studio
[√] VS Code (version 1.56.2)
[√] Connected device (3 available)

• No issues found!

Is there any missing information that I can provide ?

Thanks for any help/sugestion you can provide !


Solution

  • Update AndroidManifest.xml with the service code below:

    <service
        android:name="dev.fluttercommunity.plus.androidalarmmanager.AlarmService"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:exported="false"/>
    <receiver
        android:name="dev.fluttercommunity.plus.androidalarmmanager.AlarmBroadcastReceiver"
        android:exported="false"/>
    <receiver
        android:name="dev.fluttercommunity.plus.androidalarmmanager.RebootBroadcastReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>