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

Ste*_*fan 28 iphone cocoa-touch push-notification ios4 uilocalnotification

是否可以在应用程序未运行时通过本地通知增加应用程序徽章?

我知道如何设置徽章,但没有找到任何方法来增加此值.

localNotification.applicationIconBadgeNumber = 23;

更新:我找到了一个(远非完美)解决方案.如果用户未打开应用并为每个+1事件添加通知,您可以预测会发生什么.

一个例子:

  • 第1天:计数= 0
  • 对于第2天:localNotification.applicationIconBadgeNumber = 1;
  • 对于第3天:localNotification.applicationIconBadgeNumber = 2;
  • 对于第4天:localNotification.applicationIconBadgeNumber = 3;

==>将这些通知放在一个数组中,并在应用程序退出之前设置它们.

但是,我正在寻找比此解决方案更好的解决方案.

Ron*_*ers 48

我已经找到,实施并测试了一个"解决方法"(显然)自动递增应用程序图标的徽章编号,适用于非重复的本地通知

当多个本地通知被触发时,UILocalNotifications确实不能让iOS"自动"更新/增加徽章编号,并且用户"忽略"它们或者不立即处理它们,因此它们会在通知中"堆积"中央.

此外,"为您的应用添加一些回调方法"无法处理"自动增量",因为整个通知内容都是由iOS处理的"外部"处理,您的应用甚至不需要运行.

但是有一些解决方法,这是基于我通过实验发现的知识,因为XCode文档在徽章属性上太模糊了.

  • 徽章只是一个"整数",实际上更像是在注册通知之前分配给applicationIconBadgeNumber属性的"虚拟标签".您可以给它任何值 - 当通知触发时,iOS会将该值添加到徽章,无论您在注册通知时将其设置为什么.iOS没有神奇的"自动增量"或其他操作(可能与推送通知不同,但这不是主题).iOS只从注册的通知中获取数字(整数),并将其放入徽章中.

因此,对于"解决方法",您的应用必须已为其新创建的每个通知提供正确的递增徽章编号,并在待处理通知之上进行注册.

由于您的应用程序将来无法查看,并且知道您将立即处理哪些事件,以及哪些事件将暂时"待决",因此有一些技巧要做:

当您的应用处理通知时(通过点击通知,图标,...),您必须:

  1. 获取所有待处理通知的副本
  2. '重新编号'这些待处理通知的徽章编号
  3. 删除所有待处理的通知
  4. 重新注册通知副本及其更正的徽章编号

此外,当您的应用注册新通知时,它必须首先检查有多少通知待处理,并使用以下命令注册新通知:

badgeNbr = nbrOfPendingNotifications + 1;
Run Code Online (Sandbox Code Playgroud)

看看我的代码,它会变得更加清晰.我测试了这个,它肯定有效:

在'registerLocalNotification'方法中,您应该这样做:

NSUInteger nextBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count] + 1;
localNotification.applicationIconBadgeNumber = nextBadgeNumber;
Run Code Online (Sandbox Code Playgroud)

当您处理通知(appDelegate)时,您应该调用下面的方法,该方法清除图标上的徽章并重新设置待处理通知的徽章(如果有的话)

请注意,下一个代码适用于"顺序"注册事件.如果您要在待处理事件之间"添加"事件,则必须先对这些事件进行"重新排序".我没有那么远,但我认为这是可能的.

- (void)renumberBadgesOfPendingNotifications
{
    // clear the badge on the icon
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

    // first get a copy of all pending notifications (unfortunately you cannot 'modify' a pending notification)
    NSArray *pendingNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

    // if there are any pending notifications -> adjust their badge number
    if (pendingNotifications.count != 0)
    {
        // clear all pending notifications
        [[UIApplication sharedApplication] cancelAllLocalNotifications];

        // the for loop will 'restore' the pending notifications, but with corrected badge numbers
        // note : a more advanced method could 'sort' the notifications first !!!
        NSUInteger badgeNbr = 1;

        for (UILocalNotification *notification in pendingNotifications)
        {
            // modify the badgeNumber
            notification.applicationIconBadgeNumber = badgeNbr++;

            // schedule 'again'
            [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要成为真正的"防弹",此方法应该是"原子"(内核)代码,以防止iOS在执行此方法期间触发通知.我们将不得不承担这种风险,这种情况很可能会发生.

这是我对Stackoverflow的第一个贡献,所以如果我没有遵循这里的"规则",你也可以发表评论


vis*_*kh7 13

在应用程序未运行时,您能够动态设置徽章编号的唯一方法是使用推送通知.您必须在服务器端跟踪更新.


Kri*_*nan 6

根据文档,我相信当您的应用程序未运行时,您无法增加徽章的价值.您在安排通知时设置徽章编号,因此无法增加通知.

应用程序负责管理其图标上显示的徽章编号.例如,如果文本消息传递应用程序在收到本地通知后处理所有传入消息,则应通过将UIApplication对象的applicationIconBadgeNumber属性设置为0来删除图标标记.