2017-09-09 1 views
0

내 iOS 앱에서 이벤트 시작 15 분 전에 사용자에게 알리는 localNotification을 만들려고합니다. 그러나 나는 붙어있다. 데이터를 저장하기 위해 CoreData을 사용하고 있습니다. 내가 만들 수있는 Appointment 개체가 있습니다. date 특성은 Appointment 개체와 연결됩니다. 나는 정말로 그것에 매달렸다. timeInterval 및 나머지 알림 프로세스를 설정하는 방법을 모르겠습니다.CoreData 속성을 사용하여 UILocalNotifications 만들기

Appointment이 생성 될 때부터 시작하기 15 분 전에 timeInterval을 설정하는 방법을 모르겠습니다.

여기 내 코드의 일부입니다 :

func scheduleNotifications() { 
    let content = UNMutableNotificationContent() 
    guard let client = client, let name = client.name, let formula = formula, let date = formula.date else { return } 
    content.title = "BookMe" 
    content.subtitle = "" 
    content.body = "Your appointment with \(name) will begin soon." 
    content.badge = 1 

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: ??, repeats: false) 

편집 : 이것은 내가 가지고 있지만 아무것도 발사되지 않은 것입니다. 나는 당신이 그 이전에 알림 15 분 발사 할 수 있도록 지금 15 분 일부 날짜 이전 사이의 시간 간격을 찾기 위해 찾고 그것을 이해로서

let date = formula.date 
let fireDate = Calendar.current.date(byAdding: DateComponents(minute: -15), to: date as Date) 
guard let timeInterval = fireDate?.timeIntervalSince(Date()) else { return } 

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false) 

let request = UNNotificationRequest(identifier: self.timerUserNotificationIdentifier, content: content, trigger: trigger) 

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
+0

날짜를 작성하는 데 15 분짜리 만드는 방법을 묻는 중입니까? – Abizern

+0

@Abizern 본질적으로 그렇습니다 –

답변

0

.

은 여기가 놀이터

// Create a date in the future - this is what you get from your own data object and I'm creating it here just so I have a date. 
let scheduledDate = Calendar.current.date(from: DateComponents(year: 2017, month: 09, day: 22, hour: 22))! 

// Create a date 15 minutes earlier than your shcheduled date, this is when you want your notification to fire 
let fireDate = Calendar.current.date(byAdding: DateComponents(minute: -15), to: scheduledDate)! 

// Then just work out the time interval between the fire date and now to get the time interval 
let timeInterval = fireDate.timeIntervalSince(Date()) 

변명 생성 된 날짜의 풀기 힘에 노크 빠른 예제,이는 예를 들어 있기 때문에, 대신 느낌표를 사용하지 우아 오류를 처리해야합니다. 사용하려는

UNTimeIntervalNotificationTrigger을 추가 할

편집 now 사이 TimeInterval이 필요하며 시간을 당신은 통지를 해고하고자합니다. TimeInterval은 초를 나타내는 Double입니다. 이 경우와 같이 어떤 경우에는 알림을 시작하려는 시간과 현재 시간 사이의 시간 (초)을 나타내는 지연이 표시됩니다. 다른 경우에는 고정 날짜의 초 수를 날짜로 나타냅니다. 이 고정 날짜는 timeIntervalSince1970 - "1970 년 1 월 1 일 00:00:00 UTC의 날짜 개체 사이의 간격"입니다. UNIX 타임 스탬프 또는 timeIntervalSinceReferenceDate - "날짜 개체와 2001 년 1 월 1 일 00:00:00 UTC 사이의 간격"에 사용하는 값입니다.

무엇을 하든지 직접 초를 추가하거나 제거하여 날짜를 수정하려는 유혹에 저항하십시오. 대신 DateComponents를 사용하십시오.

+0

나는 이것을했지만 구현 된 날짜를 구현했으며 알림이 실행되지 않습니다 –

+0

당신이 한 일을 볼 수 없으므로 도움이되지 않습니다. – Abizern

+0

위의 편집 된 코드 확인 –

관련 문제