【问题标题】:Why aren't local notifications showing up on Mac?为什么本地通知不显示在 Mac 上?
【发布时间】:2020-08-21 08:51:13
【问题描述】:

我正在尝试从我的 Mac 应用程序发送本地通知(使用 Cocoa 快速编写) 到目前为止,我已经编写了这个函数

func scheduleNotification() {
        let content = UNMutableNotificationContent()
        content.title = "Test"
        content.body = "This is a test"
        content.sound = .default
        content.badge = 1
        let now = Date()
        let int = timePicker.dateValue.timeIntervalSince(now)
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: int, repeats: false)
        let request = UNNotificationRequest(identifier: "Test notification", content: content, trigger: trigger)
        center.add(request) { (err) in
            if err == nil {
                print("Success")
            }
        }

    }

但由于某种原因,它没有在通知中心显示横幅或任何内容。我的代码有问题还是其他原因导致了这种情况? (我检查了,该应用程序有权发送通知) 此外,macOS 上的等价物是什么? UIApplication.shared.applicationBadgeNumber = 0 在 iOS 上?

【问题讨论】:

    标签: swift macos cocoa unusernotificationcenter


    【解决方案1】:

    如果应用在前台,则必须使用 UNUserNotificationCenterDelegate。

    UNUserNotificationCenter.current().delegate = self
    

    首先在 viewDidLoad 中通知该委托。

    extension ViewController: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        return completionHandler([.alert, .sound, .badge])
    }
    

    }

    然后你就可以像这样使用委托了。查看此帖子的更多信息 -> Local Notifications

    【讨论】:

      【解决方案2】:

      因此,事实证明,在 MacOS Catalina 中请求许可是强制性的,这就是全部缺失。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-06
        • 2014-12-19
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多