[英]Making a Local notification that only repeats on a specific day
我正在为一个仅在特定日期触发并在该日重复直到用户决定将其删除的应用程序进行本地通知。 上有一个示例通知,该通知在每个星期三的11:00 am触发。
当用户在时间选择器上按完成时,将触发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)设置不同的时间,则通知没有任何改变。 该通知仍会在我未选择的日期(即今天和这两个时间)而不是那一天触发。
关于我可能做错或遗漏的任何线索? 提前致谢。 :)
经过一些工作,我想到了这个。 我们首先将日期更改为用户选择的第一个触发日期。
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)
}
那应该可以解决您的问题。 我没有测试它,因为它至少需要一个星期! ;)
希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.