UNTimeIntervalNotificationTrigger nextTriggerDate() 是不是给出了错误的日期?
Posted
技术标签:
【中文标题】UNTimeIntervalNotificationTrigger nextTriggerDate() 是不是给出了错误的日期?【英文标题】:Does UNTimeIntervalNotificationTrigger nextTriggerDate() give the wrong date?UNTimeIntervalNotificationTrigger nextTriggerDate() 是否给出了错误的日期? 【发布时间】:2016-11-03 21:48:52 【问题描述】:我正在更新我的本地通知以与 ios 10 一起使用,我遇到了一个问题,我认为 nextTrigger 函数返回不是“满足触发条件的下一个日期”。而是返回当前日期时间加上您最初将 UNTimeInvervalNotificationTrigger 设置为的任何内容。
因此,如果您将触发器设置为在 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,但是……你知道……这需要一段时间。所以我想我会看看这里是否有人有任何见解。我怀疑这是一个错误,如果我得到更多信息,我会更新它。
这是我用来说明问题的代码:
import UIKit
import UserNotifications
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var setButton: 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)")
)
【问题讨论】:
我遇到了和你一样的问题。在我看来,如果我在 12 点设置了 60 分钟的时间间隔,那么触发器的下一个日期是 13 点。但是,当我从 UNNotificationCenter 获得 request.trigger 时,我会打印 trigger.nextTriggerDate。日期不是 13 点。更重要的是,如果我在不同的时间打印它,值会发生变化。无论如何,感谢您的回答,它有效。 【参考方案1】:收到 Apple DTS 的回复,并被告知我所描述的功能是其设计工作方式。尽管我曾认为文档对它的解释不同,但他们告诉我“考虑到实施,文档是乐观的”。无论如何,我通过跟踪代码中的触发日期来解决它。我希望这个答案可以帮助有同样问题的其他人。
【讨论】:
当时它并没有像我认为的那样工作。 他们在撒谎说它是设计的吗?下一个触发日期不正确,所以他们在愚弄谁?? 我和你一样需要知道什么时候触发触发器,但我不知道该怎么做:(你找到解决方案了吗? 这个时候还是不行。它返回的日期似乎基于您调用该函数的时间。 是的,您必须自己计算。您可以将某些内容传递给 userInfo 字典来计算它。这是迄今为止我见过的最愚蠢的 api。返回当前日期和时间间隔有什么帮助?以上是关于UNTimeIntervalNotificationTrigger nextTriggerDate() 是不是给出了错误的日期?的主要内容,如果未能解决你的问题,请参考以下文章