1

I am trying to update the badge count of application when app is closed, can any one tell, can we update it if app is closed? I am using the following code:

NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponent = [gregCalendar components:NSCalendarUnitYear  | NSCalendarUnitWeekOfYear fromDate:[NSDate date]];

    NSDateFormatter *outputFormatterHour = [[NSDateFormatter alloc] init];
    [outputFormatterHour setDateFormat:@"HH"];
    NSDateFormatter *outputFormatterMin = [[NSDateFormatter alloc] init];
    [outputFormatterMin setDateFormat:@"mm"];
    int hour = [[outputFormatterHour stringFromDate:_selectTimePicker.date] intValue];
    int min = [[outputFormatterMin stringFromDate:_selectTimePicker.date] intValue];

    [dateComponent setHour:hour];
    [dateComponent setMinute:min];

    NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];

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

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    [localNotification setAlertTitle:@"Notification"];
    [localNotification setAlertBody:@"Body" ];
    [localNotification setAlertAction:@"Open App"];
    [localNotification setHasAction:YES];

    [localNotification setAlertLaunchImage:@"AppIcon"];

    localNotification.applicationIconBadgeNumber++;
    [localNotification setFireDate:fireDate];
    localNotification.repeatInterval = NSCalendarUnitDay;
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Muhammad Umair
  • 583
  • 11
  • 32
  • 1
    What exactly is the problem? If the notification is displayed, the badge will be set. – Avi Apr 05 '16 at 13:05
  • @Avi badge is not updating. For example, I want to send local notification daily at 5 pm, User didn't open the app for 3 days now the count of my badge should be 3 but it remains 1. – Muhammad Umair Apr 05 '16 at 13:09
  • @Avi can we update the local notification badge count, if app is in background or closed ? Although, we have scheduled the notification. – Muhammad Umair Apr 05 '16 at 13:12
  • 2
    http://stackoverflow.com/questions/8734078/ios-badge-number-live-update check this one – Muhammad Salman Apr 05 '16 at 13:13
  • @salmancs43 Thanks for this answer, but still my badge count is inconsistent. I want to update it on daily basis, if user doesn't open the app – Muhammad Umair Apr 05 '16 at 13:19
  • 2
    The only way you're going to be able to dynamically set the badge number when your application isn't running is with push notifications. You'll have to track the updates on the server side. – Muhammad Salman Apr 05 '16 at 13:33
  • 2
    http://stackoverflow.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification also check its third comment I hope you will find your solution. – Muhammad Salman Apr 05 '16 at 13:34
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108283/discussion-between-salmancs43-and-muhammad-umair). – Muhammad Salman Apr 05 '16 at 13:41

0 Answers0