Show Album Art with UserNotification.

CQTexperiment
Dzmitry Neviadomski 2020-12-26 16:42:56 +03:00
parent 1f3d045b62
commit 05f66d40f3
1 changed files with 26 additions and 0 deletions

View File

@ -174,6 +174,32 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
content.sound = nil;
content.categoryIdentifier = @"play";
if ([defaults boolForKey:@"notifications.show-album-art"] && [pe albumArtInternal]) {
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *tmpSubFolderName = [NSProcessInfo.processInfo globallyUniqueString];
NSURL *tmpSubFolderURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()]
URLByAppendingPathComponent:tmpSubFolderName isDirectory:true];
if ([fileManager createDirectoryAtPath:[tmpSubFolderURL path]
withIntermediateDirectories:true
attributes:nil
error:&error]) {
NSURL *fileURL = [tmpSubFolderURL URLByAppendingPathComponent:@"art.png"];
NSImage *image = [pe albumArt];
CGImageRef cgRef = [image CGImageForProposedRect:NULL context:nil hints:nil];
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cgRef];
[newRep setSize:[image size]];
NSData *pngData = [newRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
[pngData writeToURL:fileURL atomically:YES];
UNNotificationAttachment *icon = [UNNotificationAttachment attachmentWithIdentifier:@"art"
URL:fileURL
options:nil
error:&error];
content.attachments = @[icon];
}
}
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"PlayTrack" content:content trigger:nil];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {