Try to use UserNotifications.

This commit is contained in:
Black Hat
2019-08-14 22:50:01 +08:00
parent 4f3b230807
commit 0067ce7e2e
2 changed files with 35 additions and 20 deletions

View File

@@ -46,9 +46,6 @@ find_package(Qt5 5.12 REQUIRED Widgets Network Quick Qml Gui Svg Multimedia)
if(LINUX)
find_package(Qt5DBus REQUIRED)
endif(LINUX)
if (APPLE)
find_package(Qt5MacExtras REQUIRED)
endif(APPLE)
# Qt5_Prefix is only used to show Qt path in message()
# Qt5_BinDir is where all the binary tools for Qt are
if (QT_QMAKE_EXECUTABLE)
@@ -212,9 +209,7 @@ target_link_libraries(${PROJECT_NAME}
target_compile_definitions(${PROJECT_NAME} PRIVATE
GIT_SHA1="${GIT_SHA1}" LIB_GIT_SHA1="${LIB_GIT_SHA1}")
if (APPLE)
target_link_libraries(${PROJECT_NAME} Qt5::MacExtras)
elseif(LINUX)
if(LINUX)
target_link_libraries(${PROJECT_NAME} Qt5::DBus)
endif()

View File

@@ -1,13 +1,12 @@
#include "manager.h"
#include <Foundation/Foundation.h>
#include <QtMac>
#import <UserNotifications/UserNotifications.h>
@interface NSUserNotification (CFIPrivate)
- (void)set_identityImage:(NSImage*)image;
@end
#include <QApplication>
NotificationsManager::NotificationsManager(QObject* parent) : QObject(parent) {}
NotificationsManager::NotificationsManager(QObject* parent) : QObject(parent) {
}
void NotificationsManager::postNotification(const QString& roomId,
const QString& eventId,
@@ -19,16 +18,37 @@ void NotificationsManager::postNotification(const QString& roomId,
Q_UNUSED(eventId);
Q_UNUSED(icon);
NSUserNotification* notif = [[NSUserNotification alloc] init];
UNUserNotificationCenter* center =
[UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
notif.title = roomName.toNSString();
notif.subtitle = QString("%1 sent a message").arg(senderName).toNSString();
notif.informativeText = text.toNSString();
notif.soundName = NSUserNotificationDefaultSoundName;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
}];
[[NSUserNotificationCenter defaultUserNotificationCenter]
deliverNotification:notif];
[notif autorelease];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:300
repeats:NO];
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