Implement new notification display system, when running on Mojave or newer
parent
7f3da31b45
commit
3e6d599452
|
@ -7,12 +7,13 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#import <Cocoa/Cocoa.h>
|
#import <Cocoa/Cocoa.h>
|
||||||
|
#import <UserNotifications/UserNotifications.h>
|
||||||
|
|
||||||
#import "PlaybackController.h"
|
#import "PlaybackController.h"
|
||||||
#import "PlaylistEntry.h"
|
#import "PlaylistEntry.h"
|
||||||
|
|
||||||
@class AudioScrobbler;
|
@class AudioScrobbler;
|
||||||
@interface PlaybackEventController : NSObject <NSUserNotificationCenterDelegate> {
|
@interface PlaybackEventController : NSObject <NSUserNotificationCenterDelegate, UNUserNotificationCenterDelegate> {
|
||||||
NSOperationQueue *queue;
|
NSOperationQueue *queue;
|
||||||
|
|
||||||
PlaylistEntry *entry;
|
PlaylistEntry *entry;
|
||||||
|
@ -23,6 +24,9 @@
|
||||||
|
|
||||||
IBOutlet NSWindow *mainWindow;
|
IBOutlet NSWindow *mainWindow;
|
||||||
IBOutlet NSWindow *miniWindow;
|
IBOutlet NSWindow *miniWindow;
|
||||||
|
|
||||||
|
Boolean didGainUN;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -52,6 +52,30 @@ typedef enum
|
||||||
{
|
{
|
||||||
[self initDefaults];
|
[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 = [[NSOperationQueue alloc] init];
|
||||||
[queue setMaxConcurrentOperationCount:1];
|
[queue setMaxConcurrentOperationCount:1];
|
||||||
|
|
||||||
|
@ -64,6 +88,23 @@ typedef enum
|
||||||
return self;
|
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
|
- (NSDictionary *)fillNotificationDictionary:(PlaylistEntry *)pe status:(TrackStatus)status
|
||||||
{
|
{
|
||||||
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
|
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
|
||||||
|
@ -106,6 +147,39 @@ typedef enum
|
||||||
if ([AudioScrobbler isRunning]) return;
|
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];
|
NSUserNotification *notif = [[NSUserNotification alloc] init];
|
||||||
notif.title = [pe title];
|
notif.title = [pe title];
|
||||||
|
|
|
@ -2670,6 +2670,8 @@
|
||||||
CogAudio,
|
CogAudio,
|
||||||
"-weak_framework",
|
"-weak_framework",
|
||||||
MediaPlayer,
|
MediaPlayer,
|
||||||
|
"-weak_framework",
|
||||||
|
UserNotifications,
|
||||||
"-undefined",
|
"-undefined",
|
||||||
dynamic_lookup,
|
dynamic_lookup,
|
||||||
);
|
);
|
||||||
|
@ -2710,6 +2712,8 @@
|
||||||
CogAudio,
|
CogAudio,
|
||||||
"-weak_framework",
|
"-weak_framework",
|
||||||
MediaPlayer,
|
MediaPlayer,
|
||||||
|
"-weak_framework",
|
||||||
|
UserNotifications,
|
||||||
"-undefined",
|
"-undefined",
|
||||||
dynamic_lookup,
|
dynamic_lookup,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue