2014-09-17 2 views
2

내 앱에 오전 7시에 현지 알림을 준비하는 몇 줄의 코드가 있습니다. 문제는 앱을 종료 할 때마다 알림이 표시된다는 것입니다. 나는 단지 정해진 시간이 지나면이 일이 일어나기를 바랄 뿐이다. 내 applicationDidEnterBackground :와에서매번 로컬 알림이 앱을 닫습니다.

:

NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar 
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 
[components setHour:7]; 
UIApplication *app = [UIApplication sharedApplication]; 
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init]; 
if (notifyAlarm) { 
    notifyAlarm.fireDate = [calendar dateFromComponents:components]; 
    notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; 
    notifyAlarm.repeatInterval = 0; 
    notifyAlarm.soundName = @"not.wav"; 
    notifyAlarm.alertBody = @"test"; 
    [app scheduleLocalNotification:notifyAlarm]; 
} 

내 applicationWillEnterForeground에서 :

UIApplication *app = [UIApplication sharedApplication]; 
    NSArray *oldNotifications = [app scheduledLocalNotifications]; 
    if ([oldNotifications count] > 0) { 
     [app cancelAllLocalNotifications]; 
    } 

나는 [계산 oldNotifications] 문제가있는 것 같아요. 금액을 확인하기 위해 NSLog를 구현 한 후에는 매번 0이 표시됩니다. 도움말? :)

+0

오전 7시 이후의 현재 시간은? 내일 새벽 7시에 갖다 놓으시겠습니까? –

+0

예, 현재 시간은 오전 7시 이후입니다. 나는 tomorow를 의미하지 않는다, 나는 notif를 표시하고 싶다. 매일 7시 – Vince

+0

그래서, 지금은 시간이 오전 10시라고합니다. 3 시간 전에 알림을 만들고 있습니다. 앞으로 21 시간 동안 생성해야합니다. –

답변

2

이 코드를 시도해보십시오. NSLocalNotification을 매일 반복하려면 repeatInterval을 NSDayCalender 단위로 설정해야합니다.

NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar 
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 
[components setHour:7]; 
UIApplication *app = [UIApplication sharedApplication]; 
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init]; 
if (notifyAlarm) 
{ 
    notifyAlarm.fireDate = [calendar dateFromComponents:components]; 
    notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; 
    notifyAlarm.repeatInterval = NSDayCalendarUnit; 
    notifyAlarm.soundName = @"not.wav"; 
    notifyAlarm.alertBody = @"test"; 
    [app scheduleLocalNotification:notifyAlarm]; 
} 
+0

감사! 내가 필요한 것! – Vince