Tuesday, November 8, 2016

How to Badge an App’s Icon in the Dock

Matthias Gansrigler:

Add this key-value pair to your Info.plist: NSUserNotificationAlertStyle with a string value of either banner (recommended by Apple) or alert. Supposedly, there’s another value, none, but that hasn’t worked for me yet – the app won’t appear in the Notifications preference pane.

Having the key-value pair in your Info.plist has no downside if you don’t use NSUserNotifications. There’s only the upside of having the user be able to disable your app’s badges if they like.

Jeff Johnson:

The documentation for NSUserNotification is sparse and implies that Notification Center handles everything automatically. The reality is that developers need to do almost everything themselves.


There is no API to determine whether the user has “Badge app icon” checked in System Preferences. I was stuck on that, but the trick here, which Rig discovered, is that when “Badge app icon” is unchecked, your app’s calls to setBadgeLabel: will not actually set the badge label. This is analogous to deliverNotification: in that you should call the method regardless, and Notification Center determines whether anything happens as a result, according to the user’s preferences.

However, you may still want your own preference for privacy.

You’ll also need to call [[NSApp dockTile] setBadgeLabel:nil] in applicationWillTerminate: otherwise the Dock badge will remain visible after the app quits. And you may want to call it in applicationDidFinishLaunching: too, in case your app crashed before it could clear the previous badge.

Comments RSS · Twitter

Leave a Comment