【发布时间】:2016-03-10 09:20:25
【问题描述】:
我正在为应仅在特定日期触发并在该天重复直到用户决定将其删除的应用程序处理本地通知。一个示例通知是在每周三上午 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)为通知设置不同的时间不会改变任何内容。通知仍然在我没有选择的那一天触发,即今天在这两个时间而不是那些日子。
关于我可能做错或遗漏的任何线索? 提前致谢。 :)
【问题讨论】:
-
时间选择器是 NSDatePicker 吗?
-
它是在其他日期触发还是仅在今天触发?
-
@D.Greg 仅限今天!我尝试将其设置为其他日期,但该测试没有成功。
标签: ios swift notifications