应用程序处于活动状态时,Swift NSUserNotification 不显示 [英] Swift NSUserNotification doesn't show while app is active

查看:73
本文介绍了应用程序处于活动状态时,Swift NSUserNotification 不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OSX 开发的新手,我正在制作一个应用程序,它会在发生某些事情时触发通知.但是当应用程序是关键应用程序时,它不会显示通知,因为这是默认行为.即使应用程序是关键应用程序,我也想向他们展示.但是我只找到了用objective-c编写的这个问题的解决方案,但现在我正在使用Swift.我想知道如何用 Swift 实现它.

I am new to OSX development and I am making an app which fires a notification when something happens. But it isn't showing the notification when the app is the key app, as it is the default behavior. I want to show them even when the app IS the key app. However I only found solutions to this matter that were written in objective-c but right now I am working with Swift. I was wondering how I could I implement it with Swift.

推荐答案

为了确保始终显示通知,您需要为 NSUserNotificationCenter 设置一个委托并实现 userNotificationCenter(center:shouldPresentNotification:) ->布尔.文档 说这条消息是

To ensure the notifications are always shown you'll need to set a delegate for NSUserNotificationCenter and implement userNotificationCenter(center:shouldPresentNotification:) -> Bool. The documentation says this message is

当用户通知中心决定不发送时发送给委托展示您的通知.

Sent to the delegate when the user notification center has decided not to present your notification.

您可以在您选择的任何类中实现委托.下面是一个例子:

You can implement the delegate in any class of your choosing. Here is an example:

class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self
    }

    func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool {
        return true
    }

这篇关于应用程序处于活动状态时,Swift NSUserNotification 不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆