Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request permission before showing update notification #154

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions WakaTime/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
var statusBarA11yItem: NSMenuItem!
var statusBarA11ySeparator: NSMenuItem!
var statusBarA11yStatus: Bool = true
var notificationsEnabled: Bool = false
var settingsWindowController = SettingsWindowController()
var monitoredAppsWindowController = MonitoredAppsWindowController()
var wakaTime: WakaTime?
Expand Down Expand Up @@ -49,6 +50,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
statusBarItem.menu = menu

wakaTime = WakaTime(self)

// request notifications permission
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
guard granted else {
if let msg = error?.localizedDescription {
NSLog(msg)
print(msg)
}
return
}
self.notificationsEnabled = true
}
}

@objc func handleGetURL(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) {
Expand Down Expand Up @@ -79,7 +92,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {

@objc func checkForUpdatesClicked(_ sender: AnyObject) {
updater.check {
self.sendNotification(title: "Updating to latest release")
if self.notificationsEnabled {
self.sendNotification(title: "Updating to latest release")
}
}.catch(policy: .allErrors) { error in
if error.isCancelled {
let alert = NSAlert()
Expand Down Expand Up @@ -151,7 +166,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
content: content, trigger: nil)

let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .sound]) { _, _ in }
notificationCenter.add(request)
notificationCenter.requestAuthorization(options: [.alert, .sound]) { granted, _ in
guard granted else { return }

notificationCenter.add(request)
}
}
}
Loading