MAC OS X UserNotification in Qt

642 Views Asked by At

I have implemented an UserNotificationAlert using NSUserNotification and NSUserNotificationCenter in qt. I have written the code to take action when the actionButton is clicked but unfortunately it is not getting executed. I have the following code.

XYZ.h

namespace ABC {
void sendUserAlert(const QString &title, const QString &subTitle, const QString &actionButtonTitle, const QString &otherButtonTitle);
}

XYZ.mm

namespace ABC {

void sendUserAlert(const QString &title, const QString &subTitle, const QString &actionButtonTitle, const QString &otherButtonTitle)
        {
            @autoreleasepool {
                NSUserNotification *notification = [[NSUserNotification alloc] init];
                NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
                [notification setTitle: title.toNSString()];
                if( !subTitle.isEmpty() ) {
                    [notification setSubtitle: subTitle.toNSString()];
                }
                if( !actionButtonTitle.isEmpty()) {
                    [notification setActionButtonTitle: actionButtonTitle.toNSString()];
                }
                if ( !otherButtonTitle.isEmpty()) {
                    [notification setOtherButtonTitle: otherButtonTitle.toNSString()];
                }
                [center deliverNotification:notification];
            }
        }
}

Delegate.mm

@interface HPObserver : NSObject<NSUserNotificationCenterDelegate>
     - (id) init;
     - (void) dealloc;
     - (void) applicationDidFinishLaunching: (NSNotification *)aNotification;
@end

@implementation HPObserver
- (id) init
{
    self = [super init];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
            selector:@selector(applicationDidFinishLaunching:)
            name: NSApplicationDidFinishLaunchingNotification
            object: NSApp];
    return self;
}

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
        @autoreleasepool {
        NSUserNotification *notification = [[aNotification userInfo] objectForKey:NSApplicationLaunchUserNotificationKey];
         if (notification.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
            NSlog(@"Hello World");  
        }
    }
}

Main.cpp

using namespace ABC;
int main(int argc, char **argv)
{
QString title = QObject::tr("Do you want to close the window");
QString actionButtonTitle = QObject::tr("Yes");
QString otherButtonTitle = QObject::tr("No");

ABC::sendUserAlert(title,subTitle,actionButtonTitle,otherButtonTitle);
}

The userNotification is displayed but when I click on the "Yes" button there is no action. I think I am missing to bridge the delegate to the main function but I am not sure how to go about it. Kindly help me to sort out this problem.

0

There are 0 best solutions below