清除带有本地通知的应用程序徽章

发布于 2024-10-24 10:22:37 字数 1686 浏览 4 评论 0原文

我正在尝试使用 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];
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-10-31 10:22:37

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

尝试:

clearEpisodeNotification.applicationIconBadgeNumber = -1;

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.

try:

clearEpisodeNotification.applicationIconBadgeNumber = -1;
走野 2024-10-31 10:22:37

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

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

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

Yes, it is possible to clear the badge from the app itself.

I use the code below in one of my apps, and it works as expected (i.e. clears the badge):

//clear app badge
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文