【问题标题】:iOS Schedule local notification is not working SwiftiOS Schedule本地通知不起作用Swift
【发布时间】: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


    【解决方案1】:

    对于特定的日子,您可以设置通知请参考下面的链接 https://stackoverflow.com/a/46094544/5069429 据此,您必须单独设置特定日期的通知 你可以使用这样的功能

    func setNotificationFor(weekday:Int,hour:Int,minute:Int,title:String,subTitle:String,body:String){
        let notification = UNMutableNotificationContent()
        notification.title = title
        notification.subtitle = subTitle
        notification.body = body
    
    
        var dateComponents = DateComponents()
        dateComponents.weekday = weekday  // 0,1,2,3,4,5,6
        dateComponents.hour = hour
        dateComponents.minute = minute
        let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
    
        let request = UNNotificationRequest(identifier: "notification1", content: notification, trigger: notificationTrigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }
    

    【讨论】:

    • @Andrew 从您的代码中传递 .day 而不是 weekDay 尝试使用 .weekDay 可能对您有所帮助
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多