From 3e6d599452a738aeb07a07894b865c0bb6890b08 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sat, 21 Mar 2020 01:51:35 -0700 Subject: [PATCH] Implement new notification display system, when running on Mojave or newer --- Application/PlaybackEventController.h | 6 ++- Application/PlaybackEventController.m | 74 +++++++++++++++++++++++++++ Cog.xcodeproj/project.pbxproj | 4 ++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Application/PlaybackEventController.h b/Application/PlaybackEventController.h index e4bf3f79d..6b9e58683 100644 --- a/Application/PlaybackEventController.h +++ b/Application/PlaybackEventController.h @@ -7,12 +7,13 @@ // #import +#import #import "PlaybackController.h" #import "PlaylistEntry.h" @class AudioScrobbler; -@interface PlaybackEventController : NSObject { +@interface PlaybackEventController : NSObject { NSOperationQueue *queue; PlaylistEntry *entry; @@ -23,6 +24,9 @@ IBOutlet NSWindow *mainWindow; IBOutlet NSWindow *miniWindow; + + Boolean didGainUN; + } @end diff --git a/Application/PlaybackEventController.m b/Application/PlaybackEventController.m index d748e677f..26819f3fd 100644 --- a/Application/PlaybackEventController.m +++ b/Application/PlaybackEventController.m @@ -51,6 +51,30 @@ typedef enum if (self) { [self initDefaults]; + + didGainUN = NO; + + if (@available(macOS 10.14,*)) { + UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; + [center requestAuthorizationWithOptions:UNAuthorizationOptionAlert + completionHandler:^(BOOL granted, NSError * _Nullable error) { + self->didGainUN = granted; + + if (granted) { + UNNotificationAction * skipAction = [UNNotificationAction actionWithIdentifier:@"skip" title:@"Skip" options:UNNotificationActionOptionNone]; + + UNNotificationCategory* playCategory = [UNNotificationCategory + categoryWithIdentifier:@"play" + actions:@[skipAction] + intentIdentifiers:@[] + options:UNNotificationCategoryOptionNone]; + + [center setNotificationCategories:[NSSet setWithObjects:playCategory, nil]]; + } + }]; + + [center setDelegate:self]; + } queue = [[NSOperationQueue alloc] init]; [queue setMaxConcurrentOperationCount:1]; @@ -64,6 +88,23 @@ typedef enum return self; } +- (void)userNotificationCenter:(UNUserNotificationCenter *)center + willPresentNotification:(UNNotification *)notification + withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(macos(10.14)){ + UNNotificationPresentationOptions presentationOptions = + UNNotificationPresentationOptionAlert; + + completionHandler(presentationOptions); +} + +- (void)userNotificationCenter:(UNUserNotificationCenter *)center +didReceiveNotificationResponse:(UNNotificationResponse *)response + withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(macos(10.14)){ + if ([[response actionIdentifier] isEqualToString:@"skip"]) { + [playbackController next:self]; + } +} + - (NSDictionary *)fillNotificationDictionary:(PlaylistEntry *)pe status:(TrackStatus)status { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; @@ -106,6 +147,39 @@ typedef enum if ([AudioScrobbler isRunning]) return; } + if (@available(macOS 10.14,*)) + { + if (didGainUN) { + UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; + + UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; + + content.title = @"Now Playing"; + + NSString *subtitle; + if ([pe artist] && [pe album]) { + subtitle = [NSString stringWithFormat:@"%@ - %@", [pe artist], [pe album]]; + } else if ([pe artist]) { + subtitle = [pe artist]; + } else if ([pe album]) { + subtitle = [pe album]; + } else { + subtitle = @""; + } + + NSString *body = [NSString stringWithFormat:@"%@\n%@", [pe title], subtitle]; + content.body = body; + content.sound = nil; + content.categoryIdentifier = @"play"; + + UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"PlayTrack" content:content trigger:nil]; + + [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { + NSLog(@"%@", error.localizedDescription); + }]; + } + } + else { NSUserNotification *notif = [[NSUserNotification alloc] init]; notif.title = [pe title]; diff --git a/Cog.xcodeproj/project.pbxproj b/Cog.xcodeproj/project.pbxproj index faf5c539e..682536ca7 100644 --- a/Cog.xcodeproj/project.pbxproj +++ b/Cog.xcodeproj/project.pbxproj @@ -2670,6 +2670,8 @@ CogAudio, "-weak_framework", MediaPlayer, + "-weak_framework", + UserNotifications, "-undefined", dynamic_lookup, ); @@ -2710,6 +2712,8 @@ CogAudio, "-weak_framework", MediaPlayer, + "-weak_framework", + UserNotifications, "-undefined", dynamic_lookup, );