I want an example code for daily quote that get locAl notification for everyday of year with given different message/quote
1 Answers
0
votes
Better would be add different UILocalNotificaiton
s for each day in a loop, create a reference data lets say today and then loop 365 times and add one day timeInterval in each iteration register them to app, something like this
var messages:[String] = [/*Add messages here*/]
var date = NSDate()
let dayTimerInterval:NSTimeInterval = (60 * 60 * 26)
date = date.dateByAddingTimeInterval(dayTimerInterval)
for i in 0..<messages.count
{
let localNotif = UILocalNotification()
localNotif.alertBody = messages[i]
localNotif.fireDate = date
date = date.dateByAddingTimeInterval(dayTimerInterval)
UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
}
There is another way that you can do that with one notification and that when a local notification fire and user open app then you change its message by getting its reference... but for that you would need app running. You can do this in different ways
1.Your app receive a localNotification
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
{
notification.alertBody = "new message"
}
You app launched via local notification
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if let options = launchOptions {
// Do your checking on options here
let notif:UILocalNotification = options[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
notif.alertBody = "new alert boxy"
}
return true
}
But in both cases there are chances you missed them as both case not guaranteed every time, so add different notifications for every day.