Allow macOS notification to play sound

This commit is contained in:
pagedown 2021-11-19 14:08:00 +08:00
parent 62e32ea108
commit fccba24f0b
No known key found for this signature in database
GPG Key ID: E921CF18AC8FF6EB

View File

@ -222,6 +222,18 @@ cocoa_send_notification(PyObject *self UNUSED, PyObject *args) {
@end @end
@implementation NotificationDelegate @implementation NotificationDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
UNNotificationPresentationOptions options = UNNotificationPresentationOptionSound;
#if MAC_OS_X_VERSION_MAX_ALLOWED < 110000
options |= UNNotificationPresentationOptionAlert;
#else
options |= UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner;
#endif
completionHandler(options);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center - (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler { withCompletionHandler:(void (^)(void))completionHandler {
@ -247,6 +259,7 @@ schedule_notification(const char *identifier, const char *title, const char *bod
if (title) content.title = @(title); if (title) content.title = @(title);
if (body) content.body = @(body); if (body) content.body = @(body);
if (subtitle) content.subtitle = @(subtitle); if (subtitle) content.subtitle = @(subtitle);
content.sound = [UNNotificationSound defaultSound];
// Deliver the notification // Deliver the notification
static unsigned long counter = 1; static unsigned long counter = 1;
UNNotificationRequest* request = [ UNNotificationRequest* request = [
@ -306,7 +319,9 @@ cocoa_send_notification(PyObject *self UNUSED, PyObject *args) {
if (!center.delegate) center.delegate = [[NotificationDelegate alloc] init]; if (!center.delegate) center.delegate = [[NotificationDelegate alloc] init];
queue_notification(identifier, title, body, subtitle); queue_notification(identifier, title, body, subtitle);
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert) // The badge permission needs to be requested as well, even though it is not used,
// otherwise macOS refuses to show the preference checkbox for enable/disable notification sound.
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) { completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error != nil) { if (error != nil) {
log_error("Failed to request permission for showing notification: %s", [[error localizedDescription] UTF8String]); log_error("Failed to request permission for showing notification: %s", [[error localizedDescription] UTF8String]);