최근에 내 프로젝트를 IOS 10으로 업데이트했습니다. 등록한 모든 로컬 알림이 실행되지 않았습니다. 로컬 알림을 사용자 알림으로 변환하려고했지만 동일한 효과가 없습니다. 다음은 변환하려고 시도한 원본 코드입니다. viewDidLoad 내 :스위프트 : 로컬 알림을 사용자 알림으로 변환 IOS 10
guard let settings = UIApplication.shared.currentUserNotificationSettings else { return
}
if settings.types == UIUserNotificationType() {
let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(ac, animated: true, completion: nil)
return
}
var dateComp: DateComponents = DateComponents()
dateComp.year = 2017;
dateComp.month = 01;
dateComp.day = 28;
dateComp.hour = 11;
dateComp.minute = 0;
(dateComp as NSDateComponents).timeZone = TimeZone.current
let calender:Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
let date:Date = calender.date(from: dateComp)!
let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Notification!"
notification.alertAction = "You just Received a notification!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["customField1": "w00t"]
notification.repeatInterval = NSCalendar.Unit.weekOfYear
UIApplication.shared.scheduleLocalNotification(notification)
이로 인해 토요일마다 오전 11시에 현지 알림이 실행됩니다. viewDidLoad에있어서
func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Notification"
content.body = "You just Received a notification!"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Error")
}
}
}
: 사용자가을 이용하여 날짜를 선택하면
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
print("Error")
}
}
이 코드는 잘 작동 여기
내가 IOS (10)와 동일한 효과를 시도하고 달성하는 데 사용되는 코드 datePicker,하지만 프로그래밍 방식으로 알림의 날짜를 현지 알림 (2017-28-01 11:00)과 동일한 날짜로 설정하는 방법과 매주 알림을 발생시키는 방법을 알아낼 수 없습니다. repeatInterval 속성이없는 것과 같은 시간입니다.
"매주 알림을 동시에 발생시키는 방법": http://stackoverflow.com/a/40856269/341994 – matt
감사합니다. 예를 들어 월요일 오전 11시에만 지정할 수있는 방법을 설명해 주시겠습니까? 나는 그 정보를 어떻게 변수에 넣는지를 이해하지 못합니다. –
시간대 11 및 평일 2의 날짜 구성 요소 – matt