2012-07-26 85 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];