Swift3 如何在iOS 10(Swift)中取消特定的定期本地通知并将其重新安排到下周

Swift3 如何在iOS 10(Swift)中取消特定的定期本地通知并将其重新安排到下周,swift3,ios10,unusernotificationcenter,Swift3,Ios10,Unusernotificationcenter,我创建了不同的本地通知,以设置工作日和周末连续重复 let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true) let content = UNMutableNotificationContent() content.body = "TIME_TO_STEP_SHAPA".localized content.sound = UNNotificationSound.default()

我创建了不同的本地通知,以设置工作日和周末连续重复

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let content = UNMutableNotificationContent()
content.body = "TIME_TO_STEP_SHAPA".localized
content.sound = UNNotificationSound.default()

let request = UNNotificationRequest(identifier: "\(notificationType)-\(reminderType)-\(day)", content: content, trigger: trigger)

UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    if settings.authorizationStatus != .authorized {
        print("Local notifications are not authorized by the user")
    }
}
UNUserNotificationCenter.current().add(request) {(error) in
    if error != nil {
        print("Error: \(error?.localizedDescription)")
    }
}
取消特定通知,根据适当的条件验证通知,并在下周更新相同的通知

if (type == notificationType && notificationWeekDay == currentWeekDay) {
    //Cancelling local notification
    app.cancelLocalNotification(notif)

    let fireDate = self.getUpdatedNotification(currentDate: currentLocalDate!, fireDate: notificationFireDate!)

    HikeCommonUtils.setUpLocalNotification(fireDate, type: notificationType, reminderType: reminderType)
}
以及使用

func getUpdatedNotification(currentDate: Date, fireDate : Date) ->Date {
    let calendar = Calendar.autoupdatingCurrent
    let dateComponents = (calendar as NSCalendar).components(([.year, .month, .day]), from: currentDate)
    let timeComponents = (calendar as NSCalendar).components(([.hour, .minute, .second]), from: fireDate)
    var dateComps = DateComponents()
    dateComps.day = dateComponents.day! + 7
    dateComps.month = dateComponents.month
    dateComps.year = dateComponents.year
    dateComps.hour = timeComponents.hour
    dateComps.minute = timeComponents.minute
    dateComps.second = timeComponents.second
    let itemDate = calendar.date(from: dateComps)
    return itemDate!
}
即使在删除删除日期的通知后,因为重复“true”

在iOS 10的本地通知中是否有添加开始日期的选项


提前谢谢

您可以将逻辑添加到
nextTriggerDate()

满足触发条件的下一个日期

讨论


使用此属性可确定与此触发器关联的通知下次何时送达。

使用更新通知触发器详细信息

var triggerWeekly = calendar.dateComponents([.hour, .minute, .second], from: fireDate)
        triggerWeekly.weekday = day
而不是

            let triggerWeekly = calendar.dateComponents([.weekday, .hour, .minute, .second], from: fireDate)
为firedate更新工作

            let triggerWeekly = calendar.dateComponents([.weekday, .hour, .minute, .second], from: fireDate)