向山狮通知中心发送通知 [英] Send notification to Mountain lion notification center

查看:111
本文介绍了向山狮通知中心发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以举例说明将测试通知从Cocoa应用程序发送到通知中心吗?例如。当我点击 NSButton

Can someone give an example of sending a test notification from a Cocoa app to Notifications Center? eg. when I click on an NSButton

推荐答案

由两个类。 NSUserNotification NSUserNotificationCenter NSUserNotification 是您的实际通知,它具有可以通过属性设置的标题,消息等。要传递您创建的通知,可以使用NSUserNotificationCenter中提供的 deliverNotification:方法。 Apple文档有关于 NSUser通知和详细信息的详细信息。 NSUserNotificationCenter ,但发布通知的基本代码类似于this:

Notifications in Mountain Lion are handled by two classes. NSUserNotification and NSUserNotificationCenter. NSUserNotification is your actual notification, it has a title, a message etc. that can be set via properties. To deliver a notification that you've created, you can use the deliverNotification: method available in NSUserNotificationCenter. The Apple docs have detailed information on NSUserNotification & NSUserNotificationCenter but the basic code to post a notification looks like this:

- (IBAction)showNotification:(id)sender{
    NSUserNotification *notification = [[NSUserNotification alloc] init];
    notification.title = @"Hello, World!";
    notification.informativeText = @"A notification";
    notification.soundName = NSUserNotificationDefaultSoundName;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

这将产生一个带有标题,消息的通知在显示时播放默认声音。还有很多,你可以做的通知,而不只是这(例如调度通知),这一切都在我链接到的文档详细。

That'll produce a notification with a title, a message and that'll play the default sound when it's displayed. There's a lot more that you can do with notifications than just this (such as scheduling notifications) and that's all detailed in the documentation I linked to.

一个小点,通知只会在您的应用程序是关键应用程序时显示。如果您希望显示通知,无论应用程序是否是密钥,您都需要为 NSUserNotificationCenter 指定一个委托,并重写委托方法 userNotificationCenter:shouldPresentNotification:,以便它返回YES。 NSUserNotificationCenterDelegate 的文档可以在此处

One small point, notifications will only be displayed when your application is the key application. If you want your notifications to display regardless of if your application is key or not, you'll need to specify a delegate for NSUserNotificationCenter and override the delegate method userNotificationCenter:shouldPresentNotification: so that it returns YES. The documentation for NSUserNotificationCenterDelegate can be found here

以下是向NSUserNotificationCenter提供委托的示例,显示,无论您的应用程序是否是密钥。在应用程序的AppDelegate.m文件中,按如下所示编辑它:

Here's an example of providing a delegate to NSUserNotificationCenter and then forcing notifications to be displayed regardless of if your application is key. In your application's AppDelegate.m file, edit it like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
}

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
    return YES;
}

在AppDelegate.h中,声明该类符合NSUserNotificationCenterDelegate协议:

And in AppDelegate.h, declare that the class conforms to the NSUserNotificationCenterDelegate protocol:

@interface AppDelegate : NSObject <NSApplicationDelegate, NSUserNotificationCenterDelegate>

这篇关于向山狮通知中心发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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