iPhone:每日本地通知

我正在尝试实施本地通知

这是我设定的

// Current date NSDate *date = [NSDate date]; // Add one minute to the current time NSDate *dateToFire = [date dateByAddingTimeInterval:20]; // Set the fire date/time [localNotification setFireDate:dateToFire]; [localNotification setTimeZone:[NSTimeZone defaultTimeZone]]; 

而不是20,我想把一个固定的时间(每天)开始推。

例如:我想推动通知每6:00上午popup。

怎么可能呢?

谢谢

你只需要正确地创build一个NSDate对象作为你的发射date(时间)。 而不是使用[NSDate dateByAddingTimeInterval: 20] ,使用这样的东西:

 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setDay: 3]; [components setMonth: 7]; [components setYear: 2012]; [components setHour: 6]; [components setMinute: 0]; [components setSecond: 0]; [calendar setTimeZone: [NSTimeZone defaultTimeZone]]; NSDate *dateToFire = [calendar dateFromComponents:components]; 

这里是苹果NSDateComponents API文档

然后,当您将date添加到通知时,将重复间隔设置为一天:

 [localNotification setFireDate: dateToFire]; [localNotification setTimeZone: [NSTimeZone defaultTimeZone]]; [localNotification setRepeatInterval: kCFCalendarUnitDay]; 

与所有与date相关的代码一样,如果您的时区使用夏时制,请确保在切换到夏令时期间testing其工作原理。

我猜你需要的是NSDayCalendarUnit

你可以检查这个答案。 这里是另一个值得一读的教程。

  NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10]; UIApplication* app = [UIApplication sharedApplication]; UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease]; if (notifyAlarm) { notifyAlarm.fireDate = alertTime; notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; notifyAlarm.repeatInterval = 0; notifyAlarm.soundName = @"Glass.aiff"; notifyAlarm.alertBody = @"Staff meeting in 30 minutes"; [app scheduleLocalNotification:notifyAlarm]; }