繁体   English   中英

UILocalNotification repeatInterval中断timeZone和fireDate

[英]UILocalNotification repeatInterval breaks timeZone and fireDate

我正在我的应用中安排一批通知,有些通知具有重复的间隔,有些则只是在特定日期触发。 我正在使用以下方法设置通知以创建通知:

func notification(date: Date, repeatUnit: NSCalendar.Unit?) -> UILocalNotification {
    let notification = UILocalNotification()
    notification.category = "ReminderCategory"
    notification.alertTitle = "Test"
    notification.alertBody = "Test Body"
    notification.soundName = "Sound1.m4a"
    notification.fireDate = date
    notification.repeatInterval = repeatUnit ?? NSCalendar.Unit(rawValue: 0)
    notification.timeZone = TimeZone.init(secondsFromGMT: 0)!

    return notification
}

如果将repeatUnit变量设置为任何NSCalendar.Unit单位,则通知将在正确的时间(针对本地时区) NSCalendar.Unit

但是,如果我未设置repeatInterval ,则将通知fireDates设置为过去的某种方式,并在我安排通知后立即触发。

有人知道发生了什么吗?

这对我有用。

    let center = UNUserNotificationCenter.current()

    let content = UNMutableNotificationContent()
    content.title = "Title"
    content.body = "Your text"
    content.categoryIdentifier = "reminder-notification"
    content.sound = UNNotificationSound.default()


    var dateComponents = DateComponents()
    dateComponents.hour = Calendar.current.component(.hour, from: date)
    dateComponents.minute = Calendar.current.component(.minute, from: date)

    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
    center.add(request)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM