【问题标题】:Repeat Local notification using UserNotifications in swift 4在 swift 4 中使用 UserNotifications 重复本地通知
【发布时间】:2025-12-15 20:30:03
【问题描述】:

我需要在特定时间和特定日期重复 local notification,例如周日和周五上午 10:30 或选定的工作日(周一和周六)。

switch repeatDays {
    case .never:
        dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
        flag = false
    case .everyDay:
        dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
    case .everyWeek:
        dateComponents = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date)
    case .everyMonth:
        dateComponents = Calendar.current.dateComponents([.day, .hour, .minute], from: date)
    case .everyYear:
        dateComponents = Calendar.current.dateComponents([.month, .day, .hour, .minute], from: date)
    } 

let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: flag)
    let request = UNNotificationRequest(identifier: uuid, content: content, trigger: notificationTrigger)

    let notiCurrent = UNUserNotificationCenter.current()
    notiCurrent.add(request) { (error) in
        if let error1 = error {
            print(error1.localizedDescription)
        }
    }

但每周重复通知不会重复SundayWednesday 或其他特定日期

【问题讨论】:

    标签: ios swift usernotifications


    【解决方案1】:
     let calendar = Calendar.current
        let components = DateComponents(year: 2018, month: 05, day: 06, hour: 20, minute: 22) // Set the date here when you want Notification
        let date = calendar.date(from: components)
        let comp2 = calendar.dateComponents([.year,.month,.day,.hour,.minute], from: date!)
        let trigger = UNCalendarNotificationTrigger(dateMatching: comp2, repeats: true)
    
        let content = UNMutableNotificationContent()
        content.title = "Notification Demo"
        content.subtitle = "Demo"
        content.body = "Notification on specific date!!"
    
        let request = UNNotificationRequest(
            identifier: "identifier",
            content: content,
            trigger: trigger
        )
    
        UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
            if error != nil {
                //handle error
            } else {
                //notification set up successfully
            }
        })
    

    【讨论】:

    • 这只是在特定日期触发,不会重复特定两天。
    • @你必须设置你的逻辑以使其重复参考这个*.com/questions/5762517/…
    【解决方案2】:
    func scheduleNotification(date:Date, body: String, titles : String)  {
        let trigger = UNTimeIntervalNotificationTrigger(
            timeInterval: 1.0,
            repeats: true)
        let content = UNMutableNotificationContent()
        content.title = body
        content.body = titles
        content.sound = UNNotificationSound.default()
        content.categoryIdentifier = "todoList"
        content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground") //Update is here
    
        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
    
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
            if let error = error {
                print(" We had an error: \(error)")
            }
        }
    }
    

    【讨论】: