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

发布于 2025-01-05 00:03:20 字数 691 浏览 0 评论 0原文

我已经设置了每分钟重复一次的本地通知,但是我需要每次都增加应用程序徽章编号。当我现在运行它时,它似乎没有增加,只是保持为 1。请问有人可以帮助我吗?

以下是我创建通知的方法:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

大姐,你呐 2025-01-12 00:03:20

经过大量研究后,我发现解决方案是没有解决方案:

iPhone:通过本地通知增加应用程序徽章

当您的应用程序处于后台时,无法使用本地通知动态更新徽章编号。您必须使用推送通知。

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.

维持三分热 2025-01-12 00:03:20

如果您使用外部服务(例如 Parse for Push),这应该很容易完成。只需在触发本地通知时增加解析徽章编号即可。虽然,这是一个特殊情况。

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.

茶色山野 2025-01-12 00:03:20

虽然没有简单的 applicationIconBadgeNumber++ 方法,但正如 BFar 提到的,您可以通过在添加或删除通知时更新所有计划的 UILocalNotifications 的 applicationIconBadgeNumbers 来实现您的要求。

虽然如果您有使用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

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
梦情居士 2025-01-12 00:03:20

我能够使用以下行来完成此操作,同时安排本地通知

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

并在应用程序委托的另一端

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

    application.applicationIconBadgeNumber -= 1;
}

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;
}
一瞬间的火花 2025-01-12 00:03:20

尝试类似的方法:

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

myNotification.applicationIconBadgeNumber = plusOne;

Try something like:

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

myNotification.applicationIconBadgeNumber = plusOne;
少女的英雄梦 2025-01-12 00:03:20

这应该有效。

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

This should work.

myNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
叹沉浮 2025-01-12 00:03:20

试试这个...它在简单的场景中对我有用...

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

并且不要忘记在应用程序启动时将徽章图标设置回 0。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文