Skip to content

Commit

Permalink
add notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang-Xijie committed Aug 6, 2021
1 parent 757f925 commit 61cfc0f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions InputSourceSwitcher.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -30,6 +31,7 @@
4880B5362683819300D53186 /* Shortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shortcuts.swift; sourceTree = "<group>"; };
4880B53926838FC800D53186 /* Applesripts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Applesripts.swift; sourceTree = "<group>"; };
48860FC726845485002A5487 /* VersionAndBuildNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VersionAndBuildNumber.swift; sourceTree = "<group>"; };
489B885326BD2EE20090DC8D /* Notificatioins.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notificatioins.swift; sourceTree = "<group>"; };
48CE6B682682611D00B1A186 /* AboutView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutView.swift; sourceTree = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -94,6 +96,7 @@
children = (
48860FC726845485002A5487 /* VersionAndBuildNumber.swift */,
4880B5362683819300D53186 /* Shortcuts.swift */,
489B885326BD2EE20090DC8D /* Notificatioins.swift */,
);
path = Tools;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions InputSourceSwitcher/APP/App.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import UserNotifications

@main
struct SourceSwitcherApp: App {
Expand Down Expand Up @@ -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?) {
Expand Down
5 changes: 5 additions & 0 deletions InputSourceSwitcher/Applescripts/Applesripts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
Expand Down
27 changes: 27 additions & 0 deletions InputSourceSwitcher/Tools/Notificatioins.swift
Original file line number Diff line number Diff line change
@@ -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.")
}
}
}

0 comments on commit 61cfc0f

Please sign in to comment.