1

I'm using:

var alarm = NSUserNotification()
var currentTime = NSDate()
alarmTime = currentTime.dateByAddingTimeInterval(60)
alarm.deliveryDate = alarmTime
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(alarm)

To get a notification that will fire an hour from the current time, the problem is I want the app to automatically set up another alarm after the first one finishes. NSTimer doesn't seem like it will work because once the app goes to the background it kills the timer. What can I do to achieve this? Can I piggy back another method onto a NSUserNotification

Edit: it also needs to be dynamic, I can't just set the repeat variable of the notification because I need to be able to switch how far out the alarm will be each time it resets.

Dominic
  • 62,658
  • 20
  • 139
  • 163
Spencer Wood
  • 610
  • 6
  • 15

1 Answers1

1

You just have to set your deliveryRepeatInterval and deliveryTimeZone, as follow:

// set your deliveryTimeZone to localTimeZone
alarm.deliveryTimeZone = NSTimeZone.localTimeZone()

// The date components that specify how a notification is to be repeated.
// This value may be nil if the notification should not repeat.
// alarm.deliveryRepeatInterval = nil

// The date component values are relative to the date the notification was delivered.
// If the calendar value of the deliveryRepeatInterval is nil
// the current calendar will be used to calculate the repeat interval.
// alarm.deliveryRepeatInterval?.calendar = NSCalendar.currentCalendar()

// to repeat every day, set .deliveryRepeatInterval.day to 1.
alarm.deliveryRepeatInterval?.day = 1
// to repeat every hour, set .deliveryRepeatInterval.hour to 1.
alarm.deliveryRepeatInterval?.hour = 1

 alarm.deliveryDate = NSDate().dateByAddingTimeInterval(60)

NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(alarm)
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • How can I change the time interval each time though? For example, I want the first notification to go off at 50 minutes (a work period), and then after that one, I want it to schedule a second notification that goes off at 15 minutes (a break period) and then I want it to start over again. – Spencer Wood Jan 13 '15 at 06:20
  • Why don't you schedule both at the same time, the work period goes off at 50' and the break period goes off at 65'? – Leo Dabus Feb 10 '15 at 04:54