2016-10-30 2 views
1

저는 현재 일정 응용 프로그램을 만들고 있으며 매주 특정 요일에 특정 시간에 특정 지역의 알림을 작성하려고합니다. . 그래서 내가하는 첫 번째 일은 이벤트의 시작 시간의 날짜 값을 얻은 다음 시작 시간 값에서 5 분을 뺀 다음 알림을 예약합니다. (상관없이스위프트 3의 날짜 선택 도구에서 발동 날짜가있는 매주 반복 가능한 지역 알림 예약

let someMinutesEarlier = Calendar.current.date(byAdding: .minute, value: -5, to: startTimePicker.date) 

     let contentOfNotification = UNMutableNotificationContent() 

     let interval = someMinutesEarlier?.timeIntervalSinceNow 

     contentOfNotification.title = "Event starting" 
     contentOfNotification.body = "Some notes" 
     contentOfNotification.sound = UNNotificationSound.default() 

     let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval!, repeats: true) 
     let request = UNNotificationRequest.init(identifier: notificationIdentifier, content: contentOfNotification, trigger: trigger) 

     let center = UNUserNotificationCenter.current() 
     center.add(request) { (error) in 
      print(error as Any) 
     } 

하지만,이 경우에만 일정 알림 번만 : 이전에는 단지 매우 쉬웠다 입력했다 : notification.repeatInterval = CalendarUnit.WeekOfYear을하지만 지금은 명령 스위프트 3에서 사용되지 않으며 그래, 내가 볼 수있는 유일한 방법입니다 repeat 부울이 true로 설정 됨), someMinutesEarlier의 연도로 인해 ... 아니면 다른 것일 수 있습니까? 어떤 생각? McNight가 했나요으로

+0

왜 당신은 UNCalendarNotificationTrigger를 사용하지 않는? – McNight

+0

ios10 또는 ios10보다 이전 버전 만 타겟팅합니까? 10보다 이전 버전에서도 앱을 실행하고 싶다면 지금은 더 이상 사용되지 않는 방식을 고수 할 것입니다. 그렇지 않으면 두 버전을 모두 쓰고'available '을 사용해야합니다. – Paulw11

답변

3

, 당신과 같이 UNCalendarNotificationTrigger를 사용할 수 있습니다

let interval = 60 * 60 * 24 * 7 - 300 // One week minus 5 minutes. 
let alarmTime = Calendar.current.date(byAdding: .second, value: interval, to: Date())! 
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: alarmTime) 
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) 

(이 코드를 테스트하지 않은, 그러나 이것은 옳은 길을 얻어야한다).

자세한 정보 here.

편집 : SwiftyCruz가 제안한 고정 시간 간격 계산.

편집 2 : RickiG에서 제안한대로 시간 이동을 수행하기 위해 달력을 사용하도록 업데이트되었습니다.

+0

시간은 60 * (60 * 24 * 7-5) 또는 60 * 60 * 24 * 7-300이되어야한다고 생각합니다. – Cruz

+0

감사합니다! 예, 물론 UICalendarNotificationTrigger ... 너무 간단하지만 도움이 필요합니다. 감사합니다 모두 Paulw11 나는 이것을 고려하지만, 주로 통지 식별자 때문에 새로운 Swift 3을 사용합니다. –

+0

ios10 만 있으면 가장 좋은 방법입니다. – Paulw11

0

는 // Swift2.3

func setLNotification(weekDay:Int , hour:Int, min:Int, second:Int, alertBody:String, type:String, isRepeate:Bool){ 

    let calender = NSCalendar(identifier: NSCalendarIdentifierGregorian) 
    let dateComp: NSDateComponents? 

    dateComp = calender?.components([.Year,.WeekOfMonth,.Month], fromDate: NSDate()) 
    dateComp?.hour = hour 
    dateComp?.minute = min 
    dateComp?.second = 00 
    dateComp?.weekday = weekDay 
    dateComp!.timeZone = NSTimeZone.localTimeZone() 

    print(calender?.dateFromComponents(dateComp!)) 


    let SetCustomDate = calender?.dateFromComponents(dateComp!) 

    print(SetCustomDate) 

    let notification = UILocalNotification() 
    if isRepeate == true{ 

     switch type { 
     case "Weekly": 

      notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*7) 
      notification.repeatInterval = NSCalendarUnit.Weekday 

     case "2 Weekly": 

      notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*14) 
      notification.repeatInterval = NSCalendarUnit.Day 
     case "Monthly": 
      notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*28) 
      notification.repeatInterval = NSCalendarUnit.Day 

     default: 
      break; 
     } 

     notification.soundName = UILocalNotificationDefaultSoundName 
     notification.repeatCalendar = calender 
    } 
    notification.alertTitle = "STATS" 
    notification.alertBody = "Please update your Stats detail" 
    notification.userInfo = ["uid":"reminder"] 

    print(notification) 

    UIApplication.sharedApplication().scheduleLocalNotification(notification) 



} 
관련 문제