如何解决在今天而不是明天使UNUserNotification静音
所以我有一个小问题。 我的应用程序有重复的通知,但是如果用户在应用程序中添加或删除了某些内容,我希望今天下一个通知被静音/删除,但我仍然希望它像往常一样关闭。 是否可以使今天的待处理通知静音,但不能使重复的通知静音?
我找到了一种解决方法,目前这就是我的做法。
感谢您的帮助
let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: { (settings) in
if settings.authorizationStatus == .authorized {
let startDate = self.defaults.object(forKey: startingTimeString) as! Date
let endDate = self.defaults.object(forKey: endingTimeString) as! Date
let intervals = self.defaults.integer(forKey: reminderIntervalString)
let tempStart = Calendar.current.dateComponents([.hour,.minute],from: startDate)
let tempEnd = Calendar.current.dateComponents([.hour,from: endDate)
let tempNow = Calendar.current.dateComponents([.hour,from: Date())
//checks if the current time is within the notifications time span.
if ((tempStart.hour! == tempNow.hour! && tempStart.minute! < tempNow.minute!) ||
tempStart.hour! < tempNow.hour!) &&
((tempEnd.hour! == tempNow.hour! && tempEnd.minute! > tempNow.minute!) ||
tempEnd.hour! > tempNow.hour!)
{
//sets new reminders for the notification range
setReminders(Calendar.current.date(from: tempStart)!,Calendar.current.date(from: tempEnd)!,intervals)
current.getPendingNotificationRequests {(requests) in
for request in requests {
if let trigger = request.trigger as? UNCalendarNotificationTrigger,let triggerDate = trigger.nextTriggerDate(){
let difference = Calendar.current.dateComponents([.hour,from: Date(),to: triggerDate)
let differenceInMunutes = (difference.hour! * 60) + difference.minute!
// Checks if the time to the next notification is within half of the interval time.
if differenceInMunutes < Int(Double(intervals) * 0.5) {
current.removePendingNotificationRequests(withIdentifiers: [request.identifier])
}
}
}
}
}
}
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。