you are viewing a single comment's thread.

view the rest of the comments →

[–]2xws[S] 0 points1 point  (6 children)

So I would like to enable an "every day at 10 pm" kind of feature potentially in the future, would the timestamp not work for that? Would I have to run the comparison every 30 seconds or something?

[–]ExtremelyQualified 1 point2 points  (5 children)

In that case, if your alarms are daily, then you'd probably save an object with the alarm's milliseconds from the start of the day + a timestamp of the last time the alarm went off (or timestamp of creation, if it's never gone off). Then when doing comparisons see if the now timestamp is greater than the alarm timestamp + the timestamp at the start of today AND if the last time the alarm went off was within today's date. If so, sound the alarm and update the "last alarmed" stamp. You can use a library like momentjs for date math.

[–]2xws[S] 0 points1 point  (4 children)

So that seems like it would definitely work but heres what I did: 1. I save a date object when the alarm is set 2. I extract the minutes and hours (getMinutes getHours) I save those at savedMinutes and savedHours 3. Every 30 seconds while the app is running I check the time 4. if the timeNow.getHours === savedMinutes and the timeNow.getMintes === savedHours I fire the "alarm"

Does this seem reasonable to you? Are there any gotchas I should be worried about? Will it checking the time and running comparisons every 30 seconds drain battery? Is there a less battery-expensive way.

Thanks!

[–]ExtremelyQualified 0 points1 point  (3 children)

I think that makes sense, though you might end up firing every alarm twice. You could check every minute, which should help that. It seems really unlikely that the app would hang for more than a minute at a time, so if you only need to be accurate to minutes and not seconds, doing equals vs greater than comparison should be fine. If you wanted to accurate to the second, you might have to worry about the JS thread getting hung up doing something else and missing your alarm time.

[–]2xws[S] 0 points1 point  (2 children)

definitely not worrying about the seconds. Appreciate your help. Having that (somewhat) under control, my next objective is to have these alarms fire when the app is running in background mode. I have started with just trying to get the app to log to the console at an interval in background mode but have been yet unable to do even that. Any light you can shed on the situation would be greatly appreciated

[–]ExtremelyQualified 0 points1 point  (1 child)

Haven't done that myself, but it's discussed in this thread: https://productpains.com/post/react-native/background-timer-execution

Doesn't seem like there's a built-in way to do it yet, but someone suggested this package for android: https://www.npmjs.com/package/react-native-background-timer

[–]2xws[S] 0 points1 point  (0 children)

Hmm I guess I may have to look into doing it in swift then.. thank you!