使用本地通知清除应用徽章 [英] Clear app badge with local notifications
问题描述
我正在尝试使用 UILocalNotification
清除我的应用程序的未读徽章。从逻辑上讲,你会认为这可以通过将 UILocalNotification
实例的 applicationIconBadgeNumber
设置为0来完成。但它不起作用,以及applicationIconBadgeNumber的文档说默认值为0,表示没有变化。
I'm trying to clear my app's "unread" badge with a UILocalNotification
. Logically you would think this would be done by setting applicationIconBadgeNumber
of a UILocalNotification
instance to 0. But it doesn't work, and the docs for applicationIconBadgeNumber say "The default value is 0, which means "no change.""
所以真的没办法用本地通知清除徽章一旦设置好了吗?
So is there really no way to clear a badge with local notifications once it's been set?
更新:一些简单的代码:
Update: Some simple code:
-(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是无更改的默认设置,而直接从应用程序执行此操作将清除它。通过本地通知将其设置为负数解决了问题。
I had the same problem. When setting the badge from a local notification, setting it to 0 is the default for 'no change', while doing it straight from the application would clear it. Setting it to a negative number through a local notification solved the problem.
尝试:
clearEpisodeNotification.applicationIconBadgeNumber = -1;
这篇关于使用本地通知清除应用徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!