iphone - 使用本地通知清除应用角标(Badge)

标签 iphone ios uilocalnotification

我正在尝试使用 UILocalNotification 清除我的应用程序的“未读”标记。从逻辑上讲,您会认为这可以通过将 UILocalNotification 实例的 applicationIconBadgeNumber 设置为 0 来完成。但它不起作用,applicationIconBadgeNumber 的文档说“默认值为0,表示“没有变化”。

那么一旦设置了带有本地通知的角标(Badge)真的没有办法清除吗?

更新:一些简单的代码:

-(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];
}

最佳答案

我遇到了同样的问题。从本地通知设置角标(Badge)时,将其设置为 0 是“无变化”的默认值,而直接从应用程序中设置会清除它。通过本地通知将其设置为负数解决了问题。

尝试:

clearEpisodeNotification.applicationIconBadgeNumber = -1;

关于iphone - 使用本地通知清除应用角标(Badge),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5375355/

相关文章:

iphone - 使用MailCore在我自己的应用程序中使用yahoo帐户发送电子邮件

iphone - 即使离开 View ,音频文件也会继续播放

ios - 用户离开应用后的 NSTimer

ios - 重新启动应用程序或关闭应用程序后,计划的 UILocalNotification 不会触发

ios - 暂停 UILocalNotification

iphone - 如何检测我的 iPhone 应用程序是否正在 iPad 上运行

iphone - 在 iPhone 上无需音量 slider 即可更改音量

ios - 如何在 iOS 中为 UITextField 设置方形边缘

ios - 最后一行的 UITableView indexPath

UILocalNotifications repeatInterval 使用 NSWeekdayCalendarUnit