收到推送通知时清除徽章

时间:2022-08-26 22:00:17

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.

当我收到推送通知时,如何清除应用程序图标上显示的徽章?一旦用户点击“推送通知提醒”的“查看”或点击了应用图标,我想清除它。

4 个解决方案

#1


22  

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

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

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]

#2


4  

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.

这里的Notification是一个UILocalNotification对象,所以为了取消特定的通知,你需要坚持它的UILocalNotification。

Check this.

#3


1  

For Mac OS X Lion, it's:

对于Mac OS X Lion,它是:

    [NSApp dockTile].badgeLabel = @"";

(Lion supports badge-type push notifications.)

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

#4


0  

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.

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

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

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

#1


22  

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

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

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]

#2


4  

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.

这里的Notification是一个UILocalNotification对象,所以为了取消特定的通知,你需要坚持它的UILocalNotification。

Check this.

#3


1  

For Mac OS X Lion, it's:

对于Mac OS X Lion,它是:

    [NSApp dockTile].badgeLabel = @"";

(Lion supports badge-type push notifications.)

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

#4


0  

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.

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

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

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