summary refs log tree commit diff
path: root/src/notifications/ManagerMac.mm
blob: 8669432b6b40f05c2b3bc0bdc99065078479d965 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "notifications/Manager.h"

#import <Foundation/Foundation.h>
#import <AppKit/NSImage.h>
#import <UserNotifications/UserNotifications.h>

#include <QtMac>
#include <QImage>

@interface UNNotificationAttachment (UNNotificationAttachmentAdditions)
    + (UNNotificationAttachment *) createFromImageData:(NSData*)imgData identifier:(NSString *)imageFileIdentifier options:(NSDictionary*)attachmentOptions;
@end

@implementation UNNotificationAttachment (UNNotificationAttachmentAdditions)
    + (UNNotificationAttachment *) createFromImageData:(NSData*)imgData identifier:(NSString *)imageFileIdentifier options:(NSDictionary*)attachmentOptions {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *tmpSubFolderName = [[NSProcessInfo processInfo] globallyUniqueString];
        NSURL *tmpSubFolderURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:tmpSubFolderName] isDirectory:true];
        NSError *error = nil;
        [fileManager createDirectoryAtURL:tmpSubFolderURL withIntermediateDirectories:true attributes:nil error:&error];
        if(error) {
            NSLog(@"%@",[error localizedDescription]);
            return nil;
        }
        NSURL *fileURL = [tmpSubFolderURL URLByAppendingPathComponent:imageFileIdentifier];
        [imgData writeToURL:fileURL atomically:true];
        UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:fileURL options:attachmentOptions error:&error];
        if(error) {
            NSLog(@"%@",[error localizedDescription]);
            return nil;
        }
        return imageAttachment;

    }
@end

NotificationsManager::NotificationsManager(QObject *parent): QObject(parent)
{

}

void
NotificationsManager::objCxxPostNotification(const QString &room_name,
                                             const QString &room_id,
                                             const QString &event_id,
                                             const QString &subtitle,
                                             const QString &informativeText,
                                             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();

    if (!bodyImagePath.isEmpty()) {
        NSURL *imageURL = [NSURL fileURLWithPath:bodyImagePath.toNSString()];
        NSData *img = [NSData dataWithContentsOfURL:imageURL];
        NSArray *attachments = [NSMutableArray array];
        UNNotificationAttachment *attachment = [UNNotificationAttachment createFromImageData:img identifier:@"attachment_image.jpeg" options:nil];
        if (attachment) {
            attachments = [NSMutableArray arrayWithObjects: attachment, nil];
            content.attachments = attachments;
        }
    }

    UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:event_id.toNSString() content:content trigger:nil];

    [center addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Unable to Add Notification Request");
        }
    }];

    [content autorelease];
}

//unused
void
NotificationsManager::actionInvoked(uint, QString)
{
}

void
NotificationsManager::notificationReplied(uint, QString)
{
}

void
NotificationsManager::notificationClosed(uint, uint)
{
}

void
NotificationsManager::removeNotification(const QString &, const QString &)
{}