// 关键代码如下
- (void)resetClock:(NSDate*)date
{
// 试图取消以前的通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self saveDate:date];

if (date == nil) {
return;
}

// 设置新的通知
UILocalNotification* noti = [[[UILocalNotification alloc] init] autorelease];
// 设置响应时间
noti.fireDate = date;
// 设置时区,默认即可
noti.timeZone = [NSTimeZone defaultTimeZone];

// 重复提醒,这里设置一分钟提醒一次,只有启动应用,才会停止提醒。
noti.repeatInterval = NSMinuteCalendarUnit;
// noti.repeatCalendar = nil;

// 提示时的显示信息
noti.alertBody = @”时间到”;
// 下面属性仅在提示框状态时的有效,在横幅时没什么效果
noti.hasAction = NO;
noti.alertAction = @”open”;

// 这里可以设置从通知启动的启动界面,类似Default.png的作用。
noti.alertLaunchImage = @”lunch.png”;

// 提醒时播放的声音
// 这里用系统默认的声音。也可以自己把声音文件加到工程中来,把文件名设在下面。最后可以播放时间长点,闹钟啊
noti.soundName = UILocalNotificationDefaultSoundName;

// 这里是桌面上程序右上角的数字图标,设0的话,就没有。类似QQ的未读消息数。
noti.applicationIconBadgeNumber = 1;

// 这个属性设置了以后,在通过本应用通知启动应用时,在下面回调中
// – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// lanuchOptions里面会含有这里的userInfo.
// 正常点击应用图标进入应用,这个属性就用不到了
noti.userInfo = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];

// 生效
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
}

// 应用收到通知时,会调到下面的回调函数里,当应用在启动状态,收到通知时不会自动弹出提示框,而应该由程序手动实现。
// 只有在退出应用后,收到本地通知,系统才会弹出提示。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (application.applicationState == UIApplicationStateActive) {
// 如不加上面的判断,点击通知启动应用后会重复提示
// 这里暂时用简单的提示框代替。
// 也可以做复杂一些,播放想要的铃声。
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@”"
message:@”时间到”
delegate:self
cancelButtonTitle:@”关闭”
otherButtonTitles:nil, nil] autorelease];
[alert show];
}
}

#pragma mark – UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
[self resetClock:nil];
}
}

// 不足之处有:
1. 不能设置提示框样式,是横幅还是弹框
2. 不能灵活设置重复的时间,只能按秒,分,小时,天,周,月这样设置

示例工程代码参见:https://github.com/TianLibin/clock