Richer notifications (#68)

This commit is contained in:
Max Goedjen
2020-03-21 17:52:51 -07:00
committed by GitHub
parent f674d47507
commit a0052e8395
5 changed files with 36 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ import Foundation
import SecretKit
import SecretAgentKit
import UserNotifications
import AppKit
class Notifier {
@@ -14,14 +15,34 @@ class Notifier {
func notify(accessTo secret: AnySecret, by provenance: SigningRequestProvenance) {
let notificationCenter = UNUserNotificationCenter.current()
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Signed Request"
notificationContent.body = "\(secret.name) was used to sign a request from \(provenance.origin.name)."
notificationContent.title = "Signed Request from \(provenance.origin.name)"
notificationContent.subtitle = secret.name
if let iconURL = iconURL(for: provenance), let attachment = try? UNNotificationAttachment(identifier: "icon", url: iconURL, options: nil) {
notificationContent.attachments = [attachment]
}
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: nil)
notificationCenter.add(request, withCompletionHandler: nil)
}
}
extension Notifier {
func iconURL(for provenance: SigningRequestProvenance) -> URL? {
do {
if let app = NSRunningApplication(processIdentifier: provenance.origin.pid), let icon = app.icon?.tiffRepresentation {
let temporaryURL = URL(fileURLWithPath: (NSTemporaryDirectory() as NSString).appendingPathComponent("\(UUID().uuidString).png"))
let bitmap = NSBitmapImageRep(data: icon)
try bitmap?.representation(using: .png, properties: [:])?.write(to: temporaryURL)
return temporaryURL
}
} catch {
}
return nil
}
}
extension Notifier: SigningWitness {
func speakNowOrForeverHoldYourPeace(forAccessTo secret: AnySecret, by provenance: SigningRequestProvenance) throws {

BIN
SecretAgent/Untitled.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB