Skip to content
Snippets Groups Projects
Verified Commit 912df292 authored by Joe Donofry's avatar Joe Donofry
Browse files

Update macOS notifications to use UserNotifications framework

parent 417cc071
No related branches found
No related tags found
1 merge request!13Update macOS notifications to use UserNotifications framework
Pipeline #1963 passed
......@@ -591,7 +591,7 @@ include(Translations)
set(TRANSLATION_DEPS ${LANG_QRC} ${QRC} ${QM_SRC})
if (APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation -framework Cocoa")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation -framework Cocoa -framework UserNotifications")
set(SRC_FILES ${SRC_FILES} src/notifications/ManagerMac.mm src/notifications/ManagerMac.cpp src/emoji/MacHelper.mm)
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
set_source_files_properties( src/notifications/ManagerMac.mm src/emoji/MacHelper.mm PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
......
......@@ -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)
......
......@@ -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), "");
}
}
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment