Question

I want to send a local Notification every morning, Im scheduling it every applicationDidEnterBackground: and then in applicationWillEnterForeground: I cancel it if the scheduled notifications is more than 0, the problem Is that I dont want to use [app cancelalllocalnotifications]; because there are more notifications scheduled that I dont want to cancel, so I hope I explain well, anyway heres my code:

I schedule the Local Notification to 9:00 AM:

- (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];

}

And in applicationWillEnterForeground: I have the following:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];

    NSArray *oldNots = [app scheduledLocalNotifications];

    if ([oldNots count] > 0) {
        [app cancelLocalNotification:self.diasnot];
    }


}

And the problem is that every time I delete the app from multitasking the app Is showed, how can I fix this without deleting my other notifications (this ones are scheduled in other clases, not in AppDelegate) Thanks!!

Was it helpful?

Solution

UILocalNotification has a userInfo That you can set to whatever you want. When you want to find a specific notification you can iterate through them and check the value of / contents of userInfo.

every time I delete the app from multitasking

Your app receives not callback when it is terminated like this so don't rely on knowing about it happening.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top