Skip to content

android_alarm_manager plugin: second alarm doesn't fire when main view is opened after first alarm #44264

@vkammerer

Description

@vkammerer

Hello Flutter team,

I have been developing an app using the android_alarm_manager plugin and noticed a nasty bug that happens on specific conditions.

The bug is the following: an alarm (A1) will not fire if:

  • the initial flutter application that set it is closed before another previous alarm (A0)
  • then reopened after that other alarm A0 fires
  • and left open until A1 is set to fire.

In order to isolate it in its most minimal and easily reproducible way, I have used the example project listed in the plugin directory and updated the API calls and the layout to add a button.

I have also used a simple python http server to log messages from the alarm isolates. Maybe there is an easier way to log messages than this after the debug app has disconnected?

Reproducing steps - installation:

http: ^0.12.0+2
  • replace the content in lib/main.dart with the following:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:android_alarm_manager/android_alarm_manager.dart';
import 'package:http/http.dart' as http;

const String YOUR_IP = '192.168.178.59';
const String PORT = '8080';

Future<http.Response> logFromAlarm(String msg) async {
  return await http.get('http://$YOUR_IP:$PORT/?$msg');
}

Future<void> main() async {
  await AndroidAlarmManager.initialize();
  runApp(
    Center(
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: RaisedButton(
          onPressed: () async {
            await AndroidAlarmManager.oneShotAt(
              DateTime.now().add(const Duration(seconds: 10)),
              0,
              handleAlarm0,
              alarmClock: true,
            );
            await AndroidAlarmManager.oneShotAt(
              DateTime.now().add(const Duration(seconds: 30)),
              1,
              handleAlarm1,
              alarmClock: true,
            );
          },
          child: const Text('Set alarm', textDirection: TextDirection.ltr),
        ),
      ),
    ),
  );
}

void handleAlarm0() async {
  await logFromAlarm('alarm0');
}

void handleAlarm1() async {
  await logFromAlarm('alarm1');
}
  • open a terminal and launch a simple python http server:
python -m SimpleHTTPServer 8080
  • launch an Android emulator and start the application

Normal workflow:

Once this is done, you can start testing different scenarios, which should all work except for the very precise one which I detailed above. Essentially, the normal workflow is as follows:

  • application is launched
  • user clicks on button and registers alarm A0 and alarm A1
  • alarm A0 fires, logs to python server
  • alarm A1 fires, logs to python server

so after this normal workflow, this is what my screen looks like:

Screen Shot 2019-11-07 at 12 07 25 PM

Bug scenario:

The bug happens when the following steps are done

  • application is launched
  • user clicks on button and registers alarm A0 and alarm A1
  • user closes the application before A0 fires
  • A0 fires, logs to python server
  • user reopens the application from the application list before A1 fires
  • nothing happens: A1 doesn't fire, the python server doesn't log any request

Other working scenarios tested:

Note that many other small variations of this scenario prevent the bug from occurring. I have for example tested the following scenarios which work fine:

  • if the main application is closed and not reopened => A1 fires fine
  • if the main application is closed after A0 fires and reopened => A1 fires fine
  • if the main application is open and closed more than once => A1 fires fine

While it may seem like a corner case, it is actually quite likely to happen often in real life scenarios. If you have any question or need any help to investigate it further, I'd happily comment more on this issue, as this is blocking me from moving forward with my app.

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    p: android_alarm_managerThe Android background execution pluginpackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions