清除当地通知的应用程序徽章
我正在尝试清除我的应用程序的“未读”徽章,其中包含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是“不变”的默认设置,而直接从应用程序执行则会清除它。通过本地通知将其设置为负数解决了问题。
尝试:
clearEpisodeNotification.applicationIconBadgeNumber = -1;
谢谢,这正是我需要的 – peterjb 2011-06-10 06:44:27
它的工作原理谢谢 – 2014-11-27 11:26:23
这两个答案都解决了这个问题。它存储?即使我们删除应用程序并重新安装,它再次出现。 – 2015-09-26 09:48:46
是的,这是可能的清除程序本身的徽章
我在我的一个应用程序中使用下面的代码,并且它按预期工作(即清除徽章):
//clear app badge
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
'...但它不工作,...'会发生什么,当你尝试设置徽章为零? – Moshe 2011-03-21 13:33:15
当我将徽章设置为零并且通知触发时,_nothing_发生。它继续显示与之前相同的号码。 – peterjb 2011-03-21 19:18:23
我们可以看一些代码吗? – Moshe 2011-03-21 19:32:14