How to clear push notification badge count in iOS?
Asked Answered
V

7

43

I want to clear the push notification badge count once app is launched.Im not clear where to set the below code.Please give brief description about clearing the badge count.

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
Vivi answered 26/12, 2012 at 9:43 Comment(0)
S
77

You should set this:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

in either of these AppDelegate methods if the application is launched and sent to background then you launch the application didFinishLaunchingWithOptions method will not be called so use either of these methods:

- (void)applicationWillEnterForeground:(UIApplication *)application

- (void)applicationDidBecomeActive:(UIApplication *)application

For Swift 3+

- func applicationWillEnterForeground(_ application: UIApplication)
- func applicationDidBecomeActive(_ application: UIApplication)
Sulphurbottom answered 26/12, 2012 at 9:46 Comment(4)
Where you are checking in Simulator or in Device?Sulphurbottom
In both im getting the same problemVivi
If you try to get user co-ordinates in viewDidLoad() method, it will always give 0,0 result, because CLLocation is not initialized yet. So Use Delegate method to get the location details -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocationSulphurbottom
For sample to get location from iOS check this sample code developer.apple.com/library/ios/#samplecode/LocateMe/…Sulphurbottom
L
18

in swift 3+

in your AppDelegate.Swift , when your application active clear all like below.

func applicationDidBecomeActive(_ application: UIApplication) {
    UIApplication.shared.applicationIconBadgeNumber = 0
}
Laguna answered 19/4, 2017 at 4:13 Comment(0)
A
2

Well, the better way to do this is to make a function that subtract the badge number then make a UIButton to let the user to clear the badge. In the default mail application, if you read one email the badge will subtract one from the icon. You should never set it 0 at launch or resume, it is meaningless and make the app look crappy. Subtract it when the user interact with that event is the best way to do it. Make your app more professional, if you just reset it when the app launch who know what is the bedges mean, may as well not use it.

Avan answered 26/12, 2012 at 10:1 Comment(4)
I understand what you're getting at here, but I don't agree that you always want to decrement...many apps use a badge to mean "the things that have come in since you were last in the app", but they don't expect you to read all of them, since some may not be interesting to you. In that case, it's really just about clearing the badge once they enter the app. Yes, you could have code that decrements 10 times, but that would be tedious.Thermochemistry
As a user, it also annoys me when an app continues to have a badge even though there's nothing left to read. This is usually because of a bug in the app, but either way, a clear of the badge at launch can bypass this annoyance for users.Thermochemistry
That really depended on how you use the icon badge number in your app, if your user feel the need to clear app badge count. Your are doing something wrong. # If it is a to-do list app, you don't want to clear the badge count. # if you use it as a step counter, reset the counter every day. Please use common sense and there is no need to comment on a 3 years old answer.Avan
??? it's totally fine to clear the badge number on launch. "Make your app more professional, if you just reset it when the app launch who know what is the bedges mean, may as well not use it." It means "number of notifications since I last opened the app". That's easy to understandPacker
T
1

You can set that code anywhere in code.. Does not matter. But generally, is kept in UIApplicationDidFinishLaunching..

Tarantella answered 26/12, 2012 at 9:46 Comment(0)
F
0
    UIApplication.shared.applicationIconBadgeNumber = 1
    UIApplication.shared.applicationIconBadgeNumber = 0
Fang answered 25/1, 2018 at 13:4 Comment(2)
for clear your notification you have to first increase you count then zero. it is ios bugFang
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.Mcleroy
R
0

For iOS 13+

You need to implement either one of these functions in the SceneDelegate.swift file instead of the AppDelegate.swift

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


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

Think of the AppDelegate as for the App in general, whereas the SceneDelegate is for a specific instance. Better explanation here -> https://mcmap.net/q/206072/-should-my-app-be-updated-to-scene-delegate

Rellia answered 9/4, 2021 at 10:50 Comment(0)
T
0

Inside ContentView.swift, add the variable:

@Environment(\.scenePhase) var scenePhase

and then add onChange modifier to the outmost stack of your body:

 .onChange(of: scenePhase) { newPhase in
                   if newPhase == .active {
                                        print("Active")
                                  } 
                   else if newPhase == .inactive {
       UIApplication.shared.applicationIconBadgeNumber = 0                                  
                                        } 

                   else if newPhase == .background { 
  UIApplication.shared.applicationIconBadgeNumber = 0
                                        }
                                    }
Thaine answered 2/7, 2022 at 1:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.