From 61cfc0faa14ccddbb59a824db96544ea8915305b Mon Sep 17 00:00:00 2001 From: yxj <564197835@qq.com> Date: Fri, 6 Aug 2021 17:25:28 +0800 Subject: [PATCH] add notification --- InputSourceSwitcher.xcodeproj/project.pbxproj | 4 +++ InputSourceSwitcher/APP/App.swift | 12 +++++++++ .../Applescripts/Applesripts.swift | 5 ++++ .../Tools/Notificatioins.swift | 27 +++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 InputSourceSwitcher/Tools/Notificatioins.swift diff --git a/InputSourceSwitcher.xcodeproj/project.pbxproj b/InputSourceSwitcher.xcodeproj/project.pbxproj index 28ffd55..04a99a7 100644 --- a/InputSourceSwitcher.xcodeproj/project.pbxproj +++ b/InputSourceSwitcher.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 4880B5372683819300D53186 /* Shortcuts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4880B5362683819300D53186 /* Shortcuts.swift */; }; 4880B53A26838FC800D53186 /* Applesripts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4880B53926838FC800D53186 /* Applesripts.swift */; }; 48860FC826845485002A5487 /* VersionAndBuildNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48860FC726845485002A5487 /* VersionAndBuildNumber.swift */; }; + 489B885426BD2EE20090DC8D /* Notificatioins.swift in Sources */ = {isa = PBXBuildFile; fileRef = 489B885326BD2EE20090DC8D /* Notificatioins.swift */; }; 48CE6B692682611D00B1A186 /* AboutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48CE6B682682611D00B1A186 /* AboutView.swift */; }; 48DA10482681781500A3D537 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48DA10472681781500A3D537 /* App.swift */; }; 48DA104A2681781500A3D537 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48DA10492681781500A3D537 /* ContentView.swift */; }; @@ -30,6 +31,7 @@ 4880B5362683819300D53186 /* Shortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shortcuts.swift; sourceTree = ""; }; 4880B53926838FC800D53186 /* Applesripts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Applesripts.swift; sourceTree = ""; }; 48860FC726845485002A5487 /* VersionAndBuildNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionAndBuildNumber.swift; sourceTree = ""; }; + 489B885326BD2EE20090DC8D /* Notificatioins.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notificatioins.swift; sourceTree = ""; }; 48CE6B682682611D00B1A186 /* AboutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutView.swift; sourceTree = ""; }; 48DA10442681781500A3D537 /* Source Switcher.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Source Switcher.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 48DA10472681781500A3D537 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; @@ -94,6 +96,7 @@ children = ( 48860FC726845485002A5487 /* VersionAndBuildNumber.swift */, 4880B5362683819300D53186 /* Shortcuts.swift */, + 489B885326BD2EE20090DC8D /* Notificatioins.swift */, ); path = Tools; sourceTree = ""; @@ -241,6 +244,7 @@ 4880B5372683819300D53186 /* Shortcuts.swift in Sources */, 48462E272681EC18006DB779 /* InputSourceModel.swift in Sources */, 48CE6B692682611D00B1A186 /* AboutView.swift in Sources */, + 489B885426BD2EE20090DC8D /* Notificatioins.swift in Sources */, 48DA10482681781500A3D537 /* App.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/InputSourceSwitcher/APP/App.swift b/InputSourceSwitcher/APP/App.swift index cfb5a14..1218b9e 100644 --- a/InputSourceSwitcher/APP/App.swift +++ b/InputSourceSwitcher/APP/App.swift @@ -1,4 +1,5 @@ import SwiftUI +import UserNotifications @main struct SourceSwitcherApp: App { @@ -52,6 +53,17 @@ class AppDelegate: NSObject, NSApplicationDelegate { of: button, preferredEdge: NSRectEdge.minY) } print(popover.isShown) // true (but the popover not appear actually) + + RequestNotificationCenterAuthorization() + } + + func RequestNotificationCenterAuthorization() { + let center = UNUserNotificationCenter.current() + center.requestAuthorization(options: [.alert, .sound, .badge]) { _, error in + if let error = error { + print("[NotificationCenter.requestAuthorization] error - \(error)") + } + } } @objc func showPopover(_ sender: AnyObject?) { diff --git a/InputSourceSwitcher/Applescripts/Applesripts.swift b/InputSourceSwitcher/Applescripts/Applesripts.swift index 65566a3..1c4cb04 100644 --- a/InputSourceSwitcher/Applescripts/Applesripts.swift +++ b/InputSourceSwitcher/Applescripts/Applesripts.swift @@ -44,9 +44,14 @@ func UseApplescriptToSwitchInputSource(to inputSourceName: String) { if let script = NSAppleScript(source: applesript) { var error: NSDictionary? + script.executeAndReturnError(&error) + if let err = error { print("[Applescript] NSAppleScript.executeAndReturnError(): \(err)") + } else { + // Successfully switched. + PushNotification_DidSwitchInputSource(to: inputSourceName) } } else { print("[Applescript] NSAppleScript.init()") diff --git a/InputSourceSwitcher/Tools/Notificatioins.swift b/InputSourceSwitcher/Tools/Notificatioins.swift new file mode 100644 index 0000000..0fc560c --- /dev/null +++ b/InputSourceSwitcher/Tools/Notificatioins.swift @@ -0,0 +1,27 @@ +import Cocoa + +import UserNotifications + +func PushNotification_DidSwitchInputSource(to inputsourceName: String) { + // Create the notification and setup information + let content = UNMutableNotificationContent() + content.title = "\(inputsourceName)" + content.body = "Successfully switched." + + // Create the request + let uuidString = UUID().uuidString // TODO: not clear + let request = UNNotificationRequest( + identifier: uuidString, + content: content, + trigger: .none) + + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.add(request) { error in + if error != nil { + print("[PushNotification_DidSwitchInputSource] Error") + } else { + // Successfully + print("[PushNotification_DidSwitchInputSource] Successfully pushed.") + } + } +}