应用程序处于活动状态时,Swift NSUserNotification 不显示

编程入门 行业动态 更新时间:2024-10-11 13:31:06
本文介绍了应用程序处于活动状态时,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 不显示

本文发布于:2023-11-27 15:37:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   状态   NSUserNotification   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!