使用本地通知清除应用徽章

pet*_*rjb 21 iphone ios uilocalnotification

我试图用一个清除我的应用程序的"未读"徽章UILocalNotification.从逻辑上讲,你会认为这会通过设置来完成 applicationIconBadgeNumber一个的UILocalNotification情况下为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];
}
Run Code Online (Sandbox Code Playgroud)

小智 28

我有同样的问题.从本地通知设置徽章时,将其设置为0是"无更改"的默认设置,而直接从应用程序执行此操作将清除它.通过本地通知将其设置为负数解决了问题.

尝试:

clearEpisodeNotification.applicationIconBadgeNumber = -1;
Run Code Online (Sandbox Code Playgroud)


uve*_*ten 19

是的,可以从应用程序本身清除徽章.

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

//clear app badge
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
Run Code Online (Sandbox Code Playgroud)