Ste*_*fan 28 iphone cocoa-touch push-notification ios4 uilocalnotification
是否可以在应用程序未运行时通过本地通知增加应用程序徽章?
我知道如何设置徽章,但没有找到任何方法来增加此值.
localNotification.applicationIconBadgeNumber = 23;
更新:我找到了一个(远非完美)解决方案.如果用户未打开应用并为每个+1事件添加通知,您可以预测会发生什么.
一个例子:
==>将这些通知放在一个数组中,并在应用程序退出之前设置它们.
但是,我正在寻找比此解决方案更好的解决方案.
Ron*_*ers 48
我已经找到,实施并测试了一个"解决方法"(显然)自动递增应用程序图标的徽章编号,适用于非重复的本地通知
当多个本地通知被触发时,UILocalNotifications确实不能让iOS"自动"更新/增加徽章编号,并且用户"忽略"它们或者不立即处理它们,因此它们会在通知中"堆积"中央.
此外,"为您的应用添加一些回调方法"无法处理"自动增量",因为整个通知内容都是由iOS处理的"外部"处理,您的应用甚至不需要运行.
但是有一些解决方法,这是基于我通过实验发现的知识,因为XCode文档在徽章属性上太模糊了.
因此,对于"解决方法",您的应用必须已为其新创建的每个通知提供正确的递增徽章编号,并在待处理通知之上进行注册.
由于您的应用程序将来无法查看,并且知道您将立即处理哪些事件,以及哪些事件将暂时"待决",因此有一些技巧要做:
当您的应用处理通知时(通过点击通知,图标,...),您必须:
此外,当您的应用注册新通知时,它必须首先检查有多少通知待处理,并使用以下命令注册新通知:
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的第一个贡献,所以如果我没有遵循这里的"规则",你也可以发表评论
| 归档时间: |
|
| 查看次数: |
21335 次 |
| 最近记录: |