OS X: Also show desktop notifications when app is active
authorMarkus Goetz <markus@woboq.com>
Tue, 5 Jan 2016 12:32:46 +0000 (13:32 +0100)
committerMarkus Goetz <markus@woboq.com>
Tue, 5 Jan 2016 12:33:05 +0000 (13:33 +0100)
src/gui/systray.mm

index fea8db13dd26460bd82b4591fef80c1c6a08f761..ea30a38640286520b78cccaae416145e22b9d039 100644 (file)
@@ -2,6 +2,19 @@
 #include <QDebug>
 #import <Cocoa/Cocoa.h>
 
+@interface NotificationCenterDelegate : NSObject
+@end
+@implementation NotificationCenterDelegate
+// Always show, even if app is active at the moment.
+- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
+     shouldPresentNotification:(NSUserNotification *)notification
+{
+    Q_UNUSED(center);
+    Q_UNUSED(notification);
+    return YES;
+}
+@end
+
 namespace OCC {
 
 bool canOsXSendUserNotification()
@@ -15,6 +28,12 @@ void sendOsXUserNotification(const QString &title, const QString &message)
     Class cuserNotificationCenter = NSClassFromString(@"NSUserNotificationCenter");
     id userNotificationCenter = [cuserNotificationCenter defaultUserNotificationCenter];
 
+    static dispatch_once_t once;
+    dispatch_once(&once, ^{
+            id delegate = [[NotificationCenterDelegate alloc] init];
+            [userNotificationCenter setDelegate:delegate];
+    });
+
     Class cuserNotification = NSClassFromString(@"NSUserNotification");
     id notification = [[cuserNotification alloc] init];
     [notification setTitle:[NSString stringWithUTF8String:title.toUtf8().data()]];