【发布时间】:2023-04-03 02:29:02
【问题描述】:
Local Notification 有问题,UIDatePicker 用于通知设置时间,ViewController 用于选择重复通知的日期。如果我选择每周五重复一次,它将无法正常工作,或者改天。但是对于每一天的通知都很好。
我的代码:
func createRequestNotifications() {
let content = UNMutableNotificationContent()
content.title = "Time to learn"
content.body = "Hey bro time to get some knolage)"
content.sound = UNNotificationSound.default()
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
print(notification.day)
components.hour = notifTime
components.minute = notifMin
components.second = 0
components.day = notification.day
components.weekdayOrdinal = 10
components.timeZone = .current
let date = gregorian.date(from: components)!
if notification.day == 0 {
let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "any", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
print("evry day")
} else {
let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second, .day,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "any", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
print("date setup")
}
}
notification.day
是从另一个 ViewController 设置的 Int 值,我在其中选择日重复。
我很累,因为我不知道问题出在哪里,因为每个日期通知都在工作,但是对于特殊的日子没有:(
【问题讨论】:
标签: ios swift notifications uilocalnotification unusernotificationcenter