我正在尝试创建一个每天23:59:59开火的通知,这是我迄今为止所做的 -
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
UIUserNotificationType.Badge, categories: nil))
let calendar = NSCalendar(identifier: NSGregorianCalendar)
let cal = NSCalendar.currentCalendar()
let datefor = NSDate()
let comp = cal.components(.MonthCalendarUnit | .DayCalendarUnit | .YearCalendarUnit, fromDate: datefor)
let components = NSDateComponents()
components.year = comp.year
components.month = comp.month
components.day = comp.day
components.hour = 23
components.minute = 59
components.second = 59
let date = calendar?.dateFromComponents(components)
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Checking"
localNotification.alertBody = "Hello"
localNotification.fireDate = date
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = NSCalendarUnit.DayCalendarUnit
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
该代码无法正常工作,我只收到一封通知。 代码中的问题是什么?
答案 0 :(得分:1)
/*notification process */
//setting up date
//year
var year = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitYear, fromDate: NSDate(timeIntervalSinceNow: 0) ) )
//month
var month = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitMonth, fromDate: NSDate(timeIntervalSinceNow: 0) ) )
//day
var day = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitDay, fromDate: NSDate(timeIntervalSinceNow: 0) ) )
println( "Date: \(month)|\(day)|\(year)" )//testing
//hour
var hour = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitHour, fromDate: NSDate(timeIntervalSinceNow: 0) ) )
//minute
var minute = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitMinute, fromDate: NSDate(timeIntervalSinceNow: 0) ) )
//second
var second = Int( NSCalendar.currentCalendar().component(NSCalendarUnit.CalendarUnitSecond, fromDate: NSDate(timeIntervalSinceNow: 0) ) )
println( "Time: \(hour):\(minute):\(second)" )//testing
//time is passed the desired schedule time
if(hour > 8){
day++
//process the month of february
if(month == 2){
//determining if it is leap year
var leapyear = self.isLeapYear(year)
//leap year & feb days have exhausted
if(leapyear){
if(day > 29){
day = 1
month++
}
}
//not leap year & feb days have exhausted
else if(day > 28 ){
day = 1
month++
}
}
else if(day > 30){
//months with 30 max 30 days ( April, June, Semptember, November)
if( (month == 4) || (month == 6) || (month == 9) || (month == 11){
day = 1
month++
}
else{
//months with 31 days
if(day > 31){
day = 1
month++
//reached the end of the year
if(month > 12){
month = 1
year++
}
}
}
}
}
println("updated date: \(month)|\(day)|\(year)")
println("updated time: \(hour)|\(minute)|\(second)")
//customize a specific start date and time
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = year
dateComp.month = month
dateComp.day = day
dateComp.hour = Int(8)
dateComp.minute = Int(0)
dateComp.second = Int(0)
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calendar = NSCalendar.currentCalendar()
var date:NSDate = calendar.dateFromComponents(dateComp)!
println(calendar.description.debugDescription)
println(dateComp.debugDescription)
println(date.debugDescription)
//notification settings
var localNotification: UILocalNotification = UILocalNotification()
localNotification.alertAction = "Daily notification"
localNotification.alertBody = "Message goes here"
localNotification.timeZone = NSTimeZone.systemTimeZone()
localNotification.fireDate = date
//repeat settings
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
localNotification.repeatCalendar = NSCalendar.currentCalendar()
localNotification.category = "INVITE_CATEGORY"; //needed for multiple request in app delegate
println("- - - - - - - - - - - - - - - - - - - -")
println("schedule set for: \(localNotification.debugDescription)")
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)