How to clear badge counter on click of app icon in iphone?
Asked Answered
S

4

9

We have developed an ios app using phonegap and have implemented push notification functionality in our app. Push notification works perfectly fine for us. We have configured push notification for both (alerts and badge) and both works fine. When we click on the alert list it redirects us to the application and clears all the notifications from the alert list and also the badge counter is set to 0.

But when we click on the application icon(badge counter) it brings app to the foreground but the badge counter and alerts are not getting cleared.

We have used following code in didFinishLaunchingWithOptions method (in appdelegate.m file)that clears out the alerts and resets the badge only on-click of alerts

 application.applicationIconBadgeNumber = 0;

can anyone provide us the solution that shows same behaviour when we click on app icon with badge counter.

Scholium answered 5/12, 2014 at 8:43 Comment(1)
#14039180 try thisCircumfluent
S
32

To clear the badge count whenever the application becomes active use delegate method. You can use UIApplicationDelegate in AppDelegate.

call the [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; in either applicationWillEnterForeground nor applicationDidBecomeActive

- (void)applicationWillEnterForeground:(UIApplication *)application

{
 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
 }

or

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

Swift

func applicationWillEnterForeground(application: UIApplication) {
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}

or

func applicationDidBecomeActive(application: UIApplication) {
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}

For Swift 3:

UIApplication.shared.applicationIconBadgeNumber = 0

iOS 13 >

func sceneDidBecomeActive(_ scene: UIScene) {
UIApplication.shared.applicationIconBadgeNumber = 0
}

enter image description here

Samirasamisen answered 5/12, 2014 at 8:49 Comment(3)
Where to call these function? No such method is present in our appdelegate file.Scholium
copy anyone of this method and paste into your AppDelegate.mSamirasamisen
Swift4: UIApplication.shared.applicationIconBadgeNumber = 0Consecution
U
6

If you are on iOS 13 supporting multiple windows, do the same on the method Scene Became active like this. This method is on SceneDelegate.

func sceneDidBecomeActive(_ scene: UIScene) {
    UIApplication.shared.applicationIconBadgeNumber = 0;
}
Unbend answered 27/11, 2019 at 12:17 Comment(1)
Doing this in SceneDelegate rather than AppDelegate is what worked for me. Thanks!Banka
A
5

In Swift the following works to put in func applicationWillEnterForeground(application: UIApplication):

UIApplication.sharedApplication().applicationIconBadgeNumber = 0
Angelika answered 12/5, 2015 at 11:38 Comment(0)
S
0

This is how I reset it and remove it. You can call this in any vc assuming it has access to the tabBarController:

func resetAndRemoveTabBarBadge() {
    
    UIApplication.shared.applicationIconBadgeNumber = 0

    if let tabItems = tabBarController?.tabBar.items {

        let item = 4 // this is whichever tabBarItem you set the badge to appear on

        let tabItem = tabItems[item]
        tabItem.badgeValue = nil
    }
}
Subalpine answered 17/2, 2021 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.