0

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.

kruztw
  • 23
  • 3
  • It would be best if you used UserNotification - NSNotification is deprecated - https://developer.apple.com/documentation/usernotifications. Also You need to setup the application's UI correctly - I suggest to follow this tutorial by apple - https://developer.apple.com/tutorials/swiftui/creating-a-macos-app – CloudBalancing Nov 22 '22 at 06:52
  • @CloudBalancing `NSUserNotification` is deprecated, `NSNotification` isn't. – Willeke Nov 22 '22 at 07:54
  • True, I wish I could edit my comment misspelling – CloudBalancing Nov 29 '22 at 17:28

0 Answers0