iPhone:推送通知后如何删除徽章?

什么是删除我的应用程序的图标上的徽章的代码? 当我收到推,我需要删除它,当一个button被点击!

objC

[UIApplication sharedApplication].applicationIconBadgeNumber = 0; 

迅速

 UIApplication.sharedApplication().applicationIconBadgeNumber = 0; 

至于iOS5,只需设置证件号码不会删除通知中心的推送通知。 你必须这样做…

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

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

您可以通过将以下行添加到代码中,从推送通知中删除徽章

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

Swift 3

 UIApplication.shared.applicationIconBadgeNumber = 0 

可以添加到以下方法:

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

 optional public func applicationDidBecomeActive(_ application: UIApplication)