当应用程序位于最前面时,显示NS​​UserNotification的标题 [英] Display banner for NSUserNotification while app is frontmost

查看:57
本文介绍了当应用程序位于最前面时,显示NS​​UserNotification的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用程序位于最前面时显示用户通知.我在下面找到了代码,但是我不确定如何使用委托:它似乎只是返回一个布尔值.

I want to display user notifications while the app is frontmost. I found the code below, but I'm not sure how to use the delegate: it seems to just return a boolean value.

class MyNotificationDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {

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

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

我尝试了一些句子,例如:

I have tried some sentences like:

var delegate : MyNotificationDelegate = MyNotificationDelegate()
var notification:NSUserNotification = NSUserNotification()
var notificationcenter:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()

delegate.userNotificationCenter(notificationcenter, shouldPresentNotification: notification)

但是不会显示横幅.我知道对于 NSUserNotificationCenter deliverNotification:方法是显示横幅的方法.但是我不确定 NSUserNotificationCenterDelegate 协议.

But it won't show the banner. I know that for NSUserNotificationCenter, the deliverNotification: method is the way to show the banner. But I'm not sure about the NSUserNotificationCenterDelegate protocol.

如何始终显示通知横幅?

How can I always show the notification banner?

推荐答案

您的通知中心委托和委托方法应该在AppDelegate中实现,然后才能起作用.如果您在任何其他类中实现,则它会以无提示的形式显示在通知面板中.

Your notification centre delegate and delegate method should be implemented in AppDelegate then it works. If you implement in any other class, it won't present as banner though it silently comes in notification panel.

我尝试了如下及其工作方式:

I tried as below and its working:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {



    func applicationDidFinishLaunching(aNotification: NSNotification) {
        let notification: MyNotificationDelegate = MyNotificationDelegate()
        NSUserNotificationCenter.defaultUserNotificationCenter().delegate = self;
        notification.setNotification("Hi", message: "How are you?")
    }

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

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }


}

class MyNotificationDelegate: NSObject {

    func setNotification(title: String, message: String)
    {
        let notification: NSUserNotification = NSUserNotification()
        notification.title = title
        notification.informativeText = message
        NSUserNotificationCenter.defaultUserNotificationCenter().deliverNotification(notification)
    }
}

这篇关于当应用程序位于最前面时,显示NS​​UserNotification的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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