2017-10-23 12 views
-1

UNCalendarNotificationTrigger으로 매일 특정 시간에 반복 할 수 있습니다. UNTimeIntervalNotificationTrigger시간당 X 시간마다 푸시 알림 반복

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

나는 타이머를 만들 때 일부 간격으로 반복 할 수 있습니다.

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: Double(frequency*60), repeats: true)

어떻게하지만, 좀 유연 간격으로, 푸시 알림 시간에 반복받을 수 있나요? 예를 들어, 오전 12 시부 터 2 시간마다, 3 시간마다 또는 12 시간마다 등.

답변

0

음, 매일 특정 시간을 반복하도록 설정할 수 있다는 것을 알고 있다면 간격을 기준으로 시간을 계산하지 않으시겠습니까?

var components = DateComponents() 
components.hour = 5 
components.second = 0 

let interval: Double = 60 * 30 // 30 min 
let maxDuration: Double = 60 * 60 * 5 // 5 hours 
let startDate = Calendar.current.date(
    byAdding: components, 
    to: Calendar.current.startOfDay(for: Date())) 
let endDate = startDate.addingTimeInterval(maxDuration) 

let notificationCount: Int = Int((endDate.timeIntervalSince1970 - startDate.timeIntervalSince1970)/maxDuration) 

(0..<notificationCount) 
    .map({ startDate.addingTimeInterval(Double($0) * interval) }) 
    .forEach({ date in 
     // Schedule notification for each date here 
    }) 

범위와 하루를 직접 관리해야하지만 올바른 방향으로 가리켜 야합니다.