如何清除 iOS 中的推送通知徽章计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14038680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:37:24  来源:igfitidea点击:

How to clear push notification badge count in iOS?

iosxcodeapple-push-notifications

提问by Deepak

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;

回答by Sumanth

You should set this:

你应该这样设置:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

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

在这些AppDelegate方法中的任何一个中,如果应用程序启动并发送到后台,那么您启动应用程序的didFinishLaunchingWithOptions方法将不会被调用,因此请使用以下任一方法:

- (void)applicationWillEnterForeground:(UIApplication *)application

- (void)applicationDidBecomeActive:(UIApplication *)application

For Swift 3+

Swift 3+

- func applicationWillEnterForeground(_ application: UIApplication)
- func applicationDidBecomeActive(_ application: UIApplication)

回答by Chanaka Caldera

in swift 3+

迅速 3+

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

在您的AppDelegate.Swift 中,当您的应用程序处于活动状态时,如下所示清除所有内容。

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

回答by Kevin

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.

好吧,更好的方法是制作一个减去徽章编号的函数,然后制作一个 UIButton 来让用户清除徽章。在默认邮件应用程序中,如果您阅读一封电子邮件,徽章将从图标中减去一封。你永远不应该在启动或恢复时将它设置为 0,它毫无意义并且会使应用程序看起来很糟糕。当用户与该事件交互时减去它是最好的方法。让你的应用更专业,如果你只是在应用启动时重置它谁知道床是什么意思,还不如不使用它。

回答by Akshay Shah

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

您可以在代码中的任何位置设置该代码.. 无所谓。但一般情况下,保存在UIApplicationDidFinishLaunching..

回答by Tejash Joshi

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