简体   繁体   English

如何增加重复本地通知的应用程序徽章编号(iPhone)

[英]How to increment application badge number for recurring local notification (iPhone)

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? 当我运行它的时候它似乎没有增加,它只是保持1.请有人帮助我吗?

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];

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 iPhone:通过本地通知增加应用程序徽章

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. 您必须使用推送通知。

If you use an outside service such as Parse for Push, this should be easily done. 如果您使用Parse for Push等外部服务,则应该很容易完成。 Just increment Parses badge number when a local notification is fired. 只需在触发本地通知时递增Parses徽章编号。 Although, this is a special case. 虽然,这是一个特例。

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. 虽然没有简单的applicationIconBadgeNumber++方法,正如BFar所提到的,只要添加或删除通知,您就可以通过更新所有计划的UILocalNotifications的applicationIconBadgeNumbers来实现您的要求。

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. 虽然如果你有使用repeatInterval注意事项,这将不起作用,只要你在正确的时间调用scheduleNotificationdecrementBadgeNumber ,下面的类应该可以解决问题。

@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

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 并在appdelegate的另一端

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

    application.applicationIconBadgeNumber -= 1;
}

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. 并且不要忘记在应用启动时将徽章图标设置回0。

Try something like: 尝试类似的东西:

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

myNotification.applicationIconBadgeNumber = plusOne;

这应该工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM