1

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

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!
}

即使由于重复“真”而删除了已删除日期的通知触发。

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

提前致谢!!

4

2 回答 2

1

更新通知触发器详细信息

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

代替

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

为firedate更新工作

于 2017-04-21T06:22:50.197 回答
0

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

func nextTriggerDate()

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

讨论

使用此属性可确定下一次传递与此触发器关联的通知的时间。

于 2017-04-07T20:56:08.320 回答