From 6b7d9ce8bcfe9daedc74b39bb740767a4bc804a7 Mon Sep 17 00:00:00 2001 From: winebarrel Date: Wed, 11 Dec 2024 17:34:21 +0900 Subject: [PATCH] refactor: Fix notification initialize --- PagerCall/AppDelegate.swift | 13 +++++++++++++ PagerCall/Notification.swift | 11 ----------- PagerCall/PagerCallApp.swift | 2 -- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/PagerCall/AppDelegate.swift b/PagerCall/AppDelegate.swift index f4a4c2a..e614948 100644 --- a/PagerCall/AppDelegate.swift +++ b/PagerCall/AppDelegate.swift @@ -4,6 +4,19 @@ import UserNotifications class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_: Foundation.Notification) { UNUserNotificationCenter.current().delegate = self + + Task { + do { + let userNotificationCenter = UNUserNotificationCenter.current() + + guard try await userNotificationCenter.requestAuthorization(options: [.alert, .sound]) else { + Logger.shared.warning("user notification not authorized") + return + } + } catch { + Logger.shared.error("user notification authorization request error: \(error)") + } + } } } diff --git a/PagerCall/Notification.swift b/PagerCall/Notification.swift index 18be7af..64592c1 100644 --- a/PagerCall/Notification.swift +++ b/PagerCall/Notification.swift @@ -1,17 +1,6 @@ import UserNotifications enum Notification { - static func initialize() { - let userNotificationCenter = UNUserNotificationCenter.current() - - userNotificationCenter.requestAuthorization(options: [.alert, .sound]) { authorized, _ in - guard authorized else { - Logger.shared.warning("user notification not authorized") - return - } - } - } - static func notify(id: String, title: String, body: String, url: String) async { let userNotificationCenter = UNUserNotificationCenter.current() diff --git a/PagerCall/PagerCallApp.swift b/PagerCall/PagerCallApp.swift index 57518b1..71a8065 100644 --- a/PagerCall/PagerCallApp.swift +++ b/PagerCall/PagerCallApp.swift @@ -25,8 +25,6 @@ struct PagerCallApp: App { }() private func initialize() { - Notification.initialize() - let contentView = ContentView(pagerDuty: pagerDuty, apiKey: $apiKey) popover.contentViewController = NSHostingController(rootView: contentView)