在 swift 4 中使用 UserNotifications 重复本地通知

Posted

技术标签:

【中文标题】在 swift 4 中使用 UserNotifications 重复本地通知【英文标题】:Repeat Local notification using UserNotifications in swift 4 【发布时间】:2018-08-30 10:00:56 【问题描述】:

我需要在特定时间和特定日期重复 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 或其他特定日期

【问题讨论】:

【参考方案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)")
        
    

【讨论】:

以上是关于在 swift 4 中使用 UserNotifications 重复本地通知的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 4 中使用字符串下标 [重复]

在 Swift 4 中使用 Decodable 解析 JSON

在 swift 4.0 中使用 Alamofire/SwiftyJSON 时出错

如何在 swift 4 中使用 UnsafeMutablePointer

在 swift 4 中使用可编码的 JSON 时出错?

在 Swift 4.2 中 NSCollectionView 的 SupplementaryElementKind 字符串值中使用啥