2017-11-16 8 views
0

스위프트 4 &> 아이폰 OS 10.0 나는 특정 날짜에 주어진 시간에 지역 알림을 예약 할알림 센터 및 변경 시간대

(의 오후 3을 가정 해 봅시다). 나는 시간대에 관계없이 (시간대에 따른 알림의 자동 일정 재조정) 오전 3시에 알림이 언제나 해고되기를 바랍니다.

이전에 this SO post에 완벽하게 설명 된 것과 같이 정확히 UILocalNotifications '시간대를 조정하여 정확하게 달성 할 수있었습니다. 그러나 >iOS 10.0에서 UILocalNotifications은 사용되지 않습니다. 여기

내 코드입니다 :

func scheduleNotification(title: String, message: String, atDate: Date){ 

    let center = UNUserNotificationCenter.current() 

    // Create content 
    let content = UNMutableNotificationContent() 
    content.title = title 
    content.body = message 
    content.sound = UNNotificationSound.default() 

    // Create trigger 
    let calendar = Calendar(identifier: .gregorian) 
    let triggerDate = calendar.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: atDate) 
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false) 

    // Create identifier 
    let identifier = "\(UUID())" 

    // Create request & add to center 
    let request = UNNotificationRequest(identifier: identifier, 
             content: content, 
             trigger: trigger) 
    center.add(request, withCompletionHandler: { (error) in 
    }) 
    } 

질문 : 가 어떻게 알림이 시간대를 변경하는 제대로 트리거해야합니까?

답변

0

그래서 나는 그것을 잘 만들 수있었습니다. triggerDate에는 정확히과 같은 자동으로 nil 인 timeZone 변수가 있습니다.

triggerDate.timeZone은 정확히 this post에 설명 된 동작 인 UILocalNotification.timeZone처럼 동작합니다.

시간대를 변경할 때 시뮬레이터를 다시 시작하지 않아서 시뮬레이터에서 작동하지 않는 이유 중 하나가있었습니다. 다시 시작하면 모든 것이 예상대로 작동합니다.

참고 : 다시 시작해야하는 것은 아니지만 실행중인 시뮬레이터가 새 표준 시간대를 감지하는 데 걸리는 시간이 명확하지 않으므로 재시작이 가장 효율적이라고 생각합니다.