为什么我使用 UNUserNotificationCenter 创建的本地通知没有显示在屏幕的右上角?
Why is the local notification I created with UNUserNotificationCenter not shown in the upper-right corner of my screen?
运行 下面的代码没有在我的屏幕右上角显示通知(作为横幅或警报)。通知显示在通知中心。
我确定 "Do not disturb" 在我的系统上被禁用了。我还在系统偏好设置 -> 通知 -> 我的应用程序名称中尝试了 "Banner" 和 "Alert" 设置。
来源:
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 != nil {
print("notification center error: \(String(describing: error))")
}
})
}
func applicationWillTerminate(_ aNotification: Notification) {
}
}
控制台输出:
requestNotificationAuthorization: granted=true error=nil
completion handler called
当应用 运行 在前台时,不显示通知。 Relevant documentation.
如果您想在应用程序处于前台时查看通知横幅,请使用以下方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Forground notifications.
completionHandler([.alert, .sound])
}
想看魔术吗?
如果您使用多个显示器,拔掉它们,然后 tadaa - 通知工作。
I made sure "Do not disturb" is disabled on my system.
前往 Mac System Preferences->Notifications->Do not disturb (1st item on the list)
事实证明,默认情况下,如果您正在镜像您的显示器,“请勿打扰”模式会启用并且您不会收到任何通知。所以取消选中它,重新插入您的其他显示器并继续编码。
这太愚蠢了,几乎可以笑了。
运行 下面的代码没有在我的屏幕右上角显示通知(作为横幅或警报)。通知显示在通知中心。
我确定 "Do not disturb" 在我的系统上被禁用了。我还在系统偏好设置 -> 通知 -> 我的应用程序名称中尝试了 "Banner" 和 "Alert" 设置。
来源:
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 != nil {
print("notification center error: \(String(describing: error))")
}
})
}
func applicationWillTerminate(_ aNotification: Notification) {
}
}
控制台输出:
requestNotificationAuthorization: granted=true error=nil
completion handler called
当应用 运行 在前台时,不显示通知。 Relevant documentation.
如果您想在应用程序处于前台时查看通知横幅,请使用以下方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Forground notifications.
completionHandler([.alert, .sound])
}
想看魔术吗?
如果您使用多个显示器,拔掉它们,然后 tadaa - 通知工作。
I made sure "Do not disturb" is disabled on my system.
前往 Mac System Preferences->Notifications->Do not disturb (1st item on the list)
事实证明,默认情况下,如果您正在镜像您的显示器,“请勿打扰”模式会启用并且您不会收到任何通知。所以取消选中它,重新插入您的其他显示器并继续编码。
这太愚蠢了,几乎可以笑了。