Explicitly ask user for notification authorisation on launch

Signed-off-by: Claudio Cambra <claudio.cambra@gmail.com>
This commit is contained in:
Claudio Cambra 2022-05-17 19:20:04 +02:00
parent c6dd7da9fd
commit a2cc2ace4a
3 changed files with 20 additions and 4 deletions

View File

@ -101,7 +101,7 @@ Systray::Systray()
#ifdef Q_OS_MACOS
setUserNotificationCenterDelegate();
checkNotificationAuth();
checkNotificationAuth(MacNotificationAuthorizationOptions::Default); // No provisional auth, ask user explicitly first time
registerNotificationCategories(QString(tr("Download")));
#else
connect(AccountManager::instance(), &AccountManager::accountAdded,

View File

@ -40,8 +40,13 @@ public:
};
#ifdef Q_OS_MACOS
enum MacNotificationAuthorizationOptions {
Default = 0,
Provisional
};
void setUserNotificationCenterDelegate();
void checkNotificationAuth();
void checkNotificationAuth(MacNotificationAuthorizationOptions authOptions = MacNotificationAuthorizationOptions::Provisional);
void registerNotificationCategories(const QString &localizedDownloadString);
bool canOsXSendUserNotification();
void sendOsXUserNotification(const QString &title, const QString &message);

View File

@ -43,6 +43,11 @@ Q_LOGGING_CATEGORY(lcMacSystray, "nextcloud.gui.macsystray")
namespace OCC {
enum MacNotificationAuthorizationOptions {
Default = 0,
Provisional
};
double statusBarThickness()
{
return [NSStatusBar systemStatusBar].thickness;
@ -78,10 +83,16 @@ void registerNotificationCategories(const QString &localisedDownloadString) {
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:generalCategory, updateCategory, nil]];
}
void checkNotificationAuth()
void checkNotificationAuth(MacNotificationAuthorizationOptions additionalAuthOption = MacNotificationAuthorizationOptions::Provisional)
{
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionProvisional)
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
if(additionalAuthOption == MacNotificationAuthorizationOptions::Provisional) {
authOptions += UNAuthorizationOptionProvisional;
}
[center requestAuthorizationWithOptions:(authOptions)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
if(granted) {