【问题标题】:Scheduling weekly repeatable local notification with fire date from date picker in Swift 3使用 Swift 3 中日期选择器的触发日期安排每周可重复的本地通知
【发布时间】:2017-03-12 22:36:24
【问题描述】:

所以我目前正在构建一个日程安排应用程序,并且我正在尝试创建一个本地通知,以便在每周特定日期的特定时间触发。所以我要做的第一件事是获取事件开始时间的日期值,然后从开始时间值中减去 5 分钟,然后安排通知。以前很容易只需键入: notification.repeatInterval = CalendarUnit.WeekOfYear 但现在该命令在 Swift 3 中已被弃用,是的,所以我找到的唯一方法是:

 let someMinutesEarlier = Calendar.current.date(byAdding: .minute, value: -5, to: startTimePicker.date)

        let contentOfNotification = UNMutableNotificationContent()

        let interval = someMinutesEarlier?.timeIntervalSinceNow

        contentOfNotification.title = "Event starting"
        contentOfNotification.body = "Some notes"
        contentOfNotification.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval!, repeats: true)
        let request = UNNotificationRequest.init(identifier: notificationIdentifier, content: contentOfNotification, trigger: trigger)

        let center = UNUserNotificationCenter.current()
        center.add(request) { (error) in
            print(error as Any)
        }

但这只会安排一次通知(无论重复布尔值设置为true),因为 someMinutesEarlier 中的年份......或者它可能是别的什么?有什么想法吗?

【问题讨论】:

  • 为什么不使用 UNCalendarNotificationTrigger ?
  • 您是针对 ios10 之前的版本还是仅针对 ios10 的版本?如果您希望您的应用程序也能在早于 10 的版本上运行,我可能会暂时坚持使用已弃用的方法。否则,您将需要编写两个版本并使用available

标签: ios swift notifications local repeat


【解决方案1】:

正如 McNight 提到的,您可以像这样使用UNCalendarNotificationTrigger

let interval = 60 * 60 * 24 * 7 - 300 // One week minus 5 minutes.
let alarmTime = Calendar.current.date(byAdding: .second, value: interval, to: Date())!
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

(我没有测试过这段代码,但这应该能让你走上正确的道路)。

更多信息here

编辑: SwiftyCruz 建议的固定时间间隔计算。

编辑 2:更新为使用日历执行 RickiG 建议的时间转换。

【讨论】:

  • 我认为添加时间应该是 60 * ( 60 * 24 * 7 - 5) 或 60 * 60 * 24 * 7 - 300
  • 谢谢!是的,当然 UICalendarNotificationTrigger... 如此简单但需要帮助。谢谢大家,也谢谢 Paulw11 我会考虑这个,但主要是因为通知标识符,我使用新的 Swift 3。
  • 只要你对 ios10 满意,只有这才是最好的方法。
  • 您不应该向日历中的日期组件添加增量,而不是计算从今天开始的秒数“偏移量”。像这样:Calendar.current.date(byAdding: Calendar.Component, value: In, to: Date) - 如果你在闰年、闰秒左右的时候执行上述操作,它将关闭。跨度>
【解决方案2】:

// Swift2.3

func setLNotification(weekDay:Int , hour:Int, min:Int, second:Int, alertBody:String, type:String, isRepeate:Bool){

    let calender = NSCalendar(identifier: NSCalendarIdentifierGregorian)
    let dateComp: NSDateComponents?

    dateComp = calender?.components([.Year,.WeekOfMonth,.Month], fromDate: NSDate())
    dateComp?.hour = hour
    dateComp?.minute = min
    dateComp?.second = 00
    dateComp?.weekday = weekDay
    dateComp!.timeZone = NSTimeZone.localTimeZone()

    print(calender?.dateFromComponents(dateComp!))


    let SetCustomDate = calender?.dateFromComponents(dateComp!)

    print(SetCustomDate)

    let notification = UILocalNotification()
    if isRepeate == true{

        switch type {
        case "Weekly":

            notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*7)
            notification.repeatInterval = NSCalendarUnit.Weekday

        case "2 Weekly":

            notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*14)
            notification.repeatInterval = NSCalendarUnit.Day
        case "Monthly":
            notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*28)
            notification.repeatInterval = NSCalendarUnit.Day

        default:
            break;
        }

        notification.soundName = UILocalNotificationDefaultSoundName
        notification.repeatCalendar = calender
    }
    notification.alertTitle = "STATS"
    notification.alertBody = "Please update your Stats detail"
    notification.userInfo = ["uid":"reminder"]

    print(notification)

   UIApplication.sharedApplication().scheduleLocalNotification(notification)



}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多