Home > Mobile >  How I can add a badge on my Dock icon using Cocoa?
How I can add a badge on my Dock icon using Cocoa?

Time:09-18

I can't add a badge to my cocoa App icon on macOs BigSur. I'm trying to update icon's badge in Dock with this code:

NSApp.dockTile.showsApplicationBadge = true    
NSApp.dockTile.badgeLabel = "12"
NSApp.dockTile.display()

But nothing happens. And I received a message in Xcode console:

setShowsApplicationBadge: is not yet implemented for the NSApp dockTile

Anybody can help with this?

CodePudding user response:

Problem was resolved by this piece of code in AppDelegate

if #available(OSX 10.14, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { success, error in
                if success {
                    print("success")
                } else if let error = error {
                    print(error.localizedDescription)
                }
            }
        }

After this application was added to macOS settings -> Notifications and badge appeared in the Dock. Strange situation, because on new fresh project it's just works with

NSApp.dockTile.badgeLabel = "12"

Maybe it will help somebody else.

  • Related