2

I have UILocalnotification when multiple notification is fired i want application badge number to get increment and when notification is seen i want application badge number to decrease depending upon how many notification is cancelled / watched the notification

 - (UILocalNotification *)scheduleNotification :(int)remedyID
{
        NSString *descriptionBody;
        NSInteger frequency;

        UILocalNotification *notif = [[UILocalNotification alloc] init];


        descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyTxtDic"];
        frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyFrequency"]intValue];

        NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];

        for (NSDate *fireDate in notificationFireDates)
        {
                notif.timeZone = [NSTimeZone defaultTimeZone];


                notif.repeatInterval = NSDayCalendarUnit;
                notif.alertBody = [NSString stringWithString:descriptionBody];
                notif.alertAction = @"Show me";
                notif.soundName = UILocalNotificationDefaultSoundName;

                notif.applicationIconBadgeNumber = 1;

                notif.fireDate = fireDate;


                NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody,                                         @"kRemindMeNotificationDataKey",  [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey,
                                          nil];

                notif.userInfo = userDict;

                [[UIApplication sharedApplication] scheduleLocalNotification:notif];
            }

            return notif;

}

}

- (void)cancelNotification:(int)remedyId
{


    NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    NSLog(@"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);

    for (UILocalNotification *notification in notifications)
    {

        int notifRemedyId = [[notification.userInfo objectForKey:@"kRemindMeNotificationRemedyIDKey"]intValue];

        NSLog(@"remedyID  : %d",remedyId);
        NSLog(@"notifyId : %d",notifRemedyId);
        if (remedyId == notifRemedyId)
        {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }

    }

    NSLog(@"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]);

}
raptor
  • 291
  • 1
  • 9
  • 17
  • Here's it.. [http://stackoverflow.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification](http://stackoverflow.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification) – Mr. Frank May 09 '13 at 16:02

1 Answers1

7

You can simply use these two methods,

-(void) incrementOneBadge{
    NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
    numberOfBadges +=1;

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
}

-(void) decrementOneBdge{
    NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
    numberOfBadges -=1;

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
}
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45