2011-02-16 94 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); 
    } 

它創建邏輯應用程序時得到background.Dont擔心應用程序通知給你設置的通知的通知的。