【问题标题】:UNCalendarNotificationTrigger Hourly Trigger returns wrong trigger dateUNCalendarNotificationTrigger Hourly Trigger 返回错误的触发日期
【发布时间】:2017-02-13 17:21:10
【问题描述】:

我正在使用以下方法来获取每小时触发的本地通知。

let reminderTrigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.minute], from: dateStart), repeats: true)
let notificationRequest = UNNotificationRequest(identifier: "reminder", content: reminderContent, trigger: reminderTrigger)

这是基于 Apple 在此处的文档 - https://developer.apple.com/reference/usernotifications/uncalendarnotificationtrigger

但是,当我打印下一个触发日期时,就完全错误了。

print(dateStart)
print(reminderTrigger.nextTriggerDate())

这是输出 -

2016-09-10 05:18:58 +0000
Optional(2016-09-11 04:00:06 +0000)

不过应该​​是

2016-09-10 06:00:00 +0000

我以类似的方式尝试了每日、每周、每月、每年,它们都返回正确的下一个触发日期。每小时是唯一看起来不正确的。有什么想法吗?

【问题讨论】:

  • 我遇到了类似的问题,唯一的区别是唯一的偏移量不正确,即使在日期组件对象上设置 timeZone 也是如此。当日期到达时,通知不会被触发,但它也不会列在待处理或已发送的通知中。
  • 您的时区/日历设置肯定有问题。如果您不指定小时,它会自动设置为默认值,因此无论您的设备设置如何,它都有效。

标签: ios iphone swift ios10


【解决方案1】:

我遇到了和你一样的问题。

        UNNotificationRequest *request = notifyDict[[NSString stringWithFormat:@"%ld", indexPath.row]];
        UNTimeIntervalNotificationTrigger *trigger = (UNTimeIntervalNotificationTrigger *)request.trigger;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm:ss"];
        NSString *strDate = [dateFormatter stringFromDate:trigger.nextTriggerDate];

我得到了触发器的nextTriggerDate,但日期不是我之前为触发器设置的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2013-11-08
    • 2013-09-04
    • 2018-04-05
    • 2016-09-26
    相关资源
    最近更新 更多