2

I am now using local notification in an application, but I find something strange.

I set up and schedule a notification like this.

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) {
    return;
}
NSDate *now = [[NSDate alloc] init];
now = [now dateByAddingTimeInterval:dayToFinish * 24 * 60 * 60];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:now];

components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now];
int month = [components month];
int day = [components day];
int year = [components year];

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setYear:year];
[dateComps setMonth:month];
[dateComps setDay:day];
[dateComps setHour:18];
[dateComps setMinute:15];
[dateComps setSecond:0];

//There are a lot to set up the fire date, you could ignore it.
NSDate *fireDate = [calendar dateFromComponents:dateComps];

localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:@"Test message %@", self.name];
localNotif.applicationIconBadgeNumber = 1;

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.name forKey:@"ListRecordName"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

This part of code can be invoked several times to schedule several local notifications and now the strange things come.

  1. The badge number remains one although there are more than one entry in the notification center.
  2. Once I click on one of the notification, all other notifications disappear. But I did not use cancelAllLocalNotifications method.

How can I fix this problem, thanks.

4

1 回答 1

8

It is not possible to update dynamically the badge number with local notifications while your app is in the background. You have to use push notifications.

于 2012-12-10T05:12:24.683 回答