Repeating local notifications for specific days of week (Swift 3 IOS 10)

前端 未结 2 1525
灰色年华
灰色年华 2020-12-08 03:45

I am trying to schedule local notifications for specific days of week (e.g. Monday, Wednesday and etc) and then repeat them weekly. This is how the screen for setting notifi

2条回答
  •  庸人自扰
    2020-12-08 04:13

     let content = UNMutableNotificationContent()
     content.title = NSString.localizedUserNotificationString(forKey: "Wake up!", arguments: nil)
     content.body = NSString.localizedUserNotificationString(forKey: "Rise and shine! It's morning time!",
                                                                    arguments: nil)
     content.categoryIdentifier = "TIMER_EXPIRED"
    
     let weekdaySet = [6,5]
    
       for i in weekdaySet {
    
            var dateInfo = DateComponents()
            dateInfo.hour = 16
            dateInfo.minute = 44
            dateInfo.weekday = i
            dateInfo.timeZone = .current
    
            let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
            let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
            center.add(request) { (error : Error?) in
    
            if let theError = error {
                    print(theError.localizedDescription)
                    }
                }
           }
    

提交回复
热议问题