How to increase app icon number for repeated local notification (iPhone)

I set a local notification that repeats every minute, however I need the app icon number to increment every time. When I run it at the moment, it doesn't seem to increase, it just remains 1. Please, can someone help me?

This is how I create notifications:

// Create the UILocalNotification UILocalNotification *myNotification = [[UILocalNotification alloc] init]; myNotification.alertBody = @"Blah blah blah..."; myNotification.alertAction = @"Blah"; myNotification.soundName = UILocalNotificationDefaultSoundName; myNotification.applicationIconBadgeNumber++; myNotification.timeZone = [NSTimeZone defaultTimeZone]; myNotification.repeatInterval = NSMinuteCalendarUnit; myNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:30]; [[UIApplication sharedApplication] scheduleLocalNotification:myNotification]; 
+6
source share
7 answers

After much research, I realized that the solution is that there is no solution:

iPhone: Add app icon through local notification

It is not possible to dynamically update the icon number with local notifications while your application is in the background. You must use push notifications.

+18
source

If you are using an external service such as Parse for Push, this should be easily done. Just increase the number of parser icons when starting a local notification. Although this is a special case.

+1
source

While there is no simple applicationIconBadgeNumber++ method as mentioned in BFar, you can achieve what you request by updating all the planned iIconBadgeNumber application identifiers for UILocalNotifications when a notification is added or removed.

While this will not work, if you have notifications that use repeatInterval , while you call scheduleNotification and decrementBadgeNumber at the right time, the class below should do the trick.

 @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 
0
source

I was able to do this using the following line while the local notification schedule

 localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; 

and on the other end appdelegate

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { application.applicationIconBadgeNumber -= 1; } 
0
source

Try something like:

 int plusOne = [myNotification.applicationIconBadgeNumber intValue]; plusOne++; myNotification.applicationIconBadgeNumber = plusOne; 
-1
source

That should work.

 myNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
-1
source

Try this ... it worked for me in a simple scenario ...

 notification.applicationIconBadgeNumber = [UIApplication sharedApplication].scheduledLocalNotifications.count + 1; 

And do not forget to set the badge icon to 0 when starting the application.

-1
source

Source: https://habr.com/ru/post/908273/


All Articles