Skip to content

Commit

Permalink
Merge pull request #154 from wakatime/bugfix/request-notification-perm
Browse files Browse the repository at this point in the history
Request permission before showing update notification
  • Loading branch information
alanhamlett authored Aug 11, 2023
2 parents e7e2040 + 20412d8 commit f58b1dc
Showing 1 changed file with 21 additions and 3 deletions.
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)
}
}
}

0 comments on commit f58b1dc

Please sign in to comment.