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

}
1

1 Answer 1

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];
}
12
  • numberOfBadges -=1; this is used to decrement applicationiconbadgenumber right ?
    – raptor
    May 9, 2013 at 16:07
  • should i use this method in my MainViewController or should it be also present in appdelegate
    – raptor
    May 9, 2013 at 16:09
  • i'll let u know tomo ...btw when there are multiple notification the badge number will get increment automatically
    – raptor
    May 9, 2013 at 17:11
  • you can just set the number depending on your needs, [[UIApplication sharedApplication] setApplicationIconBadgeNumber:4]; or any number May 9, 2013 at 17:13
  • if i set 4 ...and say 3 notifications are fired ..does it show 3 applicationbadgenumber ? ..is setApplicationBadgeNumber : 4 means max of 4 will be shown ?
    – raptor
    May 9, 2013 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.