15

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)
Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
JUSDEV
  • 731
  • 1
  • 7
  • 22

4 Answers4

15

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
Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • 3
    For some reason notifications keep getting pushed one after the other. I changed it to minutes to test out but since then notifications keep being pushed even when i changed it back. Any ideas why ? @vizllx – JUSDEV Jun 03 '15 at 14:22
  • I am having the same problem. Does any one know the cause and fix – Joseph Selvaraj Sep 22 '16 at 05:18
  • 2
    If by pushed one after the other, I just found out that local notifications registered in different install sessions persist - If you uninstall and then reinstall the app, the previous notifications (if they haven't fired yet) will ALL fire at once if local notifications are enabled again in the newest install session. Hope this helps someone else. – cloudcal Nov 12 '16 at 22:45
  • Please update your answer for Swift 3 & Swift 4! It's very helpful to the new iOS Developers – Mannopson Sep 26 '17 at 12:16
13

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)
}
Vizllx
  • 9,135
  • 1
  • 41
  • 79
Keith Holliday
  • 1,692
  • 21
  • 15
3

The other answers show how to use the old, pre iOS 10 local notifications. With iOS 10 and later, you must use UNNotificationCenter as follows:

    let content = UNMutableNotificationContent()
    content.title = "This will appear in bold, on it's own line."
    content.subtitle = "This will appear in bold, on it's own line, below the title."
    content.body = "This will appear as normal text, below the title and subtitle."

    let triggerInputForHourlyRepeat = Calendar.current.dateComponents([.minute], from: intendedFireDateVariable)
    let trigger = UNCalendarNotificationTrigger.init(dateMatching: triggerInput, repeats: true)

    let request = UNNotificationRequest(identifier: "someUniqueID", content: content, trigger: trigger)
    let unc = UNUserNotificationCenter.current()
    unc.add(request, withCompletionHandler: { (error) in
        /// Handle error
    })

Helpful tutorials:

  1. https://www.techotopia.com/index.php/An_iOS_10_Local_Notification_Tutorial
  2. https://makeapppie.com/2016/11/21/manage-delete-and-update-notifications-in-ios-10/
Dave G
  • 12,042
  • 7
  • 57
  • 83
  • The question asks about triggering a daily notification at a _specific time_. Your answer doesn't show how to do that. There's no code that shows a specific time being provided. – Steve Jun 06 '21 at 11:48
1

var repeatInterval: NSCalendarUnit

=> the docs say "The calendar interval at which to reschedule the notification."

so: use NSCalendarUnit.CalendarUnitDay

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135