2011-03-21 139 views
20

我正在尝试清除我的应用程序的“未读”徽章,其中包含UILocalNotification。从逻辑上讲,你会认为这会通过设置UILocalNotification实例applicationIconBadgeNumber 0做,但它不工作,并为applicationIconBadgeNumber文档说“默认值是0,表示”没有变化“。”清除当地通知的应用程序徽章

所以真的没有办法清除与本地通知徽章一旦它被设置

更新:一些简单的代码:

-(void)applicationDidFinishLaunching 
{ 
    // Set the appication icon badge to 1 in 10 minutes, using a local notification so it works in the background: 
    // This works fine. 

    UILocalNotification *episodeNotification = [[UILocalNotification alloc] init]; 
    episodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 10)]; 
    episodeNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    episodeNotification.applicationIconBadgeNumber = 1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:episodeNotification]; 
    [episodeNotification release]; 


    // Clear the application icon badge in 20 minutes, again using a local notifcation so it works in the background: 
    // This doesn't work. According to the docs for local notification it's not supposed to 
    // because (applicationIconBadgeNumber = 0) means "Do not change the badge" 
    // I'm looking for an alternative if it exists. 

    UILocalNotification *clearEpisodeNotification = [[UILocalNotification alloc] init]; 
    clearEpisodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 20)]; 
    clearEpisodeNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    clearEpisodeNotification.applicationIconBadgeNumber = 0; 

    [[UIApplication sharedApplication] scheduleLocalNotification:clearEpisodeNotification]; 
    [clearEpisodeNotification release]; 
} 
+0

'...但它不工作,...'会发生什么,当你尝试设置徽章为零? – Moshe 2011-03-21 13:33:15

+0

当我将徽章设置为零并且通知触发时,_nothing_发生。它继续显示与之前相同的号码。 – peterjb 2011-03-21 19:18:23

+0

我们可以看一些代码吗? – Moshe 2011-03-21 19:32:14

回答

28

我有同样的问题。从本地通知中设置徽章时,将其设置为0是“不变”的默认设置,而直接从应用程序执行则会清除它。通过本地通知将其设置为负数解决了问题。

尝试:

clearEpisodeNotification.applicationIconBadgeNumber = -1; 
+1

谢谢,这正是我需要的 – peterjb 2011-06-10 06:44:27

+0

它的工作原理谢谢 – 2014-11-27 11:26:23

+0

这两个答案都解决了这个问题。它存储?即使我们删除应用程序并重新安装,它再次出现。 – 2015-09-26 09:48:46

18

是的,这是可能的清除程序本身的徽章

我在我的一个应用程序中使用下面的代码,并且它按预期工作(即清除徽章):

//clear app badge 
[UIApplication sharedApplication].applicationIconBadgeNumber=0; 
+0

啊,我看你想做它使用本地通知为什么你不想在应用程序本身内部做它 – uvesten 2011-03-21 08:45:13

+1

它是一个应用程序,通过解析iCalendar来监视它所了解的活动事件,并根据现在正在运行的活动事件数来更新其徽章如果在用户离开应用程序时现场活动的数量下降到0,徽章应该消失 – peterjb 2011-03-21 19:16:39

相关问题