mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-03 14:12:30 -06:00
Migrate away from deprecated API on MacOS
Currently `NSUserNotifications API` is used on MacOS to display notifications. However this is marked as deprecated and should be replaced with `UserNotifications.frameworks API`. With the new API it is required to ask for permission before notifications can be send. The program will ask for permission to send notifications on the start. Related: #15630. PR #23019.
This commit is contained in:
@@ -29,13 +29,17 @@
|
||||
#include "macutilities.h"
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
#include <objc/message.h>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QPixmap>
|
||||
#include <QSize>
|
||||
#include <QString>
|
||||
|
||||
#include "base/logger.h"
|
||||
#include "base/path.h"
|
||||
|
||||
QImage qt_mac_toQImage(CGImageRef image);
|
||||
@@ -85,16 +89,36 @@ namespace MacUtils
|
||||
}
|
||||
}
|
||||
|
||||
void askForNotificationPermission()
|
||||
{
|
||||
@autoreleasepool
|
||||
{
|
||||
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:
|
||||
(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
|
||||
completionHandler:^([[maybe_unused]] BOOL granted, NSError * _Nullable error)
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
LogMsg(QCoreApplication::translate("MacUtils", "Permission for notifications not granted. Error: \"%1\"").arg
|
||||
(QString::fromNSString(error.localizedDescription)), Log::WARNING);
|
||||
}
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
void displayNotification(const QString &title, const QString &message)
|
||||
{
|
||||
@autoreleasepool
|
||||
{
|
||||
NSUserNotification *notification = [[NSUserNotification alloc] init];
|
||||
notification.title = title.toNSString();
|
||||
notification.informativeText = message.toNSString();
|
||||
notification.soundName = NSUserNotificationDefaultSoundName;
|
||||
|
||||
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
|
||||
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
||||
content.title = title.toNSString();
|
||||
content.body = message.toNSString();
|
||||
content.sound = [UNNotificationSound defaultSound];
|
||||
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:
|
||||
[[NSUUID UUID] UUIDString] content:content
|
||||
trigger:nil];
|
||||
[UNUserNotificationCenter.currentNotificationCenter
|
||||
addNotificationRequest:request withCompletionHandler:nil];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user