每天早上本地通知不起作用?

时间:2014-03-21 18:34:34

标签: ios uilocalnotification

我想每天早上发送本地通知,我每隔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中)谢谢!!

1 个答案:

答案 0 :(得分:0)

UILocalNotification有一个userInfo您可以设置为您想要的任何内容。如果要查找特定通知,可以迭代它们并检查userInfo的/ contents值。

  

每次我从多任务中删除应用程序

当你的应用程序像这样终止时,它不会收到回叫,所以不要依赖于了解它的发生。

相关问题