Show room avatar in notification.

This commit is contained in:
Black Hat
2019-08-16 13:03:46 +08:00
parent 086891ef4e
commit 7d3aa52b82
2 changed files with 17 additions and 36 deletions

View File

@@ -29,5 +29,7 @@
<string>NSApplication</string> <string>NSApplication</string>
<key>NSHighResolutionCapable</key> <key>NSHighResolutionCapable</key>
<string>True</string> <string>True</string>
<key>NSUserNotificationAlertStyle</key>
<string>alert</string>
</dict> </dict>
</plist> </plist>

View File

@@ -1,8 +1,12 @@
#include "manager.h" #include "manager.h"
#import <UserNotifications/UserNotifications.h> #include <Foundation/Foundation.h>
#include <QtMac>
#include <QPixmap>
#include <QApplication> @interface NSUserNotification (CFIPrivate)
- (void)set_identityImage:(NSImage*)image;
@end
NotificationsManager::NotificationsManager(QObject* parent) : QObject(parent) {} NotificationsManager::NotificationsManager(QObject* parent) : QObject(parent) {}
@@ -16,42 +20,17 @@ void NotificationsManager::postNotification(const QString& roomId,
Q_UNUSED(eventId); Q_UNUSED(eventId);
Q_UNUSED(icon); Q_UNUSED(icon);
UNUserNotificationCenter* center = NSUserNotification* notif = [[NSUserNotification alloc] init];
[UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options =
UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:options notif.title = roomName.toNSString();
completionHandler:^(BOOL granted, notif.subtitle = QString("%1 sent a message").arg(senderName).toNSString();
NSError* _Nullable error) { notif.informativeText = text.toNSString();
if (!granted) { notif.soundName = NSUserNotificationDefaultSoundName;
NSLog(@"Something went wrong"); notif.contentImage = QtMac::toNSImage(QPixmap::fromImage(icon));
}
}];
UNTimeIntervalNotificationTrigger* trigger = [[NSUserNotificationCenter defaultUserNotificationCenter]
[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0 repeats:NO]; deliverNotification:notif];
[notif autorelease];
UNMutableNotificationContent* content = [UNMutableNotificationContent new];
content.title = roomName.toNSString();
content.subtitle = QString("%1 sent a message").arg(senderName).toNSString();
content.body = text.toNSString();
content.sound = [UNNotificationSound defaultSound];
NSString* identifier = QApplication::applicationName().toNSString();
UNNotificationRequest* request =
[UNNotificationRequest requestWithIdentifier:identifier
content:content
trigger:trigger];
[center addNotificationRequest:request
withCompletionHandler:^(NSError* _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@", error);
}
}];
} }
// unused // unused