1

I have set up a local notification to fire. I am wondering how to get it to repeat every week at the same time e.g., 9 AM on Monday.

Here's my code so far:

 @IBAction func scheduleLocal(sender: UIButton) {
guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return
}
if settings.types == .None {
    let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .Alert)
    ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(ac, animated: true, completion: nil)
    return
}
let notification = UILocalNotification()
notification.fireDate = NSDate()
notification.alertBody = "Come Exercise"
notification.alertAction = "Exercise Time"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["customField1": "w00t"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

and in viewDidLoad:

 let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
jscs
  • 63,694
  • 13
  • 151
  • 195
Ryan.H
  • 361
  • 4
  • 19

2 Answers2

2

There is a repeatInterval you can set on the local notification that takes an NSCalendarUnit as its type. You can read more about the different available calendar units here https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit

You would likely be looking for NSCalendarUnitWeekOfYear

So you could just add the following to your code for your notification

notification.repeatInterval = NSCalendarUnit.WeekOfYear
zfetters
  • 447
  • 2
  • 11
  • Hi, Thanks for your reply. So NSCalenderUnitWeekOfYear would repeat the notification infinitely at the same time on the same day each week? – Ryan.H Mar 03 '16 at 20:06
  • Thank you. How would i go about setting the first fire date to for example 9:00am monday? – Ryan.H Mar 03 '16 at 20:29
  • This answer can help you with that: http://stackoverflow.com/questions/19276361/ios-set-local-notification-on-a-specific-day – zfetters Mar 03 '16 at 21:03
  • Is there a swift translation / explanation of this? – Ryan.H Mar 03 '16 at 21:11
  • 1
    This question shows doing a daily notification which could be turned into a weekly notification using the WeekOfYear calendar unit for the repeat interval: http://stackoverflow.com/questions/30619998/repeating-local-notification-daily-at-a-set-time-with-swift – zfetters Mar 03 '16 at 21:20
  • Thats perfect. Thank you so much for your time and effort! – Ryan.H Mar 03 '16 at 21:22
0

Your code is right.

But: "Each app on a device is limited to 64 scheduled local notifications." https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html

You can schedule the local notifications and recreate them each time the user open the app. Or use remote notifications instead.

Juan de Dios
  • 2,683
  • 2
  • 20
  • 24
  • Hi Thanks for your reply. I am trying to remind the users to take a break every hour between 9-5 on monday - friday. Would you recommend local notifications or remote notifcations for this? – Ryan.H Mar 03 '16 at 20:08
  • I recommend local notifications. That give you fast schedule and weekly notifications for a year and quarter still if your user don't open the app. – Juan de Dios Mar 03 '16 at 20:15
  • How would i go about making a local notification repeat at the same time each week? – Ryan.H Mar 03 '16 at 20:21
  • Just use a function like this and change the NSDate: - (void)scheduleAlert:(NSString*)alertBody fireDate:(NSDate*)fireDate{ localNotification.fireDate = fireDate; – Juan de Dios Mar 03 '16 at 20:42
  • Does this also work for Swift as it looks like Objective C? – Ryan.H Mar 03 '16 at 20:45
  • That is perfect!. Thank you for your help. – Ryan.H Mar 03 '16 at 20:52
  • I think you can use event kit for this. – Bhavuk Jain Mar 03 '16 at 21:25