I'm developing a daemon with Qt and am wanting to send messages to the Mountain Lion notification centre: -

Qt seems to lack support for this, which isn't surprising as it's not cross-platform.
If I were using Cocoa, it looks like I would use the NSUserNotificationCenter class with an NSUserNotification.
Up until now, whenever I've seen a Foundation class, there has always been a matching Core Foundation class, but it appears that there is no CFUserNotificationCenter, but there is a CFUserNotification.
So, am I right that NSNotificationCenter is what Cocoa uses for this and is it possible to send messages to the Notification Centre without Cocoa?
If it is possible, please can someone post a code example in C or C++, or at least point out which functions to use?
You can easily integrate Qt/C++ code with Cocoa/Objective-C. XCode supports Objective-C++ which is a mix of C++ and Objective-C. These files will have a .mm extension and are added to your QMake project using the OBJECTIVE_SOURCES variable (which is unfortunately not in the official QMake documentation). So just add the NSUserNotification code right into a C++ class.
QMake Project File
HEADERS += Foo.h
OBJECTIVE_SOURCES += Foo.mm
LIBS += -framework Foundation
INCLUDEPATH += /System/Library/Frameworks/Foundation.framework/Versions/C/Headers
Foo.h
#ifndef FOO_H
#define FOO_H
class Foo {
public:
void test();
};
#endif
Foo.mm
#include "foo.h"
#include <NSUserNotification.h>
void Foo::test() {
NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
userNotification.title = @"My Great Title";
userNotification.informativeText = @"Blah Blah Blah";
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With