From ce65ea415490953ce79853109d569fa0c068146f Mon Sep 17 00:00:00 2001 From: Ignacio Tomas Date: Mon, 26 Apr 2021 19:42:50 +0900 Subject: [PATCH] Add "open" parameter for notifications --- README.md | 2 +- SwiftBar/AppDelegate.swift | 14 ++++++++++---- SwiftBar/Plugin/PluginManger.swift | 9 ++++++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ca0d3c0..18c45a6 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ For binary plugins metadata can be added as an extended file attribute: | refreshplugin | `name` plugin [name](#plugin-naming) | Force refresh plugin by name | `swiftbar://refreshplugin?name=myplugin` | | refreshplugin | `index` plugin index in menubar, starting from `0` | Force refresh plugin by its position in menubar | `swiftbar://refreshplugin?index=1` | | addplugin | `src` source URL to plugin file | Add plugin to Swiftbar from URL | `swiftbar://addplugin?src=https://coolplugin` | -| notify | `plugin` plugin [name](#plugin-naming), notification fields `title`, `subtitle`, `body` and disable sound `silent=true` | Show notification | `swiftbar://notify?plugin=MyPlugin&title=title&subtitle=subtitle&body=body&silent=true` | +| notify | `plugin` plugin [name](#plugin-naming). Notification fields: `title`, `subtitle`, `body`. `open` to open an URL on click. `silent=true` to disable sound | Show notification | `swiftbar://notify?plugin=MyPlugin&title=title&subtitle=subtitle&body=body&silent=true` | ## Preferences aka 'defaults' diff --git a/SwiftBar/AppDelegate.swift b/SwiftBar/AppDelegate.swift index 4fd6069..669ad98 100644 --- a/SwiftBar/AppDelegate.swift +++ b/SwiftBar/AppDelegate.swift @@ -125,6 +125,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUStandardUserDriverDelegat title: url.queryParameters?["title"], subtitle: url.queryParameters?["subtitle"], body: url.queryParameters?["body"], + open: url.queryParameters?["open"], silent: url.queryParameters?["silent"] == "true") default: os_log("Unsupported URL scheme \n %{public}@", log: Log.plugin, type: .error, url.absoluteString) @@ -132,10 +133,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, SPUStandardUserDriverDelegat } } - func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification, - withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) - { - completionHandler([.alert, .sound]) + func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { + let urlString = response.notification.request.content.userInfo["url"] as? String ?? "" + + if urlString != "" { + let url = URL(string: urlString)! + NSWorkspace.shared.open(url) + } + + completionHandler() } func windowWillClose(_: Notification) { diff --git a/SwiftBar/Plugin/PluginManger.swift b/SwiftBar/Plugin/PluginManger.swift index ba28e2e..017b464 100644 --- a/SwiftBar/Plugin/PluginManger.swift +++ b/SwiftBar/Plugin/PluginManger.swift @@ -238,7 +238,7 @@ class PluginManager { } extension PluginManager { - func showNotification(pluginID: PluginID, title: String?, subtitle: String?, body: String?, silent: Bool = false) { + func showNotification(pluginID: PluginID, title: String?, subtitle: String?, body: String?, open: String?, silent: Bool = false) { guard let plugin = plugins.first(where: { $0.id == pluginID }), plugin.enabled else { return } @@ -249,6 +249,13 @@ extension PluginManager { content.sound = silent ? nil : .default content.threadIdentifier = pluginID + let urlString = open ?? "" + let url = URL(string: urlString) + + if url?.host != nil, url?.scheme != nil { + content.userInfo = ["url": urlString] + } + let uuidString = UUID().uuidString let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: nil)