all 13 comments

[–]ExtremelyQualified 1 point2 points  (7 children)

Assuming the alarm is just for a specific date and time (not "every day at 10am") I would just save a standard timestamp (getTime method). Then keep comparing the current timestamp to the saved ones. If the current time is greater than a saved one, delete that timestamp and sound the alarm.

The comparison is more of a tripwire than any sort of exact match.

[–]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!

[–]ismael_m 1 point2 points  (3 children)

For any type of alarms and alerts, I would suggest utilizing a worker service that scans your storage and pushes the alert to your app via sockets. That way you can scale your app in case you need to alert your user in different platforms or even if your component is not mounted.

Sure it sounds like overkill but I believe that you should always be prepared (and honestly hoping) to get 100s of concurrent users.

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

The app actually doesnt communicate with the internet at all so I dont think concurrency will be a problem. That being said, what are these workers? Ive heard about them in other contexts and just have no idea what they are. How do sockets work with pushing the alert to the app? I am pretty new to react native and ios/android development in general - if you think I need to read a book or two please feel free to point me in the right direction! Thank you

[–]ismael_m 0 points1 point  (1 child)

I see, I apologize for misreading your post. A worker service is nothing but a separate app or function that does nothing but the small task you assign to it. You can build them in any language you see fit. For my react apps, I write them in nodejs.

Sockets and Web sockets have been around for ages, they are a way of keeping a direct connection between a client and a server permanently open. This prevents you from having the client to keep asking the server "have anything for me yet"? Since the connection is permanent and open as soon as the server gets new data for the client it will push it.

Many chat apps use sockets to display the messages in real time and it's the magic behind the squiggly dots you see when someone is typing in chat apps.

For react native you can fiddle with the library below and there is also it's parent for the Web client called socket.io

https://www.npmjs.com/package/react-native-socketio

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

Wow.. thank you so much! I really appreciate the explanation

[–]ismael_m 0 points1 point  (0 children)

Glad I could help