简体   繁体   中英

iPhone: how to remove badge after Push Notification?

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!

objC :

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

swift :

UIApplication.sharedApplication().applicationIconBadgeNumber = 0;

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;
}

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

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

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

Swift 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM