5

If anyone gets confused and thinks that this is a duplicate of my question from yesterday, it's not. There I was asking how to call a function every day, here I am asking how to call a notification at a specific time every day.

I am looking for a way to repeat a local notification every day at 7.00AM. I currently have this code setup to get the day, month, year etc.

let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: date)
    let hour = components.hour
    let minutes = components.minute
    let month = components.month
    let year = components.year
    let day = components.day

How do I call a notification every day when the time is 7.00 AM?

  • You know it usually helps to look at the documentation, as can be seen there is a repeatInterval property https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/ – Gruntcakes Jul 01 '15 at 19:37
  • I know about that, but I am stuck on getting it to repeat at 7.00. –  Jul 01 '15 at 19:38
  • No, there I was asking how to run a function, this is how to set a notification to run at a certain time every day. –  Jul 01 '15 at 19:40
  • @ZacharyTurner: ok sorry - I suggest to make that explicit at the top of your question – Antonio Jul 01 '15 at 19:41
  • 1
    http://stackoverflow.com/a/27367185/2303865 – Leo Dabus Jul 01 '15 at 20:49

2 Answers2

5

First create the calendar object as you did:

var calendar = NSCalendar()
var calendarComponents = NSDateComponents()
calendarComponents.setHour(7)
calendarComponents.setSeconds(0)
calendarcomponents.setMinutes(0)
calendar.setTimeZone(NSTimeZone.defaultTimeZone)
var dateToFire = calendar.dateFromComponents(calendarComponents)

Now we can schedule the notification daily.

localNotification.fireDate = dateToFire
localNotification.setTimeZone(NSTimeZone.defaultTimeZone)
localNotification.setRepeatInterval(kcfCalendarUnitDay)

Syntax might not be perfect, I was translating from Obj-C, but you should get the general idea.

pbush25
  • 5,228
  • 2
  • 26
  • 35
  • 1
    What is the `localNotification` variable.. You should update your answer to show what the type is and how you created it so that the OP can easily reproduce the example code you have provided. – Johnathon Sullinger Jul 02 '15 at 02:35
-3
    var now = NSDate()
    var calendar = NSCalendar.currentCalendar()
    var components = calendar.components(.CalendarUnitHour | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: now)
    var d = NSCalendar.currentCalendar().dateFromComponents(components)
    if components.hour > 6 {
        d = NSDate(timeInterval: 60*60*24, sinceDate: d!)
    }


    var n = UILocalNotification()
    n.fireDate = d
    n.repeatInterval = NSCalendarUnit.CalendarUnitDay
    n.alertBody = "This is dayly notification"
    UIApplication.sharedApplication().scheduleLocalNotification(n)
Yedidya Reiss
  • 5,316
  • 2
  • 17
  • 19
  • What do you mean at the top when you say 'set the next 7.00 date'? How do I set the 7.00 date? –  Jul 01 '15 at 19:50
  • @Yedidya his question specifically asks how to set the date, putting a piece of code with a comment that says "set the next 7:00 date" is less than helpful. – pbush25 Jul 01 '15 at 19:53
  • Please add additional information to your answer. Just pasting source code is not enough for an answer. You need to explain what your answer is actually doing and how it can be applied to the actual question, which is pushing a local notification. You have 3K reputation, you should know this. – Johnathon Sullinger Jul 02 '15 at 02:33