【发布时间】:2014-03-22 17:27:11
【问题描述】:
我想每天早上发送一个本地通知,我每隔applicationDidEnterBackground: 安排一次,然后在applicationWillEnterForeground: 如果安排的通知超过0,我会取消它,问题是我不想使用[app cancelalllocalnotifications];因为有更多的通知计划我不想取消,所以我希望我解释清楚,无论如何这是我的代码:
我将本地通知安排在上午 9:00:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
user = [self.userdefaults objectForKey:@"user"];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate =
[calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:9] ;
[componentsForReferenceDate setMonth:11] ;
[componentsForReferenceDate setYear:2012] ;
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
NSDateComponents *componentsForFireDate =
[calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];
[componentsForFireDate setHour: 9] ;
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
self.diasnot = [[UILocalNotification alloc]init];
self.diasnot.fireDate = fireDateOfNotification;
self.diasnot.alertBody = [NSString stringWithFormat: @"Buenos Días %@, tienes %d tareas pendientes. Que tengas buen día!", [self.userdefaults objectForKey:@"user"] ,[[self.userdefaults objectForKey:@"tasks"] count]];
self.diasnot.timeZone = [NSTimeZone defaultTimeZone];
if([self.userdefaults objectForKey:@"sound"] == nil)
{
self.diasnot.soundName = UILocalNotificationDefaultSoundName;
}else{
self.diasnot.soundName = [self.userdefaults objectForKey:@"sound"];
}
[application scheduleLocalNotification:self.diasnot];
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[NSUserDefaults standardUserDefaults] objectForKey:@"tasks"] count];
}
在applicationWillEnterForeground: 我有以下内容:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNots = [app scheduledLocalNotifications];
if ([oldNots count] > 0) {
[app cancelLocalNotification:self.diasnot];
}
}
问题是,每次我从多任务处理中删除应用程序时,都会显示应用程序,如何在不删除其他通知的情况下解决此问题(这些通知安排在其他类别中,而不是在 AppDelegate 中)谢谢!!
【问题讨论】: