2017-11-06 6 views
1

이전에 내 응용 프로그램에서 알림 목적으로 UILocalNotification을 사용하고 있습니다.UNNotificationRequest에서 반복 간격과 발동 날짜를 설정하는 방법

하지만 위의 API는 iOS 10.0에서 사용되지 않으므로 UserNotifications Framework의 UNNotificationRequest을 사용해야합니다.

UILocalNotification 사용 나는 아래로 화재 일뿐만 아니라 반복 간격을 설정 할 수 있었다 : 나는 통지가 불을 얻을 것이다 때 내일 날짜를 설정 할 수있어 위의 코드에서

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 

    NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]]; 
    localNotification.fireDate = tomorrow; 


    if(repeatUnit!=0){ 
      localNotification.repeatInterval = NSWeekCalendarUnit; 
    } 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

또한 나는 반복을 설정했다 화재 일로부터 1 주일 간격으로 시작됩니다.

UserNotifications Framework의 UNNotificationRequest을 사용하여이를 달성 할 수있는 방법.

새 API에서는 및 UNTimeintervalNotificationTrigger 트리거를 사용하여 UNNotificationRequest을 시작할 수 있습니다.

사용할 수있는 방법이 있습니까? UILocalNotification처럼 설정할 수 있습니까?

답변

1

화재 날짜와 반복 간격을 사용할 수는 있지만 예전처럼 명확하지 않습니다. 알림 트리거를 초기화 할 때 반복 매개 변수를 YES으로 설정합니다. 매일

반복 : 반복 간격은 당신이 dateMatching 매개 변수를 통해 트리거로 전달 날짜 구성 요소에 의해 정의된다
그냥 날짜 구성 요소를 시간, ,
통과를 ⟶ 방아쇠는 그 시간에 매일에 발화 할 것이다

주간 반복 :
또한 통과하십시오 원하는 평일 구성 요소 : 평일, 시간, ,
⟶ 그 때 여기

에서 그 평일에 매주 실행됩니다 트리거는 같은 내일 알림을 발사 한 예이다

목표 - C :

UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init]; 

// find out what weekday is tomorrow 
NSDate *sameTimeTomorrow = [NSDate dateWithTimeIntervalSinceNow:60*60*24]; 
NSInteger weekdayTomorrow = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate: sameTimeTomorrow]; 

// set the current time (without weekday) 
unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
NSDateComponents *firstFireDateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:sameTimeTomorrow]; 

// add tomorrow's weekday 
firstFireDateComponents.weekday = weekdayTomorrow; 

// set a repeating trigger for the current time and tomorrow's weekday 
// (trigger repeats every week on that weekday at the current time) 
UNCalendarNotificationTrigger *notificationTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:firstFireDateComponents repeats:YES]; 

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification" content:notification trigger:notificationTrigger]; 
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler: nil]; 
시간과 그것을 매주 반복 0

스위프트 :

let notification = UNMutableNotificationContent() 

// find out what weekday is tomorrow 
let weekDayTomorrow = Calendar.current.component(.weekday, from: Date(timeIntervalSinceNow: 60*60*24)) 

// set the current time (without weekday) 
var firstFireDateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date()) 

// add tomorrow's weekday 
firstFireDateComponents.weekday = weekDayTomorrow 

// set a repeating trigger for the current time and tomorrow's weekday 
// (trigger repeats every week on that weekday at the current time) 
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: firstFireDateComponents, repeats: true) 

let request = UNNotificationRequest(identifier: "notification", content: notification, trigger: notificationTrigger) 
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 
+0

니스. 고맙습니다. –

관련 문제