summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorJoseph Donofry <rubberduckie3554@gmail.com>2021-11-01 20:48:51 -0400
committerJoseph Donofry <rubberduckie3554@gmail.com>2021-11-01 20:48:51 -0400
commit912df2920e17b5506028cb007923ab8a20f8f1eb (patch)
tree24dc3917da16f43c16399375ad1ebd455ee38da4 /src
parentFix crash on logout (diff)
downloadnheko-912df2920e17b5506028cb007923ab8a20f8f1eb.tar.xz
Update macOS notifications to use UserNotifications framework
Diffstat (limited to 'src')
-rw-r--r--src/notifications/Manager.h6
-rw-r--r--src/notifications/ManagerMac.cpp18
-rw-r--r--src/notifications/ManagerMac.mm57
3 files changed, 59 insertions, 22 deletions
diff --git a/src/notifications/Manager.h b/src/notifications/Manager.h
index 4e24dd1b..da930296 100644
--- a/src/notifications/Manager.h
+++ b/src/notifications/Manager.h
@@ -77,10 +77,12 @@ private:
 private:
     // Objective-C(++) doesn't like to do lots of regular C++, so the actual notification
     // posting is split out
-    void objCxxPostNotification(const QString &title,
+    void objCxxPostNotification(const QString &room_name,
+                                const QString &room_id,
+                                const QString &event_id,
                                 const QString &subtitle,
                                 const QString &informativeText,
-                                const QImage &bodyImage);
+                                const QString &bodyImagePath);
 #endif
 
 #if defined(Q_OS_WINDOWS)
diff --git a/src/notifications/ManagerMac.cpp b/src/notifications/ManagerMac.cpp
index f69cec2c..30948dae 100644
--- a/src/notifications/ManagerMac.cpp
+++ b/src/notifications/ManagerMac.cpp
@@ -33,6 +33,9 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
       cache::displayName(QString::fromStdString(notification.room_id),
                          QString::fromStdString(mtx::accessors::sender(notification.event)));
 
+    const auto room_id  = QString::fromStdString(notification.room_id);
+    const auto event_id = QString::fromStdString(mtx::accessors::event_id(notification.event));
+
     const auto isEncrypted = std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
                                &notification.event) != nullptr;
     const auto isReply = utils::isReply(notification.event);
@@ -41,7 +44,7 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
         const QString messageInfo = (isReply ? tr("%1 replied with an encrypted message")
                                              : tr("%1 sent an encrypted message"))
                                       .arg(sender);
-        objCxxPostNotification(room_name, messageInfo, "", QImage());
+        objCxxPostNotification(room_name, room_id, event_id, messageInfo, "", "");
     } else {
         const QString messageInfo =
           (isReply ? tr("%1 replied to a message") : tr("%1 sent a message")).arg(sender);
@@ -49,12 +52,17 @@ NotificationsManager::postNotification(const mtx::responses::Notification &notif
             MxcImageProvider::download(
               QString::fromStdString(mtx::accessors::url(notification.event)).remove("mxc://"),
               QSize(200, 80),
-              [this, notification, room_name, messageInfo](QString, QSize, QImage image, QString) {
-                  objCxxPostNotification(
-                    room_name, messageInfo, formatNotification(notification), image);
+              [this, notification, room_name, room_id, event_id, messageInfo](
+                QString, QSize, QImage, QString imgPath) {
+                  objCxxPostNotification(room_name,
+                                         room_id,
+                                         event_id,
+                                         messageInfo,
+                                         formatNotification(notification),
+                                         imgPath);
               });
         else
             objCxxPostNotification(
-              room_name, messageInfo, formatNotification(notification), QImage());
+              room_name, room_id, event_id, messageInfo, formatNotification(notification), "");
     }
 }
diff --git a/src/notifications/ManagerMac.mm b/src/notifications/ManagerMac.mm
index 33b7b6af..b5ef3bfe 100644
--- a/src/notifications/ManagerMac.mm
+++ b/src/notifications/ManagerMac.mm
@@ -2,38 +2,65 @@
 
 #import <Foundation/Foundation.h>
 #import <AppKit/NSImage.h>
+#import <UserNotifications/UserNotifications.h>
 
 #include <QtMac>
 #include <QImage>
 
-@interface NSUserNotification (CFIPrivate)
-- (void)set_identityImage:(NSImage *)image;
-@end
-
 NotificationsManager::NotificationsManager(QObject *parent): QObject(parent)
 {
 
 }
 
 void
-NotificationsManager::objCxxPostNotification(const QString &title,
+NotificationsManager::objCxxPostNotification(const QString &room_name,
+                                             const QString &room_id,
+                                             const QString &event_id,
                                              const QString &subtitle,
                                              const QString &informativeText,
-                                             const QImage &bodyImage)
+                                             const QString &bodyImagePath)
 {
+    UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
+    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
+
+    [center requestAuthorizationWithOptions:options
+    completionHandler:^(BOOL granted, NSError * _Nullable error) {
+        if (!granted) {
+            NSLog(@"No notification access");
+            if (error) {
+                NSLog(@"%@",[error localizedDescription]);
+            }
+        }
+    }];
+
+    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
+
+    content.title             = room_name.toNSString();
+    content.subtitle          = subtitle.toNSString();
+    content.body              = informativeText.toNSString();
+    content.sound             = [UNNotificationSound defaultSound];
+    content.threadIdentifier  = room_id.toNSString();
 
-    NSUserNotification *notif = [[NSUserNotification alloc] init];
+    if (!bodyImagePath.isEmpty()) {
+        NSError * _Nullable error;
+        NSURL *imageURL = [NSURL URLWithString:bodyImagePath.toNSString()];
+        NSArray* attachments = [NSMutableArray array];
+        UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:imageURL options:nil error:&error];
+        if (error) {
+                NSLog(@"%@",[error localizedDescription]);
+        }
+        content.attachments = [attachments arrayByAddingObject:attachment];
+    }
 
-    notif.title           = title.toNSString();
-    notif.subtitle        = subtitle.toNSString();
-    notif.informativeText = informativeText.toNSString();
-    notif.soundName       = NSUserNotificationDefaultSoundName;
+    UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:event_id.toNSString() content:content trigger:nil];
 
-    if (!bodyImage.isNull())
-        notif.contentImage = [[NSImage alloc] initWithCGImage: bodyImage.toCGImage() size: NSZeroSize];
+    [center addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
+        if (error != nil) {
+            NSLog(@"Unable to Add Notification Request");
+        }
+    }];
 
-    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notif];
-    [notif autorelease];
+    [content autorelease];
 }
 
 //unused