2017-01-28 2 views
1

최근에 내 프로젝트를 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 속성이없는 것과 같은 시간입니다.

+0

"매주 알림을 동시에 발생시키는 방법": http://stackoverflow.com/a/40856269/341994 – matt

+0

감사합니다. 예를 들어 월요일 오전 11시에만 지정할 수있는 방법을 설명해 주시겠습니까? 나는 그 정보를 어떻게 변수에 넣는지를 이해하지 못합니다. –

+0

시간대 11 및 평일 2의 날짜 구성 요소 – matt

답변

1

당신은 당신은 단지 그것은 반복 된 값이 적용되지 않는 모든 것을 떠나 단지 문제 오전 11시

월요일 예를 들어 지정할 수있는 방법을 설명 할 수. 따라서 귀하의 경우 날짜 구성 요소 hour:11weekday:2을 지정하고 다른 것은 지정하지 않으려합니다.

이가 놀이터에서 이것을 실행 작동 방식을 이해하려면, 당신은 d은 오전 11시에 오는 월요일 것을 볼 수

let dc = DateComponents(hour:11, weekday:2) 
let greg = Calendar(identifier: .gregorian) 
let d = greg.nextDate(after: Date(), matching: dc, matchingPolicy:.nextTime) 

.

UNNotificationTrigger가이 알림을 보낼 시점을 정확히 알 수 있습니다.

이제 이것을 repeats 트리거로 설정하면 월요일에 오전 11시에 일마다 을 지정했습니다.

+0

도움 주셔서 감사합니다. –