【问题标题】:How to increment application badge number for recurring local notification (iPhone)如何增加应用程序徽章编号以进行定期本地通知(iPhone)
【发布时间】:2012-02-12 17:45:35
【问题描述】:

我设置了每分钟重复一次的本地通知,但是我需要应用程序徽章编号每次递增。当我现在运行它时,它似乎没有增加,它只是保持 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];

【问题讨论】:

  • 你能贴一些代码吗?
  • @HeikoG 我已经添加了用于创建通知的代码。
  • @TheCrazyChimp 你有没有找到解决这个问题的方法?

标签: iphone uilocalnotification


【解决方案1】:

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

iPhone: Incrementing the application badge through a local notification

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

【讨论】:

  • 虽然这个答案在技术上是正确的,但您实际上可以使用共享的 UIApplication 实例通过一些额外的工作来完成 The Crazy Chimp 想要做的事情。每次您安排新的本地通知或应用程序加载时,您都可以使用“scheduledLocalNotifications”属性和“cancelAllLocalNotifications”属性取消所有未来的通知,并通过按时间顺序枚举它们并递增将徽章计数重新分配给未来的本地通知您分配的徽章编号。 Def 不像输入 applicationBadgeNumber++ 那样简单,但它可以工作。
  • +1 对此用户的评论。我测试了这种方法并且它有效。这里有一个更完整的实现解释:stackoverflow.com/a/15461328/2546416
  • 是的,但是如果您使用 repeatInterval,这将不起作用.. 如果您的应用程序永远不会再次启动并且您无法预测徽章值(因为您每天都有重复间隔) 那么你不能增加...
【解决方案2】:

如果您使用 Parse for Push 等外部服务,这应该很容易完成。只需在触发本地通知时增加 Parses 徽章编号。不过,这是特例。

【讨论】:

    【解决方案3】:

    虽然没有简单的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
    

    【讨论】:

    • 它缺少 updateBadgeCountsForQueuedNotifiations 方法实现...请更新它...
    【解决方案4】:

    我可以在安排本地通知时使用以下行来做到这一点

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

    在 appdelegate 的另一端

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

    【讨论】:

      【解决方案5】:

      尝试类似:

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

      【讨论】:

      • 不幸的是,这对我不起作用 - 不过感谢您的建议。
      【解决方案6】:

      这应该可行。

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

      【讨论】:

        【解决方案7】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2011-08-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多