我正在处理应用程序的本地通知,该应用程序应该仅在特定日期触发并在该日重复,直到用户决定将其删除。每周三上午11:00发出一个示例通知。
当用户在时间选择器上按下完成时,将触发函数 - scheduleLocalNotification。
var sundayNotification : UILocalNotification!
var mondayNotification : UILocalNotification!
var tuesdayNotification : UILocalNotification!
var wednesdayNotification : UILocalNotification!
var thursdayNotification : UILocalNotification!
var fridayNotification : UILocalNotification!
var saturdayNotification : UILocalNotification!
func scheduleLocalNotification() {
//Check the Dayname and match fireDate
if dayName == "Sunday" {
sundayNotification = UILocalNotification()
//timeSelected comes from the timePicker i.e. timeSelected = timePicker.date
sundayNotification.fireDate = fixNotificationDate(timeSelected)
sundayNotification.alertTitle = "Sunday's Workout"
sundayNotification.alertBody = "This is the message from Sunday's workout"
sundayNotification.alertAction = "Snooze"
sundayNotification.category = "workoutReminderID"
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.saveContext()
UIApplication.sharedApplication().scheduleLocalNotification(sundayNotification)
} else if dayName == "Monday" {
mondayNotification = UILocalNotification()
mondayNotification.fireDate = fixNotificationDate(timeSelected)
mondayNotification.alertTitle = "Monday's Workout"
mondayNotification.alertBody = "This is the message from Monday's workout"
mondayNotification.alertAction = "Snooze"
mondayNotification.category = "workoutReminderID"
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.saveContext()
UIApplication.sharedApplication().scheduleLocalNotification(mondayNotification)
} else if ..... }
}
func fixNotificationDate(dateToFix: NSDate) -> NSDate {
let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: dateToFix)
dateComponets.second = 0
if dayName == "Sunday" {
dateComponets.weekday = 1 //n = 7
} else if dayName == "Monday" {
dateComponets.weekday = 2
} else if dayName == "Tuesday" {
dateComponets.weekday = 3
} else if dayName == "Wednesday" {
dateComponets.weekday = 4
} else if dayName == "Thursday" {
dateComponets.weekday = 5
} else if dayName == "Friday" {
dateComponets.weekday = 6
} else if dayName == "Saturday" {
dateComponets.weekday = 7
}
let fixedDate: NSDate! = NSCalendar.currentCalendar().dateFromComponents(dateComponets)
return fixedDate
}
在检查了通知时指定正确的UILocalNotification之后,这仍然奇怪地不起作用,例如在不同的日子(周日上午10:10和周一上午10:15)设置不同的时间通知不会改变任何事情。在我没有选择的那天,通知仍然会触发,即在这两个时间而不是那些日子的今天。
我可能做错了什么或丢失的任何线索? 提前致谢。 :)
答案 0 :(得分:1)
经过一番努力,我想出了这个。我们首先将日期更改为用户选择的第一个开火日期。
func setNotificationDay(today: NSDate, selectedDay: Int) -> NSDate {
let daysToAdd: Int!
let calendar = NSCalendar.currentCalendar()
let weekday = calendar.component(.Weekday, fromDate: today)
if weekday > selectedDay {
daysToAdd = (7 - weekday) + selectedDay
} else {
daysToAdd = (selectedDay - weekday)
}
let newDate = calendar.dateByAddingUnit(.Weekday, value: daysToAdd, toDate: today, options: [])
return newDate! //if you don't have a date it'll crash
}
您可以使用NSDate()
而不是将其作为参数。我会把它留给你。
现在,我们需要设置通知。我没有对您的fixNotificationDate()
做任何事情,但您可以轻松添加它。这里的关键点是设置notification.repeatInterval = .Weekday
或不会重复。我还添加了一个字典来设置日期名称。这比重复代码要好得多。
它会创建对NSCalendar
的第二次调用,但您可能会找到一种方法来避免在代码中执行此操作。
func scheduleLocalNotification(date: NSDate) {
let dayDic = [1:"Sunday",
2:"Monday",
3:"Tuesday",
4:"Wednesday",
5:"Thursday",
6:"Friday",
7:"Saturday"]
let calendar = NSCalendar.currentCalendar()
let weekday = calendar.component(.Weekday, fromDate: date)
let notification = UILocalNotification()
notification.fireDate = date
notification.repeatInterval = .Weekday
notification.alertTitle = String(format: "%@'s Workout", dayDic[weekday]!)
notification.alertBody = String(format: "This is the message from %@'s workout", dayDic[weekday]!)
notification.alertAction = "Snooze"
notification.category = "workoutReminderID"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
那应该可以解决你的问题。我没有测试它,因为它需要至少一个星期! ;)
希望有所帮助!