运行下面的代码不会在我的屏幕右上角显示通知(作为横幅或警报)。通知显示在通知中心。
我确保在我的系统上禁用了“请勿打扰”。我还尝试了“系统偏好设置”->“通知”->“我的应用程序名称”中的“横幅”和“警报”设置。
来源:
import Cocoa
import UserNotifications
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
print("requestNotificationAuthorization: granted=\(granted) error=\(String(describing: error))")
}
let content = UNMutableNotificationContent()
content.title = "bar"
content.body = "foo"
content.categoryIdentifier = "alarm"
let uuidString = UUID().uuidString
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)
notificationCenter.add(request, withCompletionHandler: { error in
print("completion handler called")
if error …Run Code Online (Sandbox Code Playgroud)