UILocalNotification:在repeatInterval期间更新BadgeNumber [英] UILocalNotification : updating BadgeNumber during repeatInterval

查看:46
本文介绍了UILocalNotification:在repeatInterval期间更新BadgeNumber的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过2天的搜寻后,我找不到任何解决方案,好像所有人都知道了(除了我自己)!

After goggling for 2 days i couldn't find any solution as if its clear to everyone (but me) !

我需要:

Alert.applicationIconBadgeNumber = x  

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

to be updated in background each time the notification fires, I am repeating the notify by:

notif.repeatInterval = NSMinuteCalendarUnit  

每1 m重复工作正常.当应用在后台运行但BadgeNumber剂量得到更新时,它仅采用第1个更新日期值.

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.

我正在通过viewDidLoad调用scheduleNotification方法

I am calling the scheduleNotification method by viewDidLoad

- (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.

推荐答案

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

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.

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

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.

我不明白您经常这样打na用户,或者在徽章编号中告诉他们日期,这是您想要实现的目标.任何困扰我太多或滥用证件号码的应用程序都将很快从我的iOS设备上删除.也许重新思考您要实现的目标可能会引导您找到更好的解决方案.

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.

这篇关于UILocalNotification:在repeatInterval期间更新BadgeNumber的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆