1

I want to schedule a local notification with a text like "Day 1" on first day and "Day 2" on second day like that:

let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)


    let notification = UILocalNotification()
    notification.fireDate = // ex: 5 am
    notification.alertBody = "Day 1" // on second day it want to be "Day 2" , on third day it want to be "Day 3" like that..
    notification.alertAction = "be awesome!"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["CustomField1": "w00t"]
    UIApplication.sharedApplication().scheduleLocalNotification(notification)

Is there any possibility to do this?

User will receive a notification of first Day as "Day 1"

Second Day as "Day 2"

Third Day as "Day 3"

Is it possible to set a notification like that?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Deepak ravi
  • 43
  • 10

1 Answers1

1

You can schedule notifications for whole week with something like this:

func scheduleAwesomeNotifications() {
    for day in 0...7 {
        let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)

        let intervalForDay = Double(60 * 60 * 24 * day)

        let notification = UILocalNotification()
        notification.fireDate = NSDate().dateByAddingTimeInterval(intervalForDay)
        notification.alertBody = "Day " + day.description
        notification.alertAction = "be awesome!"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.userInfo = ["CustomField1": "w00t"]
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }
}
Ivan Nesterenko
  • 939
  • 1
  • 12
  • 23
  • Thank You, Working fine... But for long days like a year or more.... How to do... if i use a for loop for an year Apple will allow this for publishing... for loop for one year is correct approach... – Deepak ravi Apr 23 '16 at 14:31
  • Use NSCalendar approach for that. With it you can add any value to a date http://stackoverflow.com/a/36696052/4108415. – Ivan Nesterenko Apr 23 '16 at 16:05