Can someone give an example of sending a test notification from a Cocoa app to Notifications Center? eg. when I click on an NSButton
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
Notifications in Mountain Lion are handled by two classes.
NSUserNotification
andNSUserNotificationCenter
.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 thedeliverNotification:
method available in NSUserNotificationCenter. The Apple docs have detailed information on NSUserNotification & NSUserNotificationCenter but the basic code to post a notification looks like this: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.
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 methoduserNotificationCenter:shouldPresentNotification:
so that it returns YES. The documentation forNSUserNotificationCenterDelegate
can be found hereHere'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:
And in AppDelegate.h, declare that the class conforms to the NSUserNotificationCenterDelegate protocol: