gpt4 book ai didi

ios - UILocalNotification 重复间隔不起作用

转载 作者:行者123 更新时间:2023-11-28 07:00:05 25 4
gpt4 key购买 nike

我试图让 UILocalNotification 每秒重复一次,它确实在重复,但由于某种原因不是每秒重复一次,它似乎每分钟都重复一次,我对 UILocalNotifications 还是陌生的,所以我可能正在做一些事情错误的。这是通知的图片: http://imgur.com/Hzt38py

这是我用来创建 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)
}

这是 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)
}

单击添加任务按钮时会创建通知,但不会每秒重复一次。我究竟做错了什么?

**也有人知道如何在手机处于“请勿打扰”或静音模式时播放通知声音

最佳答案

来自 Apple 的文档 repeatInterval

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.

关于ios - UILocalNotification 重复间隔不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32255063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com