收到推送通知时清除徽章

发布于 2024-11-03 19:29:41 字数 66 浏览 1 评论 0原文

当我收到推送通知时,如何清除应用程序图标上出现的徽章?我想在用户点击推送通知警报的“查看”或点击应用程序图标后清除它。

How can I clear the badge which appears on application icon when I receive Push Notification? I want to clear it once user has either tapped on "View" of Push notification alert or has tapped on the app icon.

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

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

发布评论

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

评论(4

一百个冬季 2024-11-10 19:29:41

我怀疑你在谈论 SpringBoard 的徽章:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]

I suspect you are talking about the SpringBoard's badge:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]
作妖 2024-11-10 19:29:41

Badge count set Zero

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]

使用此代码取消所有本地通知:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

使用此行代码取消一个本地通知:

[[UIApplication sharedApplication] cancelLocalNotification:theNotification];

这里 theNotification 是一个 UILocalNotification 对象,因此为了取消特定通知,您需要保留它的 UILocalNotification。

检查

Badge count set Zero

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]

Cancel all local notifications with this code:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

Cancel one local notification with this line of code:

[[UIApplication sharedApplication] cancelLocalNotification:theNotification];

here theNotification is a UILocalNotification object, so in order to cancel a specific notification, you need to hold on to it's UILocalNotification.

Check this.

乖不如嘢 2024-11-10 19:29:41

对于 Mac OS X Lion,它是:

    [NSApp dockTile].badgeLabel = @"";

(Lion 支持徽章类型推送通知。)

For Mac OS X Lion, it's:

    [NSApp dockTile].badgeLabel = @"";

(Lion supports badge-type push notifications.)

柳絮泡泡 2024-11-10 19:29:41

根据 Apple 文档,将 application.applicationIconBadgeNumber 设置为您想要在徽章上显示的数字。如果设置为0,则会被清除。

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotif) {
        NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
        [viewController displayItem:itemName];  // custom method
        application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
    }

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    return YES;
}

参考 - 向下滚动到处理清单 2.4 上方的本地和远程通知部分

From Apple's documentation, set the application.applicationIconBadgeNumber to the number you want displayed on the badge. If you set it to 0, it will be cleared.

- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotif) {
        NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
        [viewController displayItem:itemName];  // custom method
        application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
    }

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
    return YES;
}

Reference - Scroll down to the Handling Local and Remote Notifications section just above listing 2.4

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