2012-07-26 81 views

回答

1

這聽起來像你有兩個問題。首先,本地通知是在過去設置了一個啓動日期的情況下創建的 - 這就是爲什麼一旦您打開應用程序就會顯示它。

其次,您可能會將通知的repeatInterval設置爲非零值,這會導致它不止一次出現。

請參見下面的代碼來設置一個本地通知火在下午3點:

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.alertBody = @"This is a test alert"; 
NSCalendar *currentCalendar = [NSCalendar currentCalendar]; 

NSDateComponents *comps = [[NSDateComponents alloc] init]; 
[comps setHour: 15]; 
[comps setMinute: 0]; 
[comps setSecond: 0]; 
NSDate *threePM = [currentCalendar dateFromComponents:comps]; 

// Test if the current time is after three or not: 
if(threePM != [threePM earlierDate: [NSDate date]]) 
{ 
    comps = [[NSDateComponents alloc] init]; 
    [comps setDay: 1]; 
    threePM = [currentCalendar dateByAddingComponents: comps toDate: threePM options: 0]; 
} 

localNotification.fireDate = threePM; 
localNotification.repeatInterval = 0; 

[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];