无法在macOS上显示简单通知

时间:2019-02-05 19:45:01

标签: swift macos

Im是Swift的新功能。我试图创建一个控制台应用程序/实用程序,以在macOS上创建一个通知弹出窗口。没有,我似乎找不到任何方法。看来这应该很简单,但是我无法使通知弹出窗口显示:(

我在macOS Sierra上使用swift 4.0.3

任何指导,以使这项工作达到最基本的水平将不胜感激!谢谢!

import Foundation
import Cocoa

let notification = NSUserNotification()
notification.title = "Title of notification"
notification.subtitle = "Subtitle of notification"
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.deliver(notification)

1 个答案:

答案 0 :(得分:0)

仅当您的应用程序没有重点关注时,您的通知才会发送。

如果仍然要交付它,则需要实现try { Socket socket = new Socket((host,port)); // when click a button, it sends an image ... bufferedReader.readLine; } catch (Exception e) {e.printStackTrace;} // and some other codes 作为协议和以下功能:

NSUserNotificationCenterDelegate

PS。我很确定,不是全部都需要-但是我不知道是哪个