我正在尝试安排UILocalNotifications。 我想要实现的功能是在特定的日子重复通知,例如(每个星期一)将由用户选择。 有没有办法安排UILocalNotification,因此通知将仅在当天触发。
let notification = UILocalNotification()
let dict:NSDictionary = ["ID" : "your ID goes here"]
notification.userInfo = dict as! [String : String]
notification.alertBody = "\(title)"
notification.alertAction = "Open"
notification.fireDate = dateToFire
notification.repeatInterval = .Day // Can be used to repeat the notification
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
答案 0 :(得分:1)
通过以下方式每周进行一次:
notification.repeatInterval = .Weekday // Or maybe .WeekOfYear
无论一周中的哪一天dateToFire
,通知都会在一周的那一天每周重复一次。
答案 1 :(得分:1)
我不确定这会对你有所帮助,但我之前已经解决了这个问题。我创建了7个按钮,可以在一周内选择7天。这是我的旧代码:
class RemindNotification {
var timerNotification = NSTimer()
func notification(story: Story) {
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let date = NSDate()
let dateComponents = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date)
dateComponents.hour = story.remindeAtHour
dateComponents.minute = story.remindeAtMinute
for index in 0..<story.remindeAtDaysOfWeek.count {
if story.remindeAtDaysOfWeek[index] == true {
if index != 6{
dateComponents.weekday = index + 2
fireNotification(calendar!, dateComponents: dateComponents)
} else {
dateComponents.weekday = 1
fireNotification(calendar!, dateComponents: dateComponents)
}
} else {
dateComponents.weekday = 0
}
}
}
func fireNotification(calendar: NSCalendar, dateComponents: NSDateComponents) {
let notification = UILocalNotification()
notification.alertAction = "Title"
notification.alertBody = "It's time to take a photo"
notification.repeatInterval = NSCalendarUnit.WeekOfYear
notification.fireDate = calendar.dateFromComponents(dateComponents)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
这是我的应用程序,我可以选择那一天,然后它可以在我每天设置的特定时间触发LocalNotification。
你可以参考它。希望这有帮助。