NSUserNotificationCenter.default.deliver not work

79 Views Asked by At

I googled a lot, but found nothing. Here is my code:

a.swift

import Foundation
import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate {
    
        func showNotification() -> Void { 
                var notification = NSUserNotification()

                // All these values are optional
                notification.title = "Test of notification"
                notification.subtitle = "Subtitle of notifications"
                notification.informativeText = "Main informative text"
                //notification.contentImage = contentImage
                notification.soundName = NSUserNotificationDefaultSoundName
        
                NSUserNotificationCenter.default.deliver(notification)
        
            }
}
    
extension AppDelegate: NSUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
        return true
    }
}

var app = AppDelegate()
app.showNotification()

swift a.swift

I expected that it should pop up an notification on top right, but nothing happened.

Any help ?

MAC Version: 12.4

I ran the code, but nothing happend.

0

There are 0 best solutions below