![](/img/trans.png)
[英]Repeating local notifications for specific days of week (Swift 3 IOS 10)
[英]Repeating Local Notifications every Week with Swift
我有一个使用本地通知的应用程序,总共有64条通知,这是本地通知的限制。 我需要每周重复每条通知。 我知道要设置您使用的重复间隔:
alarm.repeatInterval = NSCalendarUnit
我尝试使用.WeekOfYear和.WeekOfMonth。 他们每年还是每月重复一次通知? 而且我不知道每周的日历单位。 我可以使用哪一个每周重复一次?
编辑:
这是我用来设置通知的代码。
let notifyAlarm = UILocalNotification()
let component = NSDateComponents()
component.hour = NSUserDefaults.standardUserDefaults().integerForKey("Hour1")
component.minute = NSUserDefaults.standardUserDefaults().integerForKey("Minute1")
component.weekday = 1
notifyAlarm.fireDate = calendar.dateFromComponents(component)
notifyAlarm.timeZone = NSTimeZone.defaultTimeZone()
notifyAlarm.alertBody = NSUserDefaults.standardUserDefaults().stringForKey("Message1")
notifyAlarm.repeatInterval = NSCalendarUnit.WeekdayOrdinal
notifyAlarm.soundName = NSUserDefaults.standardUserDefaults().stringForKey("Sound1")
app.scheduleLocalNotification(notifyAlarm)
我要立即设置64条这样的通知。 但是日期不同。
如果您希望一周后第一次触发通知,则需要更改触发日期。 我为此使用TimeIntervalSinceNow,以秒为单位,因此1周大约是604000秒。 您可以使用_分隔数字以提高可读性。
alarm.fireDate = NSDate(timeIntervalSinceNow: 604_000)
也许有点笨拙,但我认为对于这些类型的通知而言,这是最简单的。 我这样做是为了使其更容易。
struct NotificationFireDate {
static let nextDay: NSTimeInterval = 85_000
static let nextWeek: NSTimeInterval = 604_000
}
而不是像这样使用它
alarm.fireDate = NSDate(timeIntervalSinceNow: NotificationFireDate.nextWeek)
重复间隔应为weekOfYear
alarm.repeatInterval = NSCalendarUnit.WeekOfYear
第一个重复通知应在第一个(fireDate)通知发出后的1周内触发。
有关完整列表,请查看此内容(感谢madmik3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.