UILocalNotification重复间隔不起作用

时间:2015-08-27 16:39:33

标签: ios swift uilocalnotification

我试图让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)
    }

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

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

2 个答案:

答案 0 :(得分:1)

来自Apple的文档repeatInterval

  

如果您指定日历单位,例如每周(NSWeekCalendarUnit)或   每年(NSYearCalendarUnit),系统重新安排通知   以指定的间隔交货。 请注意间隔较小   不支持一分钟。默认值为0,表示   系统触发通知一次然后丢弃它。

答案 1 :(得分:0)

应用程序应在后台触发通知。如果您想在应用程序处于前台时触发通知,请执行

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification){

}