每天在特定时间快速重复本地通知
我是iOS开发新手,但已经创build了应用程序,我正在尝试创build一个设置时间的每日通知。 目前通知对于给定的date/时间执行一次。 我不确定如何使用repeatInterval方法每天安排。 每天重复通知的最佳方法是什么? 任何帮助将不胜感激(Y)。
var dateComp:NSDateComponents = NSDateComponents() dateComp.year = 2015; dateComp.month = 06; dateComp.day = 03; dateComp.hour = 12; dateComp.minute = 55; dateComp.timeZone = NSTimeZone.systemTimeZone() var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! var date:NSDate = calender.dateFromComponents(dateComp)! var notification:UILocalNotification = UILocalNotification() notification.category = "Daily Quote" notification.alertBody = quoteBook.randomQuote() notification.fireDate = date notification.repeatInterval = UIApplication.sharedApplication().scheduleLocalNotification(notification)
您必须提供NSCalendarUnit值,例如“HourCalendarUnit”或“DayCalendarUnit”以重复通知。
只需添加此代码即可每天重复本地通知:
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
所以,不得不修改@ vizllx上面的代码略微。 这是新的一行:
notification.repeatInterval = NSCalendarUnit.Day
以下是我使用的一个完整的工作示例:
let notification = UILocalNotification() /* Time and timezone settings */ notification.fireDate = NSDate(timeIntervalSinceNow: 8.0) notification.repeatInterval = NSCalendarUnit.Day notification.timeZone = NSCalendar.currentCalendar().timeZone notification.alertBody = "A new item is downloaded." /* Action settings */ notification.hasAction = true notification.alertAction = "View" /* Badge settings */ notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 /* Additional information, user info */ notification.userInfo = [ "Key 1" : "Value 1", "Key 2" : "Value 2" ] /* Schedule the notification */ UIApplication.sharedApplication().scheduleLocalNotification(notification) }
var repeatInterval: NSCalendarUnit
=>文档说“重新安排通知的日历时间间隔”。
所以:使用NSCalendarUnit.CalendarUnitDay