我创建了不同的本地通知来设置工作日和周末连续重复
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 的本地通知中添加开始日期的选项?
提前致谢!!