2011-02-16 4 views
0

나는 알림을 예약하고 경고 메시지를 표시해야합니다 전에 경고 60분에게 그것을 제공 ...UILocalNotification 일정 경고

내가 알림을 추가 자마자 내 앱 위임의 방법이라고

:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

알림이 백그라운드에서 경고로 표시된다는 것을 어떻게 알 수 있습니까? 내가 무시하거나 그 60 분 간격으로 예약 된 경고가 주어 확인하기 위해 사용해야하는 다른 위임 방법은 ... 거기에

- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore 
{ 
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

    NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    //NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSDateComponents *currentDateComponents = [calendar components:(NSWeekdayCalendarUnit | 
                    NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item]; 

    NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]); 

    NSLog(@"[currentDateComponents minute]: %i", [currentDateComponents minute]); 
    NSLog(@"[currentDateComponents hour]: %i",  [currentDateComponents hour]); 
    NSLog(@"[currentDateComponents day]: %i",  [currentDateComponents day]); 
    NSLog(@"[currentDateComponents week]: %i",  [currentDateComponents week]); 
    NSLog(@"[currentDateComponents month]: %i",  [currentDateComponents month]); 
    NSLog(@"[currentDateComponents year]: %i",  [currentDateComponents year]); 

    [dateComps setDay: [currentDateComponents day]]; 

    [dateComps setMonth:[currentDateComponents month]]; 

    [dateComps setYear:[currentDateComponents year]]; 

    [dateComps setHour:[currentDateComponents hour]]; 

    [dateComps setMinute:[currentDateComponents minute]]; 

    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 

    [dateComps release]; 

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

    if (localNotif == nil) 
     return; 

    localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)]; 

    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

    localNotif.alertBody = [NSString stringWithFormat:@"%@\n%@", 
          streetAddress, 
          stringOfWhenAuctionIsOn]; 

    localNotif.alertAction = NSLocalizedString(@"View Details", nil); 

    localNotif.soundName = UILocalNotificationDefaultSoundName; 

    localNotif.applicationIconBadgeNumber = 1; 

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress 
                 forKey:idOfStreetAlert]; 

    localNotif.userInfo = infoDict; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

답변

1

알림이 나타나면 바로 fireDate 속성에 문제가있을 수 있습니다. 보내진 날짜가 정확한지 확인하려는 것 같지만 fireDate가 예상 한 것임을 확인해야합니다.

또한, 당신은 아마도 동일한 효과를 달성하기 위해

localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore]; 

을 할 수 있고 NSDateComponents으로 허비 할 필요가 없습니다. 나는 이것을 테스트하지는 않았지만 효과가있을 수 있습니다.

타이머가 작동하고 앱이 실행되지 않을 때 무슨 일이 일어나는가? 앱이 종료되지 않은 경우 application:didReceiveLocalNotification:이 호출됩니다. 다른면에서 앱이 종료 된 경우 @Ishu가 지적한 application:didFinishLaunchingWithOptions:을 확인해야합니다. 나는 보통 코드를 복제하는 것을 피하기 위해 일반적인 방법으로 통지를 전달한다.

+0

그냥 궁금 해서요, 간격을 음수로 설정하는 이유는 무엇입니까? \ –

0

, 당신이 didFinishLaunchingWithOptions에서 코드를 작성할 필요가 배경 없음 위임 방법,

UILocalNotification *localNotif = 
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 

    if (localNotif) { 
     //your code 
     NSLog(@"Recieved Notification %@",localNotif); 
    } 

앱이 백그라운드에서 알림을받을 때 논리를 만듭니다. 앱이 알림을 설정한다는 알림을 받으십시오. 걱정하지 마세요.