UILocalNotification :在重复间隔期间更新 BadgeNumber
经过两天的研究,我找不到任何解决方案,好像每个人(除了我)都清楚!
我需要:
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;
}
请建议如何修复它,谢谢大家。
After goggling for 2 days i couldn't find any solution as if its clear to everyone (but me) !
I need the:
Alert.applicationIconBadgeNumber = x
to be updated in background each time the notification fires, I am repeating the notify by:
notif.repeatInterval = NSMinuteCalendarUnit
Repeating is working fine every 1 m. when the app goes in background, but the BadgeNumber dosent get updated, it takes the 1st updated date value only.
I am calling the scheduleNotification method by viewDidLoad
Here is my full code:
- (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;
}
Kindly advice how to fix it,, thanking all of you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于操作系统会按计划复制通知,因此通知中的数据不会更新,因此应用程序徽章编号不会更改。
也许如果您不重复通知,而是为每个时间间隔生成自己的新通知,它会给您提供所需的行为。我能想到生成这样的通知的唯一方法是在 ScheduleNotification 方法中发布一堆通知,然后记住在用户以正确的方式响应时删除通知。由于操作系统只记住下一个按时间顺序安排的 64 个通知,因此您只能安排大约一个小时的时间。由于您的徽章编号似乎是当前日期,因此您可以检查时间,并且只有在午夜一小时内才需要设置这么多通知。
我不明白你如此频繁地唠叨用户,也不告诉他们徽章号码中的日期,是想达到什么目的。任何让我如此困扰或滥用徽章号码的应用程序都会很快从我的 iOS 设备中删除。也许重新思考你想要完成的事情可能会引导你找到更好的解决方案。
Because the operating system copies the notification when scheduled, the data in the notification is not updated, therefore the application badge number doesn't change.
Maybe if you don't repeat the notification but generate your own new notification for each time interval it will give you the behavior you need. The only way I can think of generating notifications like that is to post a bunch of notifications in your scheduleNotification method, and then remember to delete the notifications when the user responds in the proper way. Since the OS only remembers the next chronologically scheduled 64 notifications, you could only schedule about an hour's worth. Since your badge number seems to be the current date, you could check the time and only bother with setting so many notifications if you're within an hour of midnight.
I don't understand what you are trying to accomplish by nagging the user so often, nor telling them the date in the badge number. Any app that bothered me so much or misused the badge number so would quickly get deleted from my iOS devices. Maybe rethinking what you are trying to accomplish may direct you to a better solution.
我知道这个问题已经得到解答,但您可以使用 NSUserDefaults 作为缓存徽章计数的方法。然后在
applicationIconBadgeNumber
中,您可以使用类似这样的内容:然后您可以在用户做出相应响应时重置它。
I know this is already answered, but you could use NSUserDefaults as a means of caching the badge count. Then in
applicationIconBadgeNumber
you can just use something like this:and then you could just reset it when the user responds accordingly.