Get Local Notification Daily and Weekly between 2 dates

Hi guys,

I have an app where the user can set a Start Date and End Date and the time of reveiving the Local Notification, for example: Start = 1/11/2015, End = 30/11/2015, Time for Notification = 8PM (DD-MM-YYYY)

What I think I have now, is the time between those dates in seconds, with the help of google (see code for calculations). How can I make the Local Notification be received daily and weekly between those 2 dates (start and end date) at 8PM.

If the calculation is not accurate enough, could I get some help with that too?

local currentDate = os.date( '\*t' ) local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } local endTime = os.time{ year = myApp.ey, month = myApp.em, day = myApp.ed, hour = myApp.h, min = myApp, sec = 0 } local differenceInSec = ( endTime - setTime ) local days = differenceInSec / 86400 local hours = days%1 \* 24 local minutes = hours%1 \* 60 local seconds = math.floor( minutes%1 \* 60 + 0.5 ) print( differenceInSec, math.floor( days ), math.floor( hours ), math.floor( minutes ) ) local dayNoti = differenceInSec / 86400 -- Amount of days between the calc dates local weekNoti = differenceInSec / 604800 -- Amount of weeks between the calc dates local notification = notifications.scheduleNotification( ??, notiOptions )

This might be easier to get the start and end time in seconds, get the number of days like your doing, but you need to run it through math.floor() to get an integer. 

local days = mod.floor( differenceInSec / 86400 )

then you can do a simple loop:

local notificationDate = setTime for i = 1, days do     notifications.scheduleNotification( )     notificationDate = notificationDate + 86400 local utcTime = os.date( "!\*t", notificationDate ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

Or something like that.

Thanks for your reply. This way Im not getting a Notification at all. I printed the results in the console, outside the for loop, but it printed all the notifications in one go. Shouldnt it be like, first 1 time and after 86400 seconds another one?

Do you maybe know why I cant get a notification with your solution?

EDIT: notifications.scheduleNotification( ) < this line will make the notifications received doubled right? Because Im having 2x notifications scheduled in the FOR loop.

It seems like its acting how it should in the Simulator, but its just that I dont receive any notifictions on my phone.

EDIT 2: Whenever I replace the utcTime with 5 seconds “local notification = notifications.scheduleNotification( 5, notiOptions )”, it does give me a notification on the phone, but all in on go.

I tried some stuff, but didnt work. I think that the problem with your given solution is in the “notificationDate = notificationDate + 86400”, probabaly with the setTime. It doesnt seem to react to that, but it does give me the notifications in the Simulator.

I see why you’re getting doubles:

local notificationDate = setTime for i = 1, days do &nbsp; &nbsp; -- notifications.scheduleNotification( ) -- kill this line. &nbsp; &nbsp; notificationDate = notificationDate + 86400 &nbsp;&nbsp;&nbsp; local utcTime = os.date( "!\*t", notificationDate ) &nbsp;&nbsp;&nbsp; local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

Here some code I just ran and I got an alert every 10 seconds (since thats what I told it to do)

local notifications = require( "plugin.notifications" ) local json = require( "json" ) --[[local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } local endTime = os.time{ year = myApp.ey, month = myApp.em, day = myApp.ed, hour = myApp.h, min = myApp, sec = 0 } local differenceInSec = ( endTime - setTime ) local days = differenceInSec / 86400 local hours = days % 1 \* 24 local minutes = hours % 1 \* 60 local seconds = math.floor( minutes % 1 \* 60 + 0.5 ) print( differenceInSec, math.floor( days ), math.floor( hours ), math.floor( minutes ) ) --]] local notiOptions = { alert = "Wake up!", custom = { foo = "bar" } } --local notificationDate = startTime local notificationDate = os.time() --days = math.floor( days ) --for i = 1, days do for i = 1, 10 do --notificationDate = notificationDate + 86400 notificationDate = notificationDate + 10 print("notificationDate", notificationDate) local utcTime = os.date( "!\*t", notificationDate ) print(json.prettify( utcTime ) ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

I don’t have your variables. But you can see the differences that I commented out.

Rob

Thanks for your help and time! I tried your way and I too receive every notification after 10 seconds. I changed some of yours to mine (still almost the same). 

local currentDate = os.date( '\*t' ) local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } local endTime = os.time{ year = myApp.ey, month = myApp.em, day = myApp.ed, hour = myApp.h, min = myApp.min, sec = 0 } local timeTillDateInSecs = setTime - os.time( currentDate ) local timeTillDateInDays = math.floor( ( setTime - os.time( currentDate ) ) / 86400 ) local differenceInSec = ( endTime - setTime ) local days = math.floor( differenceInSec / 86400 ) local weeks = math.floor( differenceInSec / 604800 ) --local notificationDate = setTime --local notificationDate = os.time() for i = 1, days do local notificationDate = setTime + 10 print("notificationDate", notificationDate) local utcTime = os.date( "!\*t", notificationDate ) print(json.prettify( utcTime ) ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

I think I know why I didnt receive them, I didnt change the seconds to +86400, but let it be on 10. So when the setTime hits, it did +86400, so I actually get the notifcation the next day (miss calculations on my end). I did testing with 10 seconds, but I am getting the notifications all in once after 10 seconds, for example days between the two dates is 5, I got 4 notifications all in once. 

Shouldnt it be the same results if I use ( setTime are values from a pickerwheel in a different .lua (overlay) )

local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } local notificationDate = setTime + 10

instead of 

local notificationDate = os.time() notificationDate = notificationDate + 10

Because setTime is getting the time of the user and os.time() gets the current time, but with setTime I am getting all the notifications in once. 

You have to initialize notificationTime outside the loop to your starting time (the time you want your first alert). Inside the loop, set the notification, then add a day (86400 seconds) to notificationTime. When the loops runs again, a new alert a day later will be set.

I used 10 seconds because I wasn’t going wait several days to test this.

Rob

Yes, I understand, thank you. But the weird thing that I have now is, whenever I initialize

local notificationDate = setTime

I receive all the notifications at once, but whenever I use the 

local notificationDate = os.time()

I get the notifications like I’m suppose to, its behaving like it should be, after every 10 seconds… So I guess there’s something wrong with the os.time table I am using.

local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 }

I use a 3 pickerWheels for 3 different notifications, I save the time and date to a database. I pass the 3 pickerhweels values as params to this .lua (where I set the notifications) where I initialize them as myApp.y etc, Could it be that its ‘interfering’ with each other, that it is a better idea to get the times from the database instead?

I think I got what I want now. Here’s what I have:

local currentDate = os.date( '\*t' ) local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } - 86400 local endTime = os.time{ year = myApp.ey, month = myApp.em, day = myApp.ed, hour = 0, min = 0, sec = 0 } local timeTillDateInSecs = setTime - os.time( currentDate ) local differenceInSec = endTime - setTime local days = math.floor( differenceInSec / 86400 ) local weeks = math.floor( differenceInSec / 604800 ) local notificationDate = setTime local notificationDate1 = os.time() for i = 1, days do notificationDate = notificationDate + 86400 print("notificationDate", notificationDate) local utcTime = os.date( "!\*t", notificationDate ) print( json.prettify( utcTime ) ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

In local setTime I did, -86400, because it wasn’t inclusing the day the notification has been set on, for example, it is set on today 8PM, it skipped that day (today). Im getting the data from the os.time table directly from the database, thought that would probably be a better idea. First saving the values from the pickerWheel in the database, then getting pulling them into the os.time table.

Till now everything works fine. Im now doing tests on receiving daily notifications between set- and end date.

Thanks for the help Rob. I’d like to have your opinion on if getting the data from the database woul be better than directly from the pickerWheel itself. 

Print out your values and make sure you’re getting what you expect. You can also print out the value of setTime which should be a value close to the value of os.time() + the number of seconds of the notifications.

Rob

This might be easier to get the start and end time in seconds, get the number of days like your doing, but you need to run it through math.floor() to get an integer. 

local days = mod.floor( differenceInSec / 86400 )

then you can do a simple loop:

local notificationDate = setTime for i = 1, days do &nbsp; &nbsp; notifications.scheduleNotification( ) &nbsp; &nbsp; notificationDate = notificationDate + 86400 local utcTime = os.date( "!\*t", notificationDate ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

Or something like that.

Thanks for your reply. This way Im not getting a Notification at all. I printed the results in the console, outside the for loop, but it printed all the notifications in one go. Shouldnt it be like, first 1 time and after 86400 seconds another one?

Do you maybe know why I cant get a notification with your solution?

EDIT: notifications.scheduleNotification( ) < this line will make the notifications received doubled right? Because Im having 2x notifications scheduled in the FOR loop.

It seems like its acting how it should in the Simulator, but its just that I dont receive any notifictions on my phone.

EDIT 2: Whenever I replace the utcTime with 5 seconds “local notification = notifications.scheduleNotification( 5, notiOptions )”, it does give me a notification on the phone, but all in on go.

I tried some stuff, but didnt work. I think that the problem with your given solution is in the “notificationDate = notificationDate + 86400”, probabaly with the setTime. It doesnt seem to react to that, but it does give me the notifications in the Simulator.

I see why you’re getting doubles:

local notificationDate = setTime for i = 1, days do &nbsp; &nbsp; -- notifications.scheduleNotification( ) -- kill this line. &nbsp; &nbsp; notificationDate = notificationDate + 86400 &nbsp;&nbsp;&nbsp; local utcTime = os.date( "!\*t", notificationDate ) &nbsp;&nbsp;&nbsp; local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

Here some code I just ran and I got an alert every 10 seconds (since thats what I told it to do)

local notifications = require( "plugin.notifications" ) local json = require( "json" ) --[[local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } local endTime = os.time{ year = myApp.ey, month = myApp.em, day = myApp.ed, hour = myApp.h, min = myApp, sec = 0 } local differenceInSec = ( endTime - setTime ) local days = differenceInSec / 86400 local hours = days % 1 \* 24 local minutes = hours % 1 \* 60 local seconds = math.floor( minutes % 1 \* 60 + 0.5 ) print( differenceInSec, math.floor( days ), math.floor( hours ), math.floor( minutes ) ) --]] local notiOptions = { alert = "Wake up!", custom = { foo = "bar" } } --local notificationDate = startTime local notificationDate = os.time() --days = math.floor( days ) --for i = 1, days do for i = 1, 10 do --notificationDate = notificationDate + 86400 notificationDate = notificationDate + 10 print("notificationDate", notificationDate) local utcTime = os.date( "!\*t", notificationDate ) print(json.prettify( utcTime ) ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

I don’t have your variables. But you can see the differences that I commented out.

Rob

Thanks for your help and time! I tried your way and I too receive every notification after 10 seconds. I changed some of yours to mine (still almost the same). 

local currentDate = os.date( '\*t' ) local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } local endTime = os.time{ year = myApp.ey, month = myApp.em, day = myApp.ed, hour = myApp.h, min = myApp.min, sec = 0 } local timeTillDateInSecs = setTime - os.time( currentDate ) local timeTillDateInDays = math.floor( ( setTime - os.time( currentDate ) ) / 86400 ) local differenceInSec = ( endTime - setTime ) local days = math.floor( differenceInSec / 86400 ) local weeks = math.floor( differenceInSec / 604800 ) --local notificationDate = setTime --local notificationDate = os.time() for i = 1, days do local notificationDate = setTime + 10 print("notificationDate", notificationDate) local utcTime = os.date( "!\*t", notificationDate ) print(json.prettify( utcTime ) ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

I think I know why I didnt receive them, I didnt change the seconds to +86400, but let it be on 10. So when the setTime hits, it did +86400, so I actually get the notifcation the next day (miss calculations on my end). I did testing with 10 seconds, but I am getting the notifications all in once after 10 seconds, for example days between the two dates is 5, I got 4 notifications all in once. 

Shouldnt it be the same results if I use ( setTime are values from a pickerwheel in a different .lua (overlay) )

local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } local notificationDate = setTime + 10

instead of 

local notificationDate = os.time() notificationDate = notificationDate + 10

Because setTime is getting the time of the user and os.time() gets the current time, but with setTime I am getting all the notifications in once. 

You have to initialize notificationTime outside the loop to your starting time (the time you want your first alert). Inside the loop, set the notification, then add a day (86400 seconds) to notificationTime. When the loops runs again, a new alert a day later will be set.

I used 10 seconds because I wasn’t going wait several days to test this.

Rob

Yes, I understand, thank you. But the weird thing that I have now is, whenever I initialize

local notificationDate = setTime

I receive all the notifications at once, but whenever I use the 

local notificationDate = os.time()

I get the notifications like I’m suppose to, its behaving like it should be, after every 10 seconds… So I guess there’s something wrong with the os.time table I am using.

local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 }

I use a 3 pickerWheels for 3 different notifications, I save the time and date to a database. I pass the 3 pickerhweels values as params to this .lua (where I set the notifications) where I initialize them as myApp.y etc, Could it be that its ‘interfering’ with each other, that it is a better idea to get the times from the database instead?

I think I got what I want now. Here’s what I have:

local currentDate = os.date( '\*t' ) local setTime = os.time{ year = myApp.y, month = myApp.m, day = myApp.d, hour = myApp.h, min = myApp.min, sec = 0 } - 86400 local endTime = os.time{ year = myApp.ey, month = myApp.em, day = myApp.ed, hour = 0, min = 0, sec = 0 } local timeTillDateInSecs = setTime - os.time( currentDate ) local differenceInSec = endTime - setTime local days = math.floor( differenceInSec / 86400 ) local weeks = math.floor( differenceInSec / 604800 ) local notificationDate = setTime local notificationDate1 = os.time() for i = 1, days do notificationDate = notificationDate + 86400 print("notificationDate", notificationDate) local utcTime = os.date( "!\*t", notificationDate ) print( json.prettify( utcTime ) ) local notification = notifications.scheduleNotification( utcTime, notiOptions ) end

In local setTime I did, -86400, because it wasn’t inclusing the day the notification has been set on, for example, it is set on today 8PM, it skipped that day (today). Im getting the data from the os.time table directly from the database, thought that would probably be a better idea. First saving the values from the pickerWheel in the database, then getting pulling them into the os.time table.

Till now everything works fine. Im now doing tests on receiving daily notifications between set- and end date.

Thanks for the help Rob. I’d like to have your opinion on if getting the data from the database woul be better than directly from the pickerWheel itself. 

Print out your values and make sure you’re getting what you expect. You can also print out the value of setTime which should be a value close to the value of os.time() + the number of seconds of the notifications.

Rob