gdelataillade / alarm

A Flutter plugin to easily manage alarms on iOS and Android
https://pub.dev/packages/alarm
MIT License
132 stars 86 forks source link

Alarm.ringStream.stream.listen((_) not triggered when app is in background #106

Closed msh-nayan closed 1 year ago

msh-nayan commented 1 year ago

Alarm plugin version alarm: ^1.2.2

Describe the bug To run some code when alarm starts ringing, we've implemented Alarm.ringStream.stream.listen((_) but it does not triggered when the application is on background.

Example scenario: If we set alarm on 20:00, - If we remain on the app (if app is in foreground), then alarm is triggered properly & Alarm.ringStream.stream.listen((_) is triggered as expected - If we exit from the app (by pressing HOME button of device) and keep the app on background, then then alarm is triggered properly but Alarm.ringStream.stream.listen((_) is NOT triggered, hence can't do specific tasks on that scenario - If we open the app after passing alarm time, then Alarm.ringStream.stream.listen((_) is triggered

N.B. I'VE TESTED THIS on iOS 8

To Reproduce Steps to reproduce the behavior:

  1. Form an alarm settings
    AlarmSettings tourAlarmSettings(int id, DateTime dateTime) {
    final alarmSettings = AlarmSettings(
        id: id,
        dateTime: dateTime,
        loopAudio: false,
        vibrate: false,
        fadeDuration: 0,
        volumeMax: false,
        notificationTitle: '',
        notificationBody: '',
        assetAudioPath: 'assets/alarm_tune.mp3',
        stopOnNotificationOpen: false,
        enableNotificationOnKill: false,);
    return alarmSettings;
    }
  2. Set an alarm
    Future<bool> setAlarm(AlarmSettings alarmSettings) {
    return Alarm.set(alarmSettings: alarmSettings);
    }
  3. Integrate Alarm.ringStream.stream.listen((_)
    
    static StreamSubscription? subscription;

subscription ??= Alarm.ringStream.stream.listen((alarmSettings) { debugPrint('subscription >> alarm_settings >> ${alarmSettings.id} >> ${alarmSettings.toString()}'); });



**Expected behavior**
*Whenever alarm is triggered, either on foreground or background, app will be able to catch  `Alarm.ringStream.stream.listen((_)` so that it can perform some task on that scenario*

**Screenshots**

**Device info**
*iPhone 8*

**Additional context**
*I've tested with your latest version alarm: ^2.1.0 but using this version cause another problem to build AlarmSetting (#103). I assume, even if I use the latest version, I'll have the same problem that I mentioned in this thread.*

I appreciate your effort & looking forward to hear your feedback.
Thanks.
gdelataillade commented 1 year ago

Hello @msh-nayan,

Thanks for your interest in the plugin.

Regarding your concern, it's important to note that on iOS, Flutter code typically cannot execute once the app has been backgrounded for more than 30 seconds. The intended function of this listener callback is to execute code when the app transitions back to the foreground.

If there's anything more you'd like to know or any other aspect of the issue you'd like to discuss, please feel free to ask and reopen the issue.