How to clear push notification badge count in iOS?

大憨熊 提交于 2020-01-09 13:12:41

问题


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;

回答1:


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)



回答2:


in swift 3+

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

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



回答3:


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.




回答4:


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




回答5:


    UIApplication.shared.applicationIconBadgeNumber = 1
    UIApplication.shared.applicationIconBadgeNumber = 0


来源:https://stackoverflow.com/questions/14038680/how-to-clear-push-notification-badge-count-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!