有关iOS上的本地通知的问题

sea*_*iao 2 objective-c ios uilocalnotification

我现在在应用程序中使用本地通知,但我发现一些奇怪的东西.

我设置并安排了这样的通知.

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];
Run Code Online (Sandbox Code Playgroud)

可以多次调用这部分代码来安排几个本地通知,现在奇怪的事情就来了.

  1. 虽然通知中心有多个条目,但徽章编号仍然是一个.
  2. 点击其中一个通知后,所有其他通知都会消失.但我没有使用cancelAllLocalNotifications方法.

我该如何解决这个问题,谢谢.

Abi*_*Abi 8

当您的应用在后台时,无法使用本地通知动态更新徽章编号.您必须使用推送通知.