Implement new notification display system, when running on Mojave or newer

CQTexperiment
Christopher Snowhill 2020-03-21 01:51:35 -07:00
parent 7f3da31b45
commit 3e6d599452
3 changed files with 83 additions and 1 deletions

View File

@ -7,12 +7,13 @@
//
#import <Cocoa/Cocoa.h>
#import <UserNotifications/UserNotifications.h>
#import "PlaybackController.h"
#import "PlaylistEntry.h"
@class AudioScrobbler;
@interface PlaybackEventController : NSObject <NSUserNotificationCenterDelegate> {
@interface PlaybackEventController : NSObject <NSUserNotificationCenterDelegate, UNUserNotificationCenterDelegate> {
NSOperationQueue *queue;
PlaylistEntry *entry;
@ -23,6 +24,9 @@
IBOutlet NSWindow *mainWindow;
IBOutlet NSWindow *miniWindow;
Boolean didGainUN;
}
@end

View File

@ -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];

View File

@ -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,
);