0

凝视了 2 天后,我找不到任何解决方案,好像每个人(但我)都清楚!

我需要:

Alert.applicationIconBadgeNumber = x  

要在每次通知触发时在后台更新,我通过以下方式重复通知:

notif.repeatInterval = NSMinuteCalendarUnit  

每 1 m 重复一次工作正常。当应用程序进入后台,但 BadgeNumber 得到更新时,它仅采用第一个更新的日期值。

我正在通过 viewDidLoad 调用 scheduleNotification 方法

这是我的完整代码:

- (void)scheduleNotification {
UILocalNotification *notif;
notif = [[[UILocalNotification alloc] init] autorelease];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.fireDate = [[NSDate date] dateByAddingTimeInterval:5];
notif.repeatInterval = NSMinuteCalendarUnit; 

NSInteger BadgeNumber = [self BadgeNumber];
NSInteger *BadgeNumberPointer = &BadgeNumber;
NSString *BadgeNumberString = [NSString stringWithFormat:@"%i", BadgeNumber];

notif.applicationIconBadgeNumber = *BadgeNumberPointer;

notif.alertBody = BadgeNumberString;
notif.alertAction = @"Hello";

[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}

-(int)BadgeNumber{
NSDate *currentDateUpdate = [[NSDate alloc] init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:@"dd"];
NSString *dateCheckUpdate = [formatter2 stringFromDate:currentDateUpdate];
NSInteger dateCheckUpdateInt = [[dateCheckUpdate substringWithRange:NSMakeRange(0, 2)] integerValue];

int BadgeNumber = dateCheckUpdateInt;
return BadgeNumber;
}

请教如何解决它,谢谢大家。

4

2 回答 2

1

由于操作系统会按计划复制通知,因此通知中的数据不会更新,因此应用程序徽章编号不会更改。

也许如果您不重复通知,而是为每个时间间隔生成自己的新通知,它将为您提供所需的行为。我能想到生成这样的通知的唯一方法是在您的 scheduleNotification 方法中发布一堆通知,然后记住在用户以正确方式响应时删除通知。由于操作系统只记住下一个按时间顺序安排的 64 条通知,因此您只能安排大约一个小时的时间。由于您的徽章编号似乎是当前日期,您可以检查时间,如果您在午夜后的一个小时内,只需设置这么多通知。

我不明白您通过如此频繁地唠叨用户或告诉他们徽章编号中的日期来试图完成什么。任何让我如此困扰或滥用徽章编号的应用程序都会很快从我的 iOS 设备中删除。也许重新考虑您要完成的工作可能会引导您找到更好的解决方案。

于 2011-01-19T15:18:26.887 回答
1

我知道这已经得到解答,但是您可以使用 NSUserDefaults 作为缓存徽章计数的一种方式。然后applicationIconBadgeNumber你可以使用这样的东西:

notif.applicationIconBadgeNumber = ([NSUserDefaults standardUserDefaults] integerForKey:@"badgeCount"] + 1);

然后您可以在用户相应响应时重置它。

于 2011-05-13T14:15:17.113 回答