Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Local notification clarification
Started by ammar71 Oct 24 2019 03:00 PM

3 replies to this topic
notification

Best Answer bamazy , 24 October 2019 - 11:00 PM

ammar71, I use the below code for an app to set notifications. I have changed a little here to meet your requirements. Maybe there is more optimized way to set repeat notifications. But in case no one writes a better solution, you can use something like the below code:

        -- schedule notifications for the next 7 days
        -- tomorrow or today at 8 or 9 in the morning
        local idx
        if tonumber(os.date("%H")) >= 08 then
            -- when the app is installed for the 1st time or opened again sometime,
            -- if it's already after 8, we don't want to set a notification for today
            idx = 1
        else
            idx = 0
        end

        -- get current time
        local t = os.date( "*t" )
        local timeNow = os.time( t )

        -- when the user opens the app, we cancel all the previously set notifications
        -- and set again for the next 7 days
        notifications.cancelNotification()

        local alert = 'Time to do 10 push-ups?'

        -- we wan't to set notification for the next 7 days
        for i = idx, 7, 1 do             
            -- Schedule a notification
            local scheduleTime = os.date( "*t", os.time()+24*60*60*i ) 
            local t2 = {}
            t2.year = scheduleTime.year
            t2.month = scheduleTime.month
            t2.day = scheduleTime.day
            -- we need to choose 8 or 9 depending on the day of the week
            if scheduleTime.wday == 2 or scheduleTime.wday == 4 or scheduleTime.wday == 6 then
               t2.hour = 9
            elseif scheduleTime.wday == 3 or scheduleTime.wday == 5 or scheduleTime.wday == 7 then
               t2.hour = 8
               alert = 'Time to do 7 sit-ups?' 
            end
            t2.min = 0
            t2.sec = 0

            local timeDifference = os.difftime( os.time( t2 ), timeNow )

            -- Set up notification options
            local options = {
                alert = alert
            }

            -- no notification for sunday
            if scheduleTime.wday ~= 1 then
               notifications.scheduleNotification( timeDifference, options )
            end

        end

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 60 posts
  • Corona SDK

Hi,

 

I would like to use "local notifications" in my next app. I read the docs at

https://docs.coronalabs.com/guide/events/appNotification/index.html

 

But I still have a couple of questions.

1- The system seems to work based on setting a notification time/date in the future. How would I go about setting a repeat notification? For example, if I want the app to remind me every Mon/Wed/Fri @ 9:00 am to do 10 push ups and on Tue/Thu/Sat @ 8:00 am to do 7 sit ups. Is there a way to set repeat notifications?

 

2- The Docs say we need to use Coordinated Universal Time (UTC). Let's say I ask the users to enter when they wanted the notification to trigger. The user will use a drop down box to enter hour/minute in local time. How do I convert the user's entered time to UTC?

 

One way I can think of for doing number 1 above is to wait for one notification to trigger and schedule the next notification in the event Listener for the first event. But if the user ignores the notification for one day or just dismisses it instead of clicking on it, that would stop all notifications.

 

Thanks.



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,300 posts
  • Enterprise

There isn't a way to automatically set a repeating timer with the plugin. You would just have to set individual notifications.

 

You can use the various os.date() and os.time() functions to get the number of seconds in the future for any date/time and set it that way. Or you can use UTC. There is a tutorial on working with dates and times here: https://docs.coronalabs.com/tutorial/data/timeDates/index.html

 

Rob


  • ammar71 likes this

[TOPIC: post.html]
#3

bamazy

[GLOBAL: userInfoPane.html]
bamazy
  • Contributor

  • 107 posts
  • Corona SDK

  Best Answer

ammar71, I use the below code for an app to set notifications. I have changed a little here to meet your requirements. Maybe there is more optimized way to set repeat notifications. But in case no one writes a better solution, you can use something like the below code:

        -- schedule notifications for the next 7 days
        -- tomorrow or today at 8 or 9 in the morning
        local idx
        if tonumber(os.date("%H")) >= 08 then
            -- when the app is installed for the 1st time or opened again sometime,
            -- if it's already after 8, we don't want to set a notification for today
            idx = 1
        else
            idx = 0
        end

        -- get current time
        local t = os.date( "*t" )
        local timeNow = os.time( t )

        -- when the user opens the app, we cancel all the previously set notifications
        -- and set again for the next 7 days
        notifications.cancelNotification()

        local alert = 'Time to do 10 push-ups?'

        -- we wan't to set notification for the next 7 days
        for i = idx, 7, 1 do             
            -- Schedule a notification
            local scheduleTime = os.date( "*t", os.time()+24*60*60*i ) 
            local t2 = {}
            t2.year = scheduleTime.year
            t2.month = scheduleTime.month
            t2.day = scheduleTime.day
            -- we need to choose 8 or 9 depending on the day of the week
            if scheduleTime.wday == 2 or scheduleTime.wday == 4 or scheduleTime.wday == 6 then
               t2.hour = 9
            elseif scheduleTime.wday == 3 or scheduleTime.wday == 5 or scheduleTime.wday == 7 then
               t2.hour = 8
               alert = 'Time to do 7 sit-ups?' 
            end
            t2.min = 0
            t2.sec = 0

            local timeDifference = os.difftime( os.time( t2 ), timeNow )

            -- Set up notification options
            local options = {
                alert = alert
            }

            -- no notification for sunday
            if scheduleTime.wday ~= 1 then
               notifications.scheduleNotification( timeDifference, options )
            end

        end

  • ammar71 likes this

[TOPIC: post.html]
#4

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 60 posts
  • Corona SDK

Thanks @Rob and @bamazy

 

I'll experiment with bamazy's code.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: notification