雖然沒有簡單applicationIconBadgeNumber++
方法,如BFAR提到的,你可以實現你的時候通知添加或刪除更新所有預定UILocalNotifications' applicationIconBadgeNumbers的問。
雖然如果你有使用repeatInterval
,只要您撥打正確的時間scheduleNotification
和decrementBadgeNumber
,類下面應該做的伎倆聲明這是不行的。
@implementation NotificationScheduler
+ (void) scheduleNotification:(NSString*)message date:(NSDate*)date {
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification) {
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = message;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = [self getExpectedApplicationIconBadgeNumber:date];
[app scheduleLocalNotification:notification];
[self updateBadgeCountsForQueuedNotifiations];
}
}
+ (void) decrementBadgeNumber:(long)amount {
[self setCurrentBadgeNumber:([self getCurrentBadgeNumber] - amount)];
[self updateBadgeCountsForQueuedNotifiations];
}
+ (long) getExpectedApplicationIconBadgeNumber:(NSDate*)notificationDate {
long number = [self getCurrentBadgeNumber];
for (UILocalNotification *notice in [self getScheduledLocalNotifications]) {
if (notice.fireDate <= notificationDate) {
number++;
}
}
return number;
}
+ (void) updateBadgeCountsForScheduledNotifiations {
long expectedBadgeNumber = [self getCurrentBadgeNumber];
NSArray *allLocalNotifications = [self getScheduledLocalNotifications];
for (UILocalNotification *notice in allLocalNotifications) {
expectedBadgeNumber++;
notice.applicationIconBadgeNumber = expectedBadgeNumber;
}
[[UIApplication sharedApplication] setScheduledLocalNotifications:allLocalNotifications];
}
+ (long) getCurrentBadgeNumber {
return [UIApplication sharedApplication].applicationIconBadgeNumber;
}
+ (void) setCurrentBadgeNumber:(long)number {
[UIApplication sharedApplication].applicationIconBadgeNumber = number;
}
+ (NSArray*) getScheduledLocalNotifications {
NSSortDescriptor * fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES];
return [[[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[fireDateDesc]];
}
@end
你可以張貼一些代碼的另一端? – HeikoG 2012-02-12 17:49:35
@HeikoG我添加了用於創建通知的代碼。 – 2012-02-12 18:01:13
@TheCrazyChimp你有沒有找到解決這個問題的解決方案? – 2012-05-11 15:42:29