Friday, November 24, 2017

Clear notifications badge without removing notifications

Leave a Comment

In my app, I'm receiving push notifications with badge number set to one. When app will start, it should set badges count to 0, so I'm using:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 

And it works, but also it removes all notifications from notifications center.
Is there a way to clear badges without removing notifications?

3 Answers

Answers 1

According to this topic:

How to clear badge number while preserving notification center

setting:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:-1]; 

should do the trick.

Answers 2

With iOS9 directly setting badge to negative still clears notifications. You must fire empty UILocalNotification with negative badgenumber to achieve this.

let ln = UILocalNotification() ln.applicationIconBadgeNumber = -1 UIApplication.sharedApplication().presentLocalNotificationNow(ln) 

Answers 3

@feb for ios 9+ you need to set 0 not -1 for that you need to add version condition

    if #available(iOS 9.0, *) {         UIApplication.shared.applicationIconBadgeNumber = 0     }else{         UIApplication.sharedApplication().applicationIconBadgeNumber = -1     } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment