Why is the local notification I created with UNUserNotificationCenter not shown in the upper-right corner of my screen?

谁说胖子不能爱 提交于 2021-02-17 02:47:26

问题


Running the code below doesn't show the notification in the upper-right corner of my screen (as a banner or alert). The notification is shown in the notification center.

I made sure "Do not disturb" is disabled on my system. I also tried both the "Banner" and "Alert" setting in System Preferences -> Notifications -> Name of my app.

Source:

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) {
    }
}

Console output:

requestNotificationAuthorization: granted=true error=nil
completion handler called

回答1:


if you want to see notification banner while app is in foreground use below method

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // Forground notifications.
        completionHandler([.alert, .sound])
    }



回答2:


The notification isn't shown when the app is running in the foreground. Relevant documentation.




回答3:


Wanna see a magic trick?

If you are working with multiple displays, unplug them, and tadaa - notifications work.

I made sure "Do not disturb" is disabled on my system.

Go to Mac System Preferences->Notifications->Do not disturb (1st item on the list)

Turns out, by default, if you are mirroring your display, the "Do not disturb" mode gets enabled and you don't gets any notifications. So uncheck it, plug your other display back in and keep coding.

This is so stupid that it's almost funny.



来源:https://stackoverflow.com/questions/60050210/why-is-the-local-notification-i-created-with-unusernotificationcenter-not-shown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!