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

Make alarm periodic #47

Open moosalim opened 1 year ago

moosalim commented 1 year ago

Hello

Thanks for the plugin, appreciate your effort. The alarm is not periodic, it just ring one time. Whereas I believe it should ring every day on the same time, e.g morning wakup every day. Is this a bug or a feature to be added ?

gdelataillade commented 1 year ago

Hi! Thanks for your interest in the package.

Yes, this is a feature that could be added. I'll add it when I find the time to do it. I'll let you know.

meliksahcakirr commented 1 year ago

I don't think it should be the libraries responsibility. Normally, when get a callback on in our app regarding our alarm went off, we can register the new one again.

gdelataillade commented 1 year ago

I agree with you @meliksahcakirr, it would make more sense.

@moosalim what do you think ?

moosalim commented 1 year ago

@gdelataillade I agree, this is good idea, @meliksahcakirr thanks for the suggestion

gdelataillade commented 1 year ago

Awesome. I'll close the issue then. Don't hesitate to open another one if you have any other question.

azmasamy commented 1 year ago

Can't agree with the conclusion you guys reached for this feature. What if the user didn't open the app when the alarm went off? I need to schedule a daily alarm that fires even if the user didn't open the app after the first scheduled alarm

gdelataillade commented 1 year ago

I see. I can make the Android alarm periodic but for the iOS part I don't know if there is a way to execute native code in the background periodically.

I'll take a close look at it when I find some time. Feel free to help if you want.

gdelataillade commented 1 year ago

In summary, while periodic alarms can be implemented on Android, this is not feasible for iOS. To maintain consistency between both platforms, I will not be adding this feature to the package (except if I found a solution). As an alternative, you could store the scheduled days for alarms and reset them for the upcoming week each time the app is launched.

gbbest15 commented 1 year ago

In summary, while periodic alarms can be implemented on Android, this is not feasible for iOS. To maintain consistency between both platforms, I will not be adding this feature to the package (except if I found a solution). As an alternative, you could store the scheduled days for alarms and reset them for the upcoming week each time the app is launched.

i think there should be a work around for iOS or what is the problem why is not feasible on iOS if i may ask.. what of if we use background fetch

gdelataillade commented 1 year ago

Hi @gbbest15

Thank you for your question. Indeed, I am already using background fetch in the application, but the issue lies in the fact that iOS does not provide an API to automatically repeat alarms. This would mean that I would have to manually set alarms for every Monday, for instance, but for what duration? The entire year? That seems impractical.

Additionally, it's important to note that the longer a task remains active in background fetch, the higher the chance that iOS will terminate it. Therefore, it’s better to regularly reschedule these tasks to ensure they function properly.

If you have any ideas on how to circumvent this limitation or improve this functionality, I would be delighted to hear them. Also, feel free to ask any other questions you might have.

gbbest15 commented 1 year ago

Hi @gbbest15

Thank you for your question. Indeed, I am already using background fetch in the application, but the issue lies in the fact that iOS does not provide an API to automatically repeat alarms. This would mean that I would have to manually set alarms for every Monday, for instance, but for what duration? The entire year? That seems impractical.

Additionally, it's important to note that the longer a task remains active in background fetch, the higher the chance that iOS will terminate it. Therefore, it’s better to regularly reschedule these tasks to ensure they function properly.

If you have any ideas on how to circumvent this limitation or improve this functionality, I would be delighted to hear them. Also, feel free to ask any other questions you might have.

okay, now i think i understand, i will also make some research here if i can get somethings... but right now is to trigger it with my backend base on my thinking now.. i will see make research on this..

deep526 commented 10 months ago

Can someone show me the code on how to allow users to repeat alarms daily?

vinayakgupta29 commented 9 months ago

Hey the package is good for some things but there are a few things that should be in the package.

or could you suggest any other way of doing it in flutter with the current package status

gdelataillade commented 9 months ago

Hi @vinayakgupta29 @deep526

Making alarms periodical in the plugin with the workaround is not the priority at the moment. If you need to make alarms periodical, the workaround to add to your app is:

Every time the user opens the app, you need to schedule his alarms for the following 7 days.

Let me know if you have any question.

deep526 commented 8 months ago

Hi @gdelataillade would you happen to have pseudocode to show this in the example file?

gdelataillade commented 8 months ago

Hi @deep526

I will write a Github Gist with an example of a periodic alarm implementation when I have some time. I'll keep you updated here.

deep526 commented 8 months ago

@gdelataillade Yes please that would be amazing. I got custom sounds made for alarms and created the alarm, but my users are complaining they can't repeat the alarm and it's also not stoping when users click the notification.

gdelataillade commented 8 months ago

Hi @deep526

I will write a Github Gist with an example of a periodic alarm implementation when I have some time. I'll keep you updated here.

Hi everyone,

Here's a Github Gist I wrote to give you an idea of how to implement periodic alarms in your app:

https://gist.github.com/gdelataillade/3dae217917d417c5f367557489133619

Please don't hesitate if you have any question.

deep526 commented 8 months ago

hi @gdelataillade thank you for providing the code for periodic alarms. Unfortunately I'm not an expert in flutter so I was unable to implement this into my code. I tried modifying the edit_alarm.dart file and created the ui to show days of the week selected but it was giving errors, it's requiring me to change code on the package files. Hopefully you'll have this option implemented in the example file so it's easier for us self taught coders to implement lol. Thanks for your help, I'll just remove the alarm feature and continue working on trying to make it periodic for now

gdelataillade commented 8 months ago

Hi @deep526, what kind of errors do you have ?

deep526 commented 8 months ago

Hi @gdelataillade, there no error message that shows up however, when alarm rings and user is directed to the ring.dart screen and they press stop. Then the alarm is deleted. I believe you said the alarm needs to rebuild every time the app opens up or something. I'm just not sure how the implementation works.

This is the code I used to allow users input for repeating:

Future setPeriodicAlarms(TimeOfDay time, List selectedDays) async { final nbDays = 7;

final now = DateTime.now();

for (var i = 0; i < nbDays; i++) {
  final dateTime = DateTime(
    now.year,
    now.month,
    now.day,
    time.hour,
    time.minute,
  ).add(Duration(days: i));

  if (selectedDays.contains(dateTime.weekday)) {
    final alarmSettings = AlarmSettings(
      id: dateTime.day,
      dateTime: dateTime,
      assetAudioPath: 'assets/',
      notificationTitle: 'Alarm ringing!',
      notificationBody: 'Periodic alarm ringing, day ${dateTime.day}.',
    );
    await Alarm.set(
      alarmSettings: alarmSettings,
    );
  }
}

}

gdelataillade commented 8 months ago

Hi @deep526

Make sure to call Alarm.init before executing this function. Please carefully read the Readme if it's not already done. Let me know if you still have issues.

steveng15 commented 8 months ago

Hi @deep526

Make sure to call Alarm.init before executing this function. Please carefully read the Readme if it's not already done. Let me know if you still have issues.

I saved Alarm.init in main.dart and the function for alarm repetition is in another file. how to call it?

gdelataillade commented 8 months ago

@steveng15 You should put it in a class like AlarmController, import the class and then call AlarmController.setPeriodicAlarms.

arymuhammad commented 6 months ago

hello, mr i wanna ask, why my alarm always delete when i run this function Alarm.stop(id), i just want to stop my alarm, not delete it

please help me. thanks alot

gdelataillade commented 6 months ago

Hi @arymuhammad

As explained in this issue, it's not possible to set a periodical alarm at the moment, so you have to do it manually. Please read the issue for more information.

a-3isa commented 6 months ago

there is a solution to make the alarm periodic. you can make the stop button stop the current alarm and start a new alarm after 24 hours, and make another button which kill the alarm.

deep526 commented 5 months ago

Yes, I created a separate button and labeled it "Repeat Tomorrow" to allow users to repeat the alarm at the exact same time next day. It just sets a new alarm 24hrs in the future

devnta commented 5 months ago

@gdelataillade Thanks for your support. What happens if the user doesn't launch the app? How do I schedule in this case? p/s: Sorry for my bad English.

gdelataillade commented 5 months ago

Hi @phamquoctrongnta

It is recommended that users launch the app to ensure future alarms are scheduled and active. This serves as a security measure to confirm that alarms are functioning correctly. Additionally, it’s advisable to schedule not only your next alarm but also subsequent ones, especially for periodic alarms.

Hope this helps, let me know if you have any questions.

devnta commented 5 months ago

@gdelataillade If I want to set an alarm for 9:00 a.m. every Friday, how many alarms should I set? Thanks a lot.

gdelataillade commented 5 months ago

@phamquoctrongnta honestly I don't know, I would try to have alarms scheduled for the next 2 Fridays and see how it goes. Don't hesitate to share your feedback, it can be valuable for everyone.

a-3isa commented 5 months ago

@gdelataillade If I want to set an alarm for 9:00 a.m. every Friday, how many alarms should I set? Thanks a lot.

modify the stopAlarm function like this

void stopAlarm(AlarmSettings alarmSettings) { DateTime newdDateTime = alarmSettings.dateTime.add(const Duration(days: 7)); final newalarmSettings = AlarmSettings( id: alarmSettings.id, dateTime: newdDateTime, loopAudio: alarmSettings.loopAudio, vibrate: alarmSettings.vibrate, volume: alarmSettings.volume, assetAudioPath: alarmSettings.assetAudioPath, notificationTitle: alarmSettings.notificationTitle, notificationBody: alarmSettings.notificationBody, enableNotificationOnKill: alarmSettings.enableNotificationOnKill, ); Alarm.stop(alarmSettings.id).then((bool result) { return Alarm.set(alarmSettings: newalarmSettings); }); }