使用 NSTimer 的重复本地通知不起作用
Recurring local notifications using NSTimer don't work
用户可以在滑块(3、5、8、10 等)的几分钟间隔之间 select。
因为无法在自定义时间设置重复本地通知(仅在一秒、一分钟、一小时、一天...)。
我创建了一个计时器,它会在选定的时间创建一个新的本地通知。奇怪的是它可以在物理 iPhone 6 和 iPhone 5 模拟器上运行。但不是物理上的iPhone5,都是运行iOS9.3.2。
下面是启动计时器的代码,数字是从滑块 (3, 5, 8...) 中获取的整数值:
@IBAction func saveSettings(sender: AnyObject) {
var timer = NSTimer.scheduledTimerWithTimeInterval(Double(number*60), target: self, selector: Selector("sendNotification"), userInfo: nil, repeats: true)
print("notification interval set to \(number)")
}
func sendNotification() {
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: Double(number))
localNotification.alertBody = "This is a test body."
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
似乎是 iPhone 5 和 6 之间的某种硬件差异影响了此解决方案。它是什么,我该怎么做才能让它在两者上都起作用?
您需要创建 UILocalNotification
并为所有时间间隔安排它。并设置 fireDate
notification.fireDate(YOUR_TIME_INTERVAL)
UIApplication.sharedApplication.scheduleLocalNotification(notification)
类似于您使用 NSTimer 的情况
func sendNotificationNew() -> Void {
let dateTime = NSDate()
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
或更改您的代码
@IBAction func saveSettings(sender: AnyObject) {
let dateTime = NSDate().dateWithTimeIntervalSinceNow(Double(number*60))
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
我现在已经解决了这个问题。
问题是如果应用暂停,NSTimer 不会执行。它在模拟器中工作,因为应用程序似乎永远不会在那里暂停。它可能在物理上工作 iPhone 6 因为它有足够的内存不会快速挂起应用程序。
所以,现在如果用户选择 5 分钟,那么我总共创建 12 个通知,每个通知的触发日期错开 5 分钟。每个通知都有一个重复间隔设置为 .NSHourCalendarUnit。
这样就可以创建重复的本地通知。
用户可以在滑块(3、5、8、10 等)的几分钟间隔之间 select。
因为无法在自定义时间设置重复本地通知(仅在一秒、一分钟、一小时、一天...)。
我创建了一个计时器,它会在选定的时间创建一个新的本地通知。奇怪的是它可以在物理 iPhone 6 和 iPhone 5 模拟器上运行。但不是物理上的iPhone5,都是运行iOS9.3.2。
下面是启动计时器的代码,数字是从滑块 (3, 5, 8...) 中获取的整数值:
@IBAction func saveSettings(sender: AnyObject) {
var timer = NSTimer.scheduledTimerWithTimeInterval(Double(number*60), target: self, selector: Selector("sendNotification"), userInfo: nil, repeats: true)
print("notification interval set to \(number)")
}
func sendNotification() {
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: Double(number))
localNotification.alertBody = "This is a test body."
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
似乎是 iPhone 5 和 6 之间的某种硬件差异影响了此解决方案。它是什么,我该怎么做才能让它在两者上都起作用?
您需要创建 UILocalNotification
并为所有时间间隔安排它。并设置 fireDate
notification.fireDate(YOUR_TIME_INTERVAL)
UIApplication.sharedApplication.scheduleLocalNotification(notification)
类似于您使用 NSTimer 的情况
func sendNotificationNew() -> Void {
let dateTime = NSDate()
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
或更改您的代码
@IBAction func saveSettings(sender: AnyObject) {
let dateTime = NSDate().dateWithTimeIntervalSinceNow(Double(number*60))
var notification = UILocalNotification()
notification.timeZone = NSTimeZone.defaultTimeZone()
notification.fireDate(dateTime)
notification.alertBody("Test")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
我现在已经解决了这个问题。
问题是如果应用暂停,NSTimer 不会执行。它在模拟器中工作,因为应用程序似乎永远不会在那里暂停。它可能在物理上工作 iPhone 6 因为它有足够的内存不会快速挂起应用程序。
所以,现在如果用户选择 5 分钟,那么我总共创建 12 个通知,每个通知的触发日期错开 5 分钟。每个通知都有一个重复间隔设置为 .NSHourCalendarUnit。
这样就可以创建重复的本地通知。