Repeating local notification daily at a set time with swift
I am new to iOS development, but have created the app and I am trying to create a daily notification for a set time. Currently the notification executes once for the given date/time. I am unsure how to use the repeatInterval method to schedule it daily. What is the best method to repeat the notification daily ? any help would be much appreciated (Y).
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 06;
dateComp.day = 03;
dateComp.hour = 12;
dateComp.minute = 55;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!
var notification:UILocalNotification = UILocalNotification()
notification.category = "Daily Quote"
notification.alertBody = quoteBook.randomQuote()
notification.fireDate = date
notification.repeatInterval =
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Solution 1:
You have to provide an NSCalendarUnit value like “HourCalendarUnit” or “DayCalendarUnit” for repeating a notification.
Just add this code to repeat the local notification daily :
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
Solution 2:
So, had to modify the @vizllx's above code slighty. Here is the new line:
notification.repeatInterval = NSCalendarUnit.Day
Here is a full working example I used:
let notification = UILocalNotification()
/* Time and timezone settings */
notification.fireDate = NSDate(timeIntervalSinceNow: 8.0)
notification.repeatInterval = NSCalendarUnit.Day
notification.timeZone = NSCalendar.currentCalendar().timeZone
notification.alertBody = "A new item is downloaded."
/* Action settings */
notification.hasAction = true
notification.alertAction = "View"
/* Badge settings */
notification.applicationIconBadgeNumber =
UIApplication.sharedApplication().applicationIconBadgeNumber + 1
/* Additional information, user info */
notification.userInfo = [
"Key 1" : "Value 1",
"Key 2" : "Value 2"
]
/* Schedule the notification */
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}