I spent whole weekend in experimenting with android_alarm_manager plug-in in flutter and finally got it working after firebase setup, but not exactly.

Issue 1: AndroidAlarmManager.periodic invokes in approximately 1 minute regardless of duration what I set - (5 secs/ 15 secs/ 30 Secs).

Issue 2: Anyway it uses to run every minute in foreground (even if the device is locked), but doesn't work when in background (on home click). Though worked for couple of minutes when pressed back button.

Background running is what expected from this plugin. But I have no idea why it is behaving in this weird way. Any help would be appreciated. Please let me know if any of you facing the same issue or I am the only one.

Steps Followed:

  1. pubspec.yaml - android_alarm_manager, firebase_auth, google_sign_in.
  2. Addition of google-services.json and com.google.gms.google-services in both build.gradle files.
  3. Addition of service tag in manifest file.
  4. Creating Application.java class in src and addition of same (.Appplication) in manifest file.
  5. main.dart from example file provided.
  6. reference

main.dart -

import 'dart:async';
import 'dart:isolate';

import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/widgets.dart';

final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
FirebaseUser firebaseUser;

Future<void> ensureFirebaseUser() async {
  if (firebaseUser == null) {
    firebaseUser = await firebaseAuth.currentUser();
    if (firebaseUser == null) {
      firebaseUser = await firebaseAuth.signInAnonymously();
    }
  }
}

class HelloMessage {
  HelloMessage(this._now, this._msg, this._isolate, this._user, this._token);

  final DateTime _now;
  final String _msg;
  final int _isolate;
  final FirebaseUser _user;
  final String _token;

  @override
  String toString() {
    return "[$_now] $_msg "
        "isolate=$_isolate "
        "user='$_user' "
        "token=$_token";
  }
}

void printHelloMessage(String msg) {
  ensureFirebaseUser().then((_) {
    firebaseUser.getIdToken().then((String idToken) {
      print(HelloMessage(
        DateTime.now(),
        msg,
        Isolate.current.hashCode,
        firebaseUser,
        idToken,
      ));
    });
  });
}

void printHello() {
  printHelloMessage("Hello, world!");
}

void printGoodbye() {
  printHelloMessage("Goodbye, world!");
}

bool oneShotFired = false;

void printOneShot() {
  printHelloMessage("Hello, once!");
}

Future<void> main() async {
  final int helloAlarmID = 0;
  final int goodbyeAlarmID = 1;
  final int oneShotID = 2;

  // Start the AlarmManager service.
  await AndroidAlarmManager.initialize();

  printHelloMessage("Hello, main()!");
  runApp(const Center(
      child: Text('Hello, world!', textDirection: TextDirection.ltr)));
  await AndroidAlarmManager.periodic(
      const Duration(seconds: 5), helloAlarmID, printHello,
      wakeup: true);
  await AndroidAlarmManager.oneShot(
      const Duration(seconds: 5), goodbyeAlarmID, printGoodbye);
  if (!oneShotFired) {
    await AndroidAlarmManager.oneShot(
        const Duration(seconds: 5), oneShotID, printOneShot);
  }
}

Console O/P -

I/flutter ( 4893): [2018-11-26 01:27:56.093322] Hello, world! isolate=289040868 user='FirebaseUser({uid: .......
I/flutter ( 4893): [2018-11-26 01:27:56.120643] Goodbye, world! isolate=289040868 user='FirebaseUser({uid: .....
I/flutter ( 4893): [2018-11-26 01:27:56.123499] Hello, once! isolate=289040868 user='FirebaseUser({uid: ......
I/flutter ( 4893): [2018-11-26 01:28:57.860115] Hello, world! isolate=289040868 user='FirebaseUser({uid: ......
I/flutter ( 4893): [2018-11-26 01:30:00.037481] Hello, world! isolate=289040868 user='FirebaseUser({uid: .....
I/flutter ( 4893): [2018-11-26 01:31:08.422318] Hello, world! isolate=289040868 user='FirebaseUser({uid: .....
I/flutter ( 4893): [2018-11-26 01:32:08.545903] Hello, world! isolate=289040868 user='FirebaseUser({uid: .....

Though I still don't understand why firebase is being used with this plug-in, seems like I have done everything right btw. Thanks.