Question

After goggling for 2 days i couldn't find any solution as if its clear to everyone (but me) !

I need the:

Alert.applicationIconBadgeNumber = x  

to be updated in background each time the notification fires, I am repeating the notify by:

notif.repeatInterval = NSMinuteCalendarUnit  

Repeating is working fine every 1 m. when the app goes in background, but the BadgeNumber dosent get updated, it takes the 1st updated date value only.

I am calling the scheduleNotification method by viewDidLoad

Here is my full code:

- (void)scheduleNotification {
UILocalNotification *notif;
notif = [[[UILocalNotification alloc] init] autorelease];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.fireDate = [[NSDate date] dateByAddingTimeInterval:5];
notif.repeatInterval = NSMinuteCalendarUnit; 

NSInteger BadgeNumber = [self BadgeNumber];
NSInteger *BadgeNumberPointer = &BadgeNumber;
NSString *BadgeNumberString = [NSString stringWithFormat:@"%i", BadgeNumber];

notif.applicationIconBadgeNumber = *BadgeNumberPointer;

notif.alertBody = BadgeNumberString;
notif.alertAction = @"Hello";

[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}

-(int)BadgeNumber{
NSDate *currentDateUpdate = [[NSDate alloc] init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:@"dd"];
NSString *dateCheckUpdate = [formatter2 stringFromDate:currentDateUpdate];
NSInteger dateCheckUpdateInt = [[dateCheckUpdate substringWithRange:NSMakeRange(0, 2)] integerValue];

int BadgeNumber = dateCheckUpdateInt;
return BadgeNumber;
}

Kindly advice how to fix it,, thanking all of you.

Was it helpful?

Solution

Because the operating system copies the notification when scheduled, the data in the notification is not updated, therefore the application badge number doesn't change.

Maybe if you don't repeat the notification but generate your own new notification for each time interval it will give you the behavior you need. The only way I can think of generating notifications like that is to post a bunch of notifications in your scheduleNotification method, and then remember to delete the notifications when the user responds in the proper way. Since the OS only remembers the next chronologically scheduled 64 notifications, you could only schedule about an hour's worth. Since your badge number seems to be the current date, you could check the time and only bother with setting so many notifications if you're within an hour of midnight.

I don't understand what you are trying to accomplish by nagging the user so often, nor telling them the date in the badge number. Any app that bothered me so much or misused the badge number so would quickly get deleted from my iOS devices. Maybe rethinking what you are trying to accomplish may direct you to a better solution.

OTHER TIPS

I know this is already answered, but you could use NSUserDefaults as a means of caching the badge count. Then in applicationIconBadgeNumber you can just use something like this:

notif.applicationIconBadgeNumber = ([NSUserDefaults standardUserDefaults] integerForKey:@"badgeCount"] + 1);

and then you could just reset it when the user responds accordingly.

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