objective-c iPhone:如何在推送通知后删除徽章?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1803344/
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-09-03 22:33:21  来源:igfitidea点击:

iPhone: how to remove badge after Push Notification?

iphoneobjective-cxcodepush

提问by obliviux

What is the code to remove the badge on my app's icon? When I receive push, I need to remove it when a button is clicked!

删除我的应用程序图标上的徽章的代码是什么?当我收到推送时,我需要在单击按钮时将其删除!

回答by Felixyz

objC:

对象

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

swift:

迅速

UIApplication.sharedApplication().applicationIconBadgeNumber = 0;

回答by Ravindra Naik

You can remove badge from push notifications by adding the following lines to your code

您可以通过在代码中添加以下几行来从推送通知中删除徽章

(void)applicationDidBecomeActive:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

回答by Hlung

As for iOS5, just setting badge number won't remove those push notification in the notification center. You have to do this...

对于iOS5,仅设置徽章编号不会删除通知中心的那些推送通知。你必须这样做...

[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

I already tested this. It looks like cancelAllLocalNotificationsmethod also works with push notifications in notification center as well.

我已经测试过了。看起来cancelAllLocalNotifications方法也适用于通知中心的推送通知。

回答by Maverick

Swift 3

斯威夫特 3

UIApplication.shared.applicationIconBadgeNumber = 0

Can be added to following methods:

可以添加到以下方法中:

optional public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool

and

optional public func applicationDidBecomeActive(_ application: UIApplication)