gpt4 book ai didi

ios - 如何在 Swift 中使用本地通知?

转载 作者:行者123 更新时间:2023-11-29 02:17:57 25 4
gpt4 key购买 nike

我正在尝试创建一个每天 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)

该代码无效,我只收到一条通知,仅此而已。代码有什么问题?

最佳答案

您尝试过在 Iphone 上测试过吗?以下是我正在从事的项目的代码。 (请注意仅在应用程序第一次调用此段代码,如果您继续运行该应用程序,将添加更多计划)

  /*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)

关于ios - 如何在 Swift 中使用本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28529459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com