2016-11-03 127 views
3

我正在更新我的本地通知以與iOS 10一起工作,並且遇到了一個問題,我認爲nextTrigger函數返回NOT「觸發條件滿足的下一個日期」,而是返回當前日期時間是PLUS你最初設置的UNTimeInvervalNotificationTrigger。UNTimeIntervalNotificationTrigger nextTriggerDate()是否給出錯誤的日期?

因此,如果您設置觸發器在60秒內關閉,從文檔中我期望當我調用nextTriggerDate()時,我會讓它返回任何日期時間,當我設置觸發器+ 60秒。所以如果我在12:00:00設置它,我期望nextTriggerDate()將是12:01:00。然而我所經歷的是,無論當前日期是+ 60秒,它都會返回。

我寫了一個示例,它計劃一個UNTimeIntervalNotificationTrigger,然後每秒鐘打印出nextTriggerDate()。當我運行這個時,我每秒都會得到一個新的nextTriggerDate。像這樣:

  //Optional(2016-11-03 21:26:31 +0000) 
      //Optional(2016-11-03 21:26:32 +0000) 
      //Optional(2016-11-03 21:26:33 +0000) 
      //Optional(2016-11-03 21:26:34 +0000) 
      //Optional(2016-11-03 21:26:35 +0000) 
      //Optional(2016-11-03 21:26:36 +0000) 

我已經用蘋果打開了一個TSI,但是......你知道......需要一段時間。所以我想我會看看這裏有沒有人有任何見解。我懷疑這是一個錯誤,如果我得到更多的信息,我會更新這個。

這是我用來說明問題的代碼:

進口的UIKit 進口UserNotifications

類的ViewController:UIViewController中,UIPickerViewDelegate,UIPickerViewDataSource { @IBOutlet弱變種SET按鈕:UIButton的!

@IBOutlet weak var timePicker: UIPickerView! 

weak var timer: Timer? 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


@IBAction func setButtonAction(_ sender: UIButton) { 
    var mySecond = pickerSelection 

    // build notification 
    let content = UNMutableNotificationContent() 
    content.title = "Title of notification" 
    content.body = "This is the body of the notification" 
    content.sound = UNNotificationSound.default() 
    content.categoryIdentifier = "myCategory" 

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: Double(mySecond), repeats: false) 

    let request = UNNotificationRequest(identifier: "test notification", content: content, trigger: trigger) 

    UNUserNotificationCenter.current().add(request) {(error) in 
     if let error = error { 
      print("Uh oh! We had an error: \(error)") 
     } 
    } 

    self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.tick), userInfo: nil, repeats: true) 


} 


let pickerData = [":00",":01",":02",":03",":04",":05",":06",":07",":08",":09",":10",":11",":12",":13",":14",":15",":16",":17",":18",":19",":20",":21",":22",":23",":24",":25",":26",":27",":28",":29",":30",":31",":32",":33",":34",":35",":36",":37",":38",":39",":40",":41",":42",":43",":44",":45",":46",":47",":48",":49",":50",":51",":52",":53",":54",":55",":56",":57",":58",":59"] 


var pickerSelection = 0 


override func viewDidLoad() { 
    super.viewDidLoad() 
    self.timePicker.dataSource = self 
    self.timePicker.delegate = self 
    self.timePicker.selectRow(pickerSelection, inComponent: 0, animated: false) 

} 

// The number of columns of data 
func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 1 
} 

// The number of rows of data 
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return pickerData.count 
} 

// The data to return for the row and component (column) that's being passed in 
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return pickerData[row] 
} 

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 
    let pickerLabel = UILabel() 
    let titleData = pickerData[row] 
    let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Futura", size: 44.0)!]) 
    pickerLabel.attributedText = myTitle 
    pickerLabel.textAlignment = .center 

    return pickerLabel 
} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    pickerSelection = row 
} 

func tick() { 
    let center = UNUserNotificationCenter.current() 
    center.getPendingNotificationRequests(completionHandler: { (scheduledLocalNotifications) in 
     for localNotice in scheduledLocalNotifications { 
      var localTrigger = localNotice.trigger as! UNTimeIntervalNotificationTrigger 

      var localTime = localTrigger.nextTriggerDate() 
      // ** This output shows something like this: 

      //Optional(2016-11-03 21:26:31 +0000) 
      //Optional(2016-11-03 21:26:32 +0000) 
      //Optional(2016-11-03 21:26:33 +0000) 
      //Optional(2016-11-03 21:26:34 +0000) 
      //Optional(2016-11-03 21:26:35 +0000) 
      //Optional(2016-11-03 21:26:36 +0000) 
      print("\(localTime)") 

     } 
    }) 
} 

}

+0

我遇到了和你一樣的問題。 在我看來,如果我在12點設置60分鐘的時間間隔,觸發器的下一個日期是13點。但是,當我從UNNotificationCenter獲取request.trigger時,我打印了trigger.nextTriggerDate。日期不是13點。更何況,如果我在不同的時間打印它,價值會發生變化。 無論如何,謝謝你的anwser,它的工作原理。 – user7097837

回答

4

從蘋果DTS收到回覆,被告知,我想描述的功能是它是如何設計的工作。儘管我曾經認爲文檔中有不同的解釋,但他們告訴我說:「鑑於實施情況,文檔是樂觀的。」無論如何,我通過跟蹤代碼中的火災日期來解決這個問題。我希望這個答案可以幫助其他有我同樣問題的人。

相關問題