5

我想在每天上午 8:00 连续 30 天安排 UILocalNotificaion,并且我想仅使用一个 UILocationNotification 实例来实现该功能。这是我安排本地通知的代码。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:@"08:00"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody = @"You just received a local notification";
localNotification.alertAction = @"View Details";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[formatter release];
[date release];

它将在上午 08:00 永远触发每日通知,但我只想连续 30 天收到通知。最简单的解决方案是触发 30 个 UILocalNotifications,但我想用 1 个 UILocalNotification 实例来做到这一点。我怎样才能做到这一点?请帮忙。

4

2 回答 2

4

创建通知时,在 UILocalNotification userInfo 属性中保存 NSDate 对象。当您打开通知时,请使用 userinfo 中的日期检查当前日期。如果差异超过 30 天,您可以取消本地通知。

于 2013-10-31T12:56:03.320 回答
0
NSDate *now = [NSDate date];

// now a NSDate object for now + 30 days
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:30];
NSDate *endDate = [gregorian dateByAddingComponents:offsetComponents toDate:now options:0];

之后检查:

if([currentDate isEarlierDate:endDate]){
//Fire the notification
}
于 2013-10-31T13:02:18.120 回答