UILocalNotification 重复间隔不起作用 [英] UILocalNotification repeat interval not working
问题描述
我试图让 UILocalNotification 每秒重复一次,它确实在重复,但由于某种原因不是每一秒,它似乎每分钟重复一次,我还是 UILocalNotifications 的新手,所以我可能正在做一些事情错误的.这是通知的图片:http://imgur.com/Hzt38py
I'm trying to have the UILocalNotification repeat every second, and it is indeed repeating, but not every second for some reason, it seems to repeat every minute instead, I'm still new to UILocalNotifications so I might be doing something wrong. Here's a pic of the notifs: http://imgur.com/Hzt38py
这是我用来创建 UILocalNotification 的函数
Here's the function I'm using to create the UILocalNotification
func notificationCreater (date:NSDate, uuid:String) {
let notification = UILocalNotification ()
notification.alertBody = "Test Run 9"
notification.fireDate = date
notification.repeatInterval = NSCalendarUnit.CalendarUnitSecond
//notification.userInfo = ["UUID": uuid]
// notification.soundName = "alarmSound.m4a"
// notification.alertAction = "he"
// notification.soundName = "alarmSound.m4a"
// notification.alertTitle = "Test Title"
println("schld")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
这是视图控制器的其余部分
Here's the rest of the View Controller
class AddTaskViewController: UIViewController,AVAudioPlayerDelegate{
@IBOutlet weak var taskTextField: UITextField!
@IBOutlet weak var dueDatePicker: UIDatePicker!
var delegate = AddTaskViewControllerDelegate?()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Background")!)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func cancelButtonTapped(sender: UIButton) {
self.dismissViewControllerAnimated(true, completion: nil)
delegate?.addTaskCanceled!("task canceled")
UIApplication.sharedApplication().cancelAllLocalNotifications()
}
@IBAction func addTaskButtonTapped(sender: UIButton) {
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("TaskModel", inManagedObjectContext: managedObjectContext!)
let task = TaskModel(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
task.task = taskTextField.text
task.date = dueDatePicker.date
task.uuid = NSUUID().UUIDString
//if NSUserDefaults.standardUserDefaults().boolForKey(kShouldCompleteNewTodoKey) == true {task.completed = true}
task.completed = false
appDelegate.saveContext()
let calendar = NSCalendar.currentCalendar()
let comp = calendar.components(NSCalendarUnit.CalendarUnitSecond, fromDate: task.date)
let seconds = Double(comp.second)
// let notification = UILocalNotification()
// notification.alertBody = "testBody"
// notification.fireDate = dueDatePicker.date
// notification.alertTitle = "testTitle"
println("seconds:\(seconds)")
var request = NSFetchRequest(entityName: "TaskModel")
var error:NSError? = nil
var results:NSArray = managedObjectContext!.executeFetchRequest(request, error: &error)!
notificationCreater(dueDatePicker.date, uuid: task.uuid)
self.dismissViewControllerAnimated(true, completion: nil)
}
点击添加任务按钮时会创建通知,但不会每秒重复一次.我究竟做错了什么?
The notification is created when the add task button is clicked, but it doesn't repeat every second. What am I doing wrong?
**即使手机处于请勿打扰"或静音模式,也有人知道如何播放通知声音
**Also anyone know how to play the notification sound even when the phone is in "Do Not Disturb" or silent mode
推荐答案
来自 Apple 的文档 repeatInterval
From Apple's documentation repeatInterval
如果您分配日历单位,例如每周 (NSWeekCalendarUnit) 或每年(NSYearCalendarUnit),系统重新安排通知以指定的时间间隔交货.注意间隔少不支持超过一分钟. 默认值为 0,表示系统会触发一次通知,然后将其丢弃.
If you assign a calendar unit such as weekly (NSWeekCalendarUnit) or yearly (NSYearCalendarUnit), the system reschedules the notification for delivery at the specified interval. Note that intervals of less than one minute are not supported. The default value is 0, which means that the system fires the notification once and then discards it.
这篇关于UILocalNotification 重复间隔不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!