How to increment application badge number for recu

2019-02-05 03:26发布

I've setup a local notification that repeats every minute, however I need the application badge number to increment each time. When I run it at the moment it doesn't seem to increase, it just stays a 1. Please can someone help me out?

Here is how I create the 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];

7条回答
smile是对你的礼貌
2楼-- · 2019-02-05 04:07

While there's no simple applicationIconBadgeNumber++ method, as BFar mentioned, you can achieve what you're asking by updating all of the scheduled UILocalNotifications' applicationIconBadgeNumbers whenever a notification is added or removed.

While this won't work if you have notices that use repeatInterval, as long as you call scheduleNotification and decrementBadgeNumber at the right times, 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
查看更多
疯言疯语
3楼-- · 2019-02-05 04:11

Try something like:

int plusOne = [myNotification.applicationIconBadgeNumber intValue];
plusOne++;

myNotification.applicationIconBadgeNumber = plusOne;
查看更多
一夜七次
4楼-- · 2019-02-05 04:14

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

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

And don't forget to set badge icon back to 0 when app launch.

查看更多
We Are One
5楼-- · 2019-02-05 04:19

If you use an outside service such as Parse for Push, this should be easily done. Just increment Parses badge number when a local notification is fired. Although, this is a special case.

查看更多
做自己的国王
6楼-- · 2019-02-05 04:21

After doing lot's of research I figured out the solution is that there is no solution:

iPhone: Incrementing the application badge through a local notification

It is not possible to update dynamically the badge number with local notifications while your app is in the background. You have to use push notifications.

查看更多
Explosion°爆炸
7楼-- · 2019-02-05 04:27

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

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

and on the other end in the appdelegate

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    application.applicationIconBadgeNumber -= 1;
}
查看更多
登录 后发表回答