iOS计划本地通知不起作用Swift

问题描述 投票:-1回答:1

我有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.大约

Int值是从另一个ViewController设置的,其中我选择一天重复。

而我累了,因为我不知道问题出在哪里,因为每个日期通知都有效,但是对于特殊日子没有:(

ios swift notifications uilocalnotification unusernotificationcenter
1个回答
0
投票

特殊日子你可以设置通知请参考下面链接qazxsw poi根据你必须单独设置特定日期的通知你可以使用这样的功能

https://stackoverflow.com/a/46094544/5069429
© www.soinside.com 2019 - 2024. All rights reserved.