使用Swift 3中日期选择器的日期安排每周可重复的本地通知

时间:2016-10-30 18:16:36

标签: ios swift notifications local repeat

因此,我目前正在构建一个计划应用程序,并且我试图创建一个本地通知,以便在每周的特定日期,每周的特定时间触发。所以我做的第一件事就是获取事件开始时间的日期值,然后从开始时间值中减去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中的年份...或者它可能是其他什么?有什么想法吗?

2 个答案:

答案 0 :(得分:4)

正如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的建议使用日历执行时移。

答案 1 :(得分:0)

// 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)



}