创建明天开始的每日本地通知

时间:2014-04-17 06:52:48

标签: ios uilocalnotification

我有一个应用程序,每天都会提醒用户有一个待办事项,但我不需要提醒他们是否已经这样做了。

我解决问题的方法是创建一个随机时间每天启动的本地通知。当一个todo项目已经完成当天,我将当地通知日重新安排到明天,取消它并创建一个明天的新日期。但是这个解决方案并不起作用。看起来,当您指定每天触发本地通知(repearInterval = NSDayCalendarUnit)时,它不会遵守fireDate day属性。

这是我的代码:

/*** notification is created like this ***/
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [self randomHourMinFromDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"A todo item is due!";
localNotif.alertAction = @"View Item";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSDayCalendarUnit;


/*** some other part of the code ***/
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notification = [scheduledNotifications firstObject];

// adds 1 day to notification fireDate
NSDate *newFireDate = [notification.fireData dateByAddingTimeInterval:60*60*24*1];

[[UIApplication sharedApplication] cancelLocalNotification:notification];
notification.fireDate = newFireDate;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
你能告诉我我做错了什么吗?或者推荐一个更好的解决方案? TIA!

1 个答案:

答案 0 :(得分:0)

我有个主意。当todo项目已在当天完成时,取消通知并创建一个新通知。将sparkate设置为当前日期,并设置dateByAddingTimeInterval:60 * 60 * 24.

localNotification.fireDate = [CurrentDate dateByAddingTimeInterval:60*60*24];

愿这可行。

相关问题