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

时间:2012-12-10 02:30:16

标签: 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];

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

  1. 虽然通知中心有多个条目,但徽章编号仍为1。
  2. 点击其中一条通知后,所有其他通知都会消失。但我没有使用cancelAllLocalNotifications方法。
  3. 我该如何解决这个问题,谢谢。

1 个答案:

答案 0 :(得分:8)

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