Android Alarm Manager Plus in Flutter never fires oneShot

106

first did you add the required AndroidManifest.xml tags?

second thing, by reading the documentation on https://pub.dev/packages/android_alarm_manager_plus, the callback is executed on a separate Isolate thus you can't pass a function from an instance class since isolates don't share memory (isolate is to run a piece of code on another thread). You can make sure that the plugin is working by adding a static function with a print statement (you can't call setState from a static function)

change the playAlarm function into:

  static void playAlarm() {
    print("playAlarm");
  }

this function is used to verify that the plugin is working

Share:
106
RNPF
Author by

RNPF

Updated on January 04, 2023

Comments

  • RNPF
    RNPF over 1 year

    I am trying to get a handle on the nearly entirely undocumented Android Alarm Manager Plus, and have a very simple app to press a button, set an alarm, and fire the alarm as follows:

    import 'package:flutter/material.dart';
    import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await AndroidAlarmManager.initialize();
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.pink,
          ),
          home: SetAlarmPage(),
        );
      }
    }
    
    class SetAlarmPage extends StatefulWidget {
      const SetAlarmPage({Key? key}) : super(key: key);
    
      @override
      State<SetAlarmPage> createState() => _SetAlarmPageState();
    }
    
    class _SetAlarmPageState extends State<SetAlarmPage> {
      String test = "Press Me!";
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text("Set an Alarm")),
          body: Center(
            child: ElevatedButton(
              child: Text(test),
              onPressed: () {
                print(test + " Button Pressed...");
                setAlarm();
              },
            ),
          ),
          floatingActionButton:
              FloatingActionButton(onPressed: null, child: Icon(Icons.add)),
        );
      }
    
      void setAlarm() async {
        print("setAlarm");
        final int alarmID = 1;
        await AndroidAlarmManager.oneShot(Duration(minutes: 1), alarmID, playAlarm);
      }
    
      void playAlarm() {
        print("playAlarm");
        setState(() {
          test = "Pressed!";
        });
      }
    }
    

    I manage to get the alarm service started, but beyond that, nothing. I have tried initializing the AndroidAlarmManager object both in main and in setAlarm, tried moving around ensureInitialized, tried setting different durations in oneShot, tried changing the ID, and tried firing a more simple alarm function. No matter what I do, the alarm wont set or fire.

    I'm pretty sure its something simple, but for a core function of android, there is no real documentation on how to use it to speak of.

    Does anyone know what android alarm manager plus wants that I'm not providing, here?

  • RNPF
    RNPF about 2 years
    Setting it static did work, yes. I'll need to read up on isolates.