1

I have a daily notification that reminds the user to use the app. I would like the text to not be static though. I have say, 5 different strings, and I would like the string to be chosen randomly if possible.

My current code, that uses the same text everyday:

   let date: NSDate = NSDate()
    let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!

    let newDate: NSDate = cal.dateBySettingHour(22, minute: 00, second: 0, ofDate: date, options: NSCalendarOptions())!


    var localNotification: UILocalNotification = UILocalNotification()
    localNotification.alertAction = "MyApp"
    localNotification.alertBody = "Call to action text"
    localNotification.fireDate = newDate
    localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
Japes
  • 255
  • 2
  • 16
  • You need to schedule multiple notifications rather than a single, repeating notification – Paulw11 Sep 15 '15 at 02:08
  • how can that be done indefinitely? – Japes Sep 15 '15 at 02:09
  • There is a limit to how many notifications you can schedule at once (about 20? from memory) - When you app is launched you can schedule additional notifications to replace the ones that have already triggered. If the user doesn't launch your app for some time then eventually notifications will stop. You can do something like scheduling a notification for each of the next 5 days and then for 7 days and then for 14 days to try and prompt the user to return – Paulw11 Sep 15 '15 at 02:12
  • really? that doesnt seem to be reasonable. Considering doing this in android was like 3 lines of code... – Japes Sep 15 '15 at 02:13
  • That's how it is. You can only supply the text of a notification when it is scheduled, not when it is delivered. If you use push notifications from a server then you can do what you want – Paulw11 Sep 15 '15 at 02:14
  • ok, as a work around, what if I made 7 notifications, and set each one to fire weekly on a different day? I don't see anything in NSCalendar about weekly units though... – Japes Sep 15 '15 at 02:34
  • `NSCalendarUnit.WeekCalendarUnit` will give you a weekly repeat – Paulw11 Sep 15 '15 at 02:49
  • xcode says it is unresolved, i thought it was depreciated? no? – Japes Sep 15 '15 at 02:54
  • Sorry, you can use `NSCalendarUnit.CalendarUnitWeekOfYear` - http://stackoverflow.com/questions/24942640/what-i-have-to-use-to-repeat-local-notifications-weekly-in-ios-8 – Paulw11 Sep 15 '15 at 02:59
  • great, this seems like a solution. If you care to re-write as an answer, I will mark it as correct. Thanks. – Japes Sep 15 '15 at 03:00

1 Answers1

0

You could use a switch statement based on the day of the week. Here's an excellent NSHipster tutorial on NSDate components. Here's some sample code re: how to pull a day of the week out of an NSDate:

func getDayOfWeek(today:String)->Int {

    let formatter  = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    let todayDate = formatter.dateFromString(today)!
    let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let myComponents = myCalendar.components(.CalendarUnitWeekday, fromDate: todayDate)
    let weekDay = myComponents.weekday
    return weekDay
}

let weekday = getDayOfWeek("2014-08-27")
println(weekday) // 4 = Wednesday

Here's Apple's documentation for switch statements.

Basically, you'll want to create a method that takes an NSDate as an input and spits out a day of the week. Once you've got the day of the week, you you can create your custom message in the switch statement.

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • 1
    I was thinking along these lines, but I don't know how to schedule it to recur indefinitely on said say of the week. NSCalendarUnit doesnt seem to have weekly units. – Japes Sep 15 '15 at 02:43