-
Notifications
You must be signed in to change notification settings - Fork 0
/
platformsupport_apple.mm
146 lines (124 loc) · 4.98 KB
/
platformsupport_apple.mm
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// This file is licensed under either of licenses:
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-License-Identifier: BSD-2-Clause
#if defined(Q_OS_IOS)
#include <UIKit/UIKit.h>
#else
#import <AppKit/AppKit.h>
#import <CoreFoundation/CoreFoundation.h>
#endif
#include <QDir>
#include <QImage>
#include <QPointer>
#include <QStandardPaths>
#include <UserNotifications/UserNotifications.h>
#if !__has_feature(objc_arc)
#error "ARC is off"
#endif
#include "platformsupport.h"
static QString kCachePath{QStringLiteral("noti-cache")};
PlatformSupport::PlatformSupport(QObject *parent)
: QObject{parent}
{
platformInit();
}
PlatformSupport::~PlatformSupport() = default;
void PlatformSupport::platformInit()
{
m_notificationsAllowed = -1;
QTimer::singleShot(0, this, [&]() {
__block QPointer<PlatformSupport> selfRef(this);
[[UNUserNotificationCenter currentNotificationCenter]
getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
if (auto self = selfRef.get()) {
qDebug() << "auth status is " << settings.authorizationStatus;
switch (settings.authorizationStatus) {
case UNAuthorizationStatusNotDetermined:
m_notificationsAllowed = -1;
break;
case UNAuthorizationStatusAuthorized:
case UNAuthorizationStatusProvisional:
#ifdef Q_OS_IOS
case UNAuthorizationStatusEphemeral:
#endif
self->m_notificationsAllowed = 1;
break;
default:
self->m_notificationsAllowed = 0;
break;
}
emit self->permissionStateFetchCompletion();
}
}];
});
auto location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
QDir cacheDir(location);
cacheDir.cd(kCachePath);
cacheDir.removeRecursively();
QDir(location).mkpath(kCachePath);
}
void PlatformSupport::requestNotifications()
{
__block QPointer<PlatformSupport> selfRef(this);
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:(UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError *_Nullable) {
if (auto self = selfRef.get()) {
qDebug() << "Granted =" << granted;
self->m_notificationsAllowed = (granted != NO) ? 1 : 0;
}
}];
}
bool PlatformSupport::requiresNotificationPermission() const
{
return true;
}
void PlatformSupport::notifyWithImage(NotifyClass,
const QString &title,
const QString &description,
const QUrl &url)
{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = title.toNSString();
content.body = description.toNSString();
content.sound = [UNNotificationSound defaultSound];
content.relevanceScore = 0.75;
if (!url.isEmpty()) {
QImage image(url.toLocalFile());
auto location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
QDir cacheDir(location);
cacheDir.cd(kCachePath);
QString randomName = QString::number(QDateTime::currentMSecsSinceEpoch());
randomName.append(QStringLiteral(".png"));
QString fullSaveLocation = cacheDir.absoluteFilePath(randomName);
qDebug() << "Saving name" << fullSaveLocation;
if (!image.isNull())
image = image.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (!image.isNull() && image.save(fullSaveLocation, "PNG")) {
UNNotificationAttachment *attachment = [UNNotificationAttachment
attachmentWithIdentifier:@""
URL:QUrl::fromLocalFile(fullSaveLocation).toNSURL()
options:@{}
error:nil];
if (attachment) {
qDebug() << "Attached URL" << url;
content.attachments = @[attachment];
} else {
qDebug() << "Could not attach" << url;
}
}
}
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:1
repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest
requestWithIdentifier:@"grab-plat-notify"
content:content
trigger:trigger];
// Schedule the notification.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request
withCompletionHandler:^(NSError *err) {
Q_UNUSED(err);
}];
}