开发者

How to increase/update badge no of localNotification

I have made a birthday r开发者_开发问答eminder application using localNotification.

It read the contacts from AddressBook & find out birthday list of that day(today).

I am repeating this app everyday using localNotification.repeatinterval(NSDayCalenderUnit).

It's working fine for me.

When I got a notification very 1st time it's badge no is 1(no problem),but if i close the notification & let the app is running in background & i got one other notification on next day.(then it's badge no should be 2).

So please help me by guiding how can I increase this badge no. count when I close the notification while my app is running in background.

I don't know when app is running in background through localNotification.repeatinterval which of the method if fired.

I would appreciate any help.


EDIT: May be there are a lot of better solutions on this case, but for my case, this works perfectly. If someone has better solution, please share it with us. EDIT.

I made this increasing of the badge number logic. In my AppDelegate.m file I wrote a method called arrangeBadgeNumbers:

-(void)arrangeBadgeNumbers{
self.notificationsArray = [NSMutableArray arrayWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]];
NSLog(@"notifications array count: %d",self.notificationsArray.count);
NSMutableArray *fireDates = [[NSMutableArray alloc]init];
for (NSInteger i=0; i<self.notificationsArray.count; i++) 
{
    UILocalNotification *notif = [self.notificationsArray objectAtIndex:i];
    NSDate *firedate = notif.fireDate;
    [fireDates addObject:firedate];
}
NSArray *sortedFireDates= [fireDates sortedArrayUsingSelector:@selector(compare:)];

for (NSInteger i=0; i<self.notificationsArray.count; i++) 
{
    UILocalNotification *notif = [self.notificationsArray objectAtIndex:i];
    notif.applicationIconBadgeNumber=[sortedFireDates indexOfObject:notif.fireDate]+1;
}
[[UIApplication sharedApplication] setScheduledLocalNotifications:self.notificationsArray];
[fireDates release];
}

I invoke this method on:

-(void)applicationWillTerminate:(UIApplication *)application; 
-(void)applicationDidEnterBackground:(UIApplication *)application;
-(void)applicationWillResignActive:(UIApplication *)application;

In the last method I also make application.applicationIconBadgeNumber = 0 To start arraigning from 0 and because in my app, when the user open the app, this mean that he has seen the notifications. So next time he closes the app, there will not be unseen local notifications.

I hope this will save some time.


There is no built in functionality in UILocalNatification for auto-incrementing of badge number. you need to provide badge number for every single notification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜