UILocalNotification : updating BadgeNumber during repeatInterval

*爱你&永不变心* 提交于 2019-12-08 12:25:05

问题


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.


回答1:


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.




回答2:


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:

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

and then you could just reset it when the user responds accordingly.



来源:https://stackoverflow.com/questions/4734464/uilocalnotification-updating-badgenumber-during-repeatinterval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!