swift - 使用 Swift 在 Mac 上显示通知横幅
问题描述
我尝试使用 Swift 在 Mac OS 上显示一些通知横幅。但我只在通知中心得到它们,而不是作为横幅。
你有想法吗?这是我的简单代码:
func showNotification() -> Void {
let notification = NSUserNotification()
notification.title = "Title of notification"
notification.subtitle = "Subtitle of notification"
notification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.deliver(notification)
}
@IBAction func btnPressed(_ sender: NSButton) {
showNotification()
testLbl.stringValue = "Button was pressed"
}
解决方案
如果您的应用程序在前台,您将不会收到横幅。
尝试使用...</p>
notification.deliveryDate = Date(timeIntervalSinceNow: 5)
NSUserNotificationCenter.default.scheduleNotification(notification)
然后切换到另一个应用程序
推荐阅读
- php - 从 json 中删除反斜杠 - WP REST API
- r - 在服务器中安排 Rscript
- reactjs - TypeError:无法使用 Axios Post 请求读取未定义的属性“_id”
- c++ - 线程安全堆栈 C++ 中的潜在死锁
- javascript - TypeScript 中的以下条件有什么区别?
- node.js - 使用 JOI 库验证电子邮件
- mysql - 从 MySQL 表中选择达到或超过指定时间的所有记录
- javascript - 如何防止使用 window.location 以角度不断刷新页面
- javascript - 在 WebGL 中的两个着色器之间使用相同的统一变量
- gnuplot - 我的脚本 gnuplot 4.6 上的开始日期和结束日期有问题