From 360ecfb128d50d519c974674ecb095cc0060ef67 Mon Sep 17 00:00:00 2001 From: Hwang Sang Hwan <137605270+Hrepay@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:02:19 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[#487]=20=ED=8F=89=EC=9D=BC=2011=EC=8B=9C?= =?UTF-8?q?=20=EC=9D=BC=EB=B0=98=20=ED=95=99=EC=8B=9D=20=EB=A1=9C=EC=BB=AC?= =?UTF-8?q?=20=EC=95=8C=EB=A6=BC=EC=9D=84=20=EB=B3=B5=EA=B5=AC=ED=95=98?= =?UTF-8?q?=EA=B3=A0,=20=EA=B0=99=EC=9D=80=20=EB=82=A0=20=EC=B0=9C=20?= =?UTF-8?q?=EC=95=8C=EB=A6=BC=EC=9D=84=20=EB=B0=9B=EC=9C=BC=EB=A9=B4=20?= =?UTF-8?q?=EB=A1=9C=EC=BB=AC=20=EC=95=8C=EB=A6=BC=EC=9D=80=20=EA=B1=B0?= =?UTF-8?q?=EB=A5=B4=EB=8F=84=EB=A1=9D=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Resources/en.lproj/Localizable.strings | 4 + .../Resources/ja.lproj/Localizable.strings | 4 + .../Resources/ko.lproj/Localizable.strings | 4 + .../Resources/vi.lproj/Localizable.strings | 4 + .../DailyMenuNotificationScheduler.swift | 118 ++++++++++++++++++ .../Notification/NotificationManager.swift | 8 -- .../NotificationPermissionPrompt.swift | 4 +- .../App/Sources/Notification/PushRoute.swift | 16 +++ .../LanguageSettingViewController.swift | 2 + .../ViewController/MyPageViewController.swift | 3 +- .../Utility/Application/AppDelegate.swift | 35 ++++-- .../Sources/Utility/Literal/TextLiteral.swift | 8 ++ .../DailyMenuNotificationSchedulerTests.swift | 48 +++++++ 13 files changed, 241 insertions(+), 17 deletions(-) create mode 100644 EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift create mode 100644 EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift diff --git a/EATSSU/App/Resources/en.lproj/Localizable.strings b/EATSSU/App/Resources/en.lproj/Localizable.strings index 945082a5..28fd1dbd 100644 --- a/EATSSU/App/Resources/en.lproj/Localizable.strings +++ b/EATSSU/App/Resources/en.lproj/Localizable.strings @@ -460,6 +460,10 @@ // MARK: - Notification +/// ๐Ÿค” ์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ +"notification.dailyWeekdayNotificationTitle" = "๐Ÿค” What should I eat todayโ€ฆ"; +/// ์˜ค๋Š˜์˜ ํ•™์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! +"notification.dailyWeekdayNotificationBody" = "Check today's cafeteria menu!"; /// ์•Œ๋ฆผ ๊ถŒํ•œ ํ•„์š” "notification_error_permission_denied_message" = "Notification Permission Required"; /// ์•Œ๋ฆผ์„ ๋ฐ›์œผ๋ ค๋ฉด ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\n์„ค์ •์—์„œ ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ—ˆ์šฉํ•ด์ฃผ์„ธ์š”. diff --git a/EATSSU/App/Resources/ja.lproj/Localizable.strings b/EATSSU/App/Resources/ja.lproj/Localizable.strings index 5d9b97eb..973f3faf 100644 --- a/EATSSU/App/Resources/ja.lproj/Localizable.strings +++ b/EATSSU/App/Resources/ja.lproj/Localizable.strings @@ -461,6 +461,10 @@ // MARK: - Notification +/// ๐Ÿค” ์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ +"notification.dailyWeekdayNotificationTitle" = "๐Ÿค” ไปŠๆ—ฅไฝ•ใ‚’้ฃŸในใ‚ˆใ†โ€ฆ"; +/// ์˜ค๋Š˜์˜ ํ•™์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! +"notification.dailyWeekdayNotificationBody" = "ไปŠๆ—ฅใฎๅญฆ้ฃŸใƒกใƒ‹ใƒฅใƒผใ‚’ใƒใ‚งใƒƒใ‚ฏใ—ใพใ—ใ‚‡ใ†๏ผ"; /// ์•Œ๋ฆผ ๊ถŒํ•œ ํ•„์š” "notification_error_permission_denied_message" = "้€š็Ÿฅๆจฉ้™ใŒๅฟ…่ฆใงใ™"; /// ์•Œ๋ฆผ์„ ๋ฐ›์œผ๋ ค๋ฉด ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\n์„ค์ •์—์„œ ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ—ˆ์šฉํ•ด์ฃผ์„ธ์š”. diff --git a/EATSSU/App/Resources/ko.lproj/Localizable.strings b/EATSSU/App/Resources/ko.lproj/Localizable.strings index 34dc877e..fb4a1794 100644 --- a/EATSSU/App/Resources/ko.lproj/Localizable.strings +++ b/EATSSU/App/Resources/ko.lproj/Localizable.strings @@ -461,6 +461,10 @@ // MARK: - Notification +/// ๐Ÿค” ์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ +"notification.dailyWeekdayNotificationTitle" = "๐Ÿค” ์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ"; +/// ์˜ค๋Š˜์˜ ํ•™์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! +"notification.dailyWeekdayNotificationBody" = "์˜ค๋Š˜์˜ ํ•™์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š”!"; /// ์•Œ๋ฆผ ๊ถŒํ•œ ํ•„์š” "notification_error_permission_denied_message" = "์•Œ๋ฆผ ๊ถŒํ•œ ํ•„์š”"; /// ์•Œ๋ฆผ์„ ๋ฐ›์œผ๋ ค๋ฉด ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\n์„ค์ •์—์„œ ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ—ˆ์šฉํ•ด์ฃผ์„ธ์š”. diff --git a/EATSSU/App/Resources/vi.lproj/Localizable.strings b/EATSSU/App/Resources/vi.lproj/Localizable.strings index b4ef841b..cdae42c6 100644 --- a/EATSSU/App/Resources/vi.lproj/Localizable.strings +++ b/EATSSU/App/Resources/vi.lproj/Localizable.strings @@ -461,6 +461,10 @@ // MARK: - Notification +/// ๐Ÿค” ์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ +"notification.dailyWeekdayNotificationTitle" = "๐Ÿค” Hรดm nay ฤƒn gรฌ ฤ‘รขyโ€ฆ"; +/// ์˜ค๋Š˜์˜ ํ•™์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! +"notification.dailyWeekdayNotificationBody" = "Xem thแปฑc ฤ‘ฦกn nhร  ฤƒn hรดm nay!"; /// ์•Œ๋ฆผ ๊ถŒํ•œ ํ•„์š” "notification_error_permission_denied_message" = "Cแบงn quyแปn thรดng bรกo"; /// ์•Œ๋ฆผ์„ ๋ฐ›์œผ๋ ค๋ฉด ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.\n์„ค์ •์—์„œ ์•Œ๋ฆผ ๊ถŒํ•œ์„ ํ—ˆ์šฉํ•ด์ฃผ์„ธ์š”. diff --git a/EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift b/EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift new file mode 100644 index 00000000..a0455158 --- /dev/null +++ b/EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift @@ -0,0 +1,118 @@ +// +// DailyMenuNotificationScheduler.swift +// EATSSU +// +// Created by ํ™ฉ์ƒํ™˜ on 11/9/26. +// + +import Foundation +import UserNotifications + +/// ํ‰์ผ 11์‹œ "์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ" ๋กœ์ปฌ ์•Œ๋ฆผ +/// +/// ์ฐœ ์•Œ๋ฆผ์€ ์„œ๋ฒ„๊ฐ€ ์ฐœํ•œ ๋ฉ”๋‰ด๊ฐ€ ๋‚˜์˜จ ์‚ฌ์šฉ์ž์—๊ฒŒ๋งŒ ํ‘ธ์‹œ๋กœ ๋ณด๋‚ด๊ณ , ๊ทธ ์™ธ ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ฐ€๋Š” ์ผ๋ฐ˜ ํ•™์‹ ์•Œ๋ฆผ์€ +/// ์„œ๋ฒ„ ๋ฒ”์œ„๊ฐ€ ์•„๋‹ˆ์–ด์„œ ์•ฑ์ด ๋กœ์ปฌ ์•Œ๋ฆผ์œผ๋กœ ๋ณด๋‚ธ๋‹ค. "๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ON + ๊ธฐ๊ธฐ ์•Œ๋ฆผ ๊ถŒํ•œ ํ—ˆ์šฉ"์ผ ๋•Œ๋งŒ ์˜ˆ์•ฝํ•˜๊ณ , +/// ๊ทธ ์™ธ์—๋Š” ์ทจ์†Œํ•œ๋‹ค. ์„œ๋ฒ„ ์•Œ๋ฆผํ•จ์—๋Š” ๋‚จ์ง€ ์•Š๋Š”๋‹ค +enum DailyMenuNotificationScheduler { + + // MARK: - Constants + + /// ์•Œ๋ฆผ ์‹๋ณ„์ž ์ ‘๋‘์‚ฌ. ์š”์ผ๋ณ„๋กœ ํ•˜๋‚˜์”ฉ ์˜ˆ์•ฝํ•œ๋‹ค (`weekdayNotification-2` โ€ฆ `-6`) + static let identifierPrefix = "weekdayNotification-" + + /// ์›”~๊ธˆ (`Calendar.weekday`: 1์ด ์ผ์š”์ผ) + static let weekdays = [2, 3, 4, 5, 6] + + static let hour = 11 + + /// ๋‹น์ผ ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์„ ๋ฐ›์€ ๋‚ ์งœ(yyyyMMdd). ๊ฐ™์€ ๋‚  ๋กœ์ปฌ ์•Œ๋ฆผ์€ ๋„์šฐ์ง€ ์•Š๋Š”๋‹ค + private static let favoritePushReceivedDayKey = "dailyMenuNotification.favoritePushReceivedDay" + + // MARK: - Decision + + static func identifier(weekday: Int) -> String { + "\(identifierPrefix)\(weekday)" + } + + /// ์ฃผ์–ด์ง„ ์‹œ๊ฐ์ด ์†ํ•œ ์š”์ผ์˜ ์•Œ๋ฆผ ์‹๋ณ„์ž + static func identifier(for date: Date, calendar: Calendar = .current) -> String { + identifier(weekday: calendar.component(.weekday, from: date)) + } + + static func isDailyMenuNotification(identifier: String) -> Bool { + identifier.hasPrefix(identifierPrefix) + } + + /// ์˜ˆ์•ฝ ์กฐ๊ฑด: ๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ํ† ๊ธ€ ON + ๊ธฐ๊ธฐ ๊ถŒํ•œ ํ—ˆ์šฉ + static func shouldSchedule(authorization: UNAuthorizationStatus, isToggleOn: Bool) -> Bool { + guard isToggleOn else { return false } + switch authorization { + case .authorized, .provisional, .ephemeral: + return true + case .denied, .notDetermined: + return false + @unknown default: + return false + } + } + + // MARK: - Sync + + /// ํ˜„์žฌ ํ† ๊ธ€ยท๊ถŒํ•œ ์ƒํƒœ์— ๋งž๊ฒŒ ์˜ˆ์•ฝํ•˜๊ฑฐ๋‚˜ ์ทจ์†Œํ•œ๋‹ค + /// ํ˜ธ์ถœ ์‹œ์ : ์•ฑ ์‹คํ–‰, ์•Œ๋ฆผ ๊ถŒํ•œ ๊ฒฐ๊ณผ, ๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ํ† ๊ธ€ ๋ณ€๊ฒฝ, ์–ธ์–ด ๋ณ€๊ฒฝ(๋ฌธ๊ตฌ ๊ฐฑ์‹ ) + static func sync() { + Task { + let settings = await UNUserNotificationCenter.current().notificationSettings() + let isToggleOn = UserDefaults.standard.bool(forKey: TextLiteral.MyPage.pushNotificationUserSettingKey) + if shouldSchedule(authorization: settings.authorizationStatus, isToggleOn: isToggleOn) { + await schedule() + } else { + cancel() + } + } + } + + /// ์›”~๊ธˆ 11์‹œ์— ๋ฐ˜๋ณต๋˜๋Š” ์•Œ๋ฆผ์„ ๋“ฑ๋กํ•œ๋‹ค. ๊ฐ™์€ ์‹๋ณ„์ž๋กœ ๋‹ค์‹œ ๋“ฑ๋กํ•˜๋ฉด ๋ฎ์–ด์“ฐ๋ฏ€๋กœ(์–ธ์–ด ๋ณ€๊ฒฝ ๋“ฑ) ์ทจ์†Œ ์—†์ด ํ˜ธ์ถœํ•ด๋„ ๋œ๋‹ค + private static func schedule() async { + let center = UNUserNotificationCenter.current() + for weekday in weekdays { + let content = UNMutableNotificationContent() + content.title = TextLiteral.Notification.dailyWeekdayNotificationTitle + content.body = TextLiteral.Notification.dailyWeekdayNotificationBody + content.sound = .default + + var components = DateComponents() + components.weekday = weekday + components.hour = hour + components.minute = 0 + let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) + + let request = UNNotificationRequest(identifier: identifier(weekday: weekday), content: content, trigger: trigger) + do { + try await center.add(request) + } catch { + print("11์‹œ ํ•™์‹ ์•Œ๋ฆผ ์˜ˆ์•ฝ ์‹คํŒจ: \(error.localizedDescription)") + } + } + } + + static func cancel() { + UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: weekdays.map(identifier(weekday:))) + } + + // MARK: - ์ฐœ ์•Œ๋ฆผ๊ณผ ์ค‘๋ณต ์ œ๊ฑฐ + + /// ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์„ ๋ฐ›์•˜์„ ๋•Œ ํ˜ธ์ถœ. ๊ฐ™์€ ๋‚  ์ด๋ฏธ ๋–  ์žˆ๋Š” ๋กœ์ปฌ ์•Œ๋ฆผ์„ ์•Œ๋ฆผ ์„ผํ„ฐ์—์„œ ์ง€์šฐ๊ณ , + /// ์•„์ง ์•ˆ ๋–ด๋‹ค๋ฉด `shouldPresent`๊ฐ€ ๊ฑฐ๋ฅด๋„๋ก ๋‚ ์งœ๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค + /// + /// ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์žˆ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ํ‘ธ์‹œ๋ฅผ ๋ˆ„๋ฅด์ง€ ์•Š์œผ๋ฉด ์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์•„ ๋‘ ์•Œ๋ฆผ์ด ํ•จ๊ป˜ ๋‚จ์„ ์ˆ˜ ์žˆ๋‹ค + static func didReceiveFavoritePush(on date: Date = Date()) { + UserDefaults.standard.set(DateFormatter.serverDay.string(from: date), forKey: favoritePushReceivedDayKey) + UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [identifier(for: date)]) + } + + /// ์•ฑ ์‚ฌ์šฉ ์ค‘ ๋กœ์ปฌ ์•Œ๋ฆผ์ด ๋„์ฐฉํ–ˆ์„ ๋•Œ ๋„์šธ์ง€. ๊ฐ™์€ ๋‚  ์ฐœ ์•Œ๋ฆผ์„ ์ด๋ฏธ ๋ฐ›์•˜์œผ๋ฉด ๋„์šฐ์ง€ ์•Š๋Š”๋‹ค + static func shouldPresent(dailyNotificationOn date: Date = Date()) -> Bool { + UserDefaults.standard.string(forKey: favoritePushReceivedDayKey) != DateFormatter.serverDay.string(from: date) + } +} diff --git a/EATSSU/App/Sources/Notification/NotificationManager.swift b/EATSSU/App/Sources/Notification/NotificationManager.swift index 666bdcb0..7ca83505 100644 --- a/EATSSU/App/Sources/Notification/NotificationManager.swift +++ b/EATSSU/App/Sources/Notification/NotificationManager.swift @@ -16,14 +16,6 @@ class NotificationManager { // MARK: - Methods - /// ์ด์ „ ๋ฒ„์ „์ด ๊ธฐ๊ธฐ์— ์˜ˆ์•ฝํ•ด ๋‘” ํ‰์ผ 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์„ ์ง€์šด๋‹ค - /// - /// 11์‹œ ํ•™์‹ ์•Œ๋ฆผ์€ ์„œ๋ฒ„ ํ‘ธ์‹œ(์ฐœ ์•Œ๋ฆผ / ์ผ๋ฐ˜ ํ•™์‹ ์•Œ๋ฆผ, ํ•˜๋ฃจ 1๊ฑด)๋กœ ์˜ฎ๊ฒจ, ์—…๋ฐ์ดํŠธ ํ›„์—๋„ ๋กœ์ปฌ ์•Œ๋ฆผ์ด ๋‚จ์•„ ์žˆ์œผ๋ฉด ์ค‘๋ณต์œผ๋กœ ์˜จ๋‹ค. - func removeLegacyDailyNotifications() { - let identifiers = [2, 3, 4, 5, 6].map { "weekdayNotification-\($0)" } - UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers) - } - /// ์•ฑ ์‹คํ–‰ ์‹œ ์•Œ๋ฆผ ๋ฐœ์†ก ๊ถŒํ•œ์„ ์š”์ฒญํ•˜๋Š” ํŒ์—… ํ˜ธ์ถœ ๋ฉ”์†Œ๋“œ func requestNotificationPermission() async throws -> Bool { return try await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) diff --git a/EATSSU/App/Sources/Notification/NotificationPermissionPrompt.swift b/EATSSU/App/Sources/Notification/NotificationPermissionPrompt.swift index a22372d7..fc7a1279 100644 --- a/EATSSU/App/Sources/Notification/NotificationPermissionPrompt.swift +++ b/EATSSU/App/Sources/Notification/NotificationPermissionPrompt.swift @@ -39,7 +39,7 @@ enum NotificationPermissionPrompt { } /// "์•Œ๋ฆผ ํ—ˆ์šฉํ•˜๊ธฐ": ๋ฏธ๊ฒฐ์ •์ด๋ฉด ์‹œ์Šคํ…œ ๊ถŒํ•œ ์š”์ฒญ, ์ด๋ฏธ ๊ฑฐ๋ถ€๋ผ ์žˆ์œผ๋ฉด ์„ค์ • ์ด๋™ ์•ˆ๋‚ด - /// ํ—ˆ์šฉ๋˜๋ฉด ๋งˆ์ดํŽ˜์ด์ง€ ํ† ๊ธ€์„ ์ผœ๊ณ  ์„œ๋ฒ„์— ํ‘ธ์‹œ ํ† ํฐ์„ ๋“ฑ๋กํ•œ๋‹ค + /// ํ—ˆ์šฉ๋˜๋ฉด ๋งˆ์ดํŽ˜์ด์ง€ ํ† ๊ธ€์„ ์ผœ๊ณ , ์„œ๋ฒ„ ํ‘ธ์‹œ ํ† ํฐ ๋“ฑ๋ก๊ณผ 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ ์˜ˆ์•ฝ์„ ๋งž์ถ˜๋‹ค static func requestPermission(on tabBar: CustomTabBarContainerController) { Task { @MainActor in let settings = await NotificationManager.shared.checkNotificationSetting() @@ -48,11 +48,13 @@ enum NotificationPermissionPrompt { let granted = (try? await NotificationManager.shared.requestNotificationPermission()) ?? false UserDefaults.standard.set(granted, forKey: TextLiteral.MyPage.pushNotificationUserSettingKey) PushTokenManager.shared.sync() + DailyMenuNotificationScheduler.sync() case .denied: presentSettingsDialog(on: tabBar) default: UserDefaults.standard.set(true, forKey: TextLiteral.MyPage.pushNotificationUserSettingKey) PushTokenManager.shared.sync() + DailyMenuNotificationScheduler.sync() } } } diff --git a/EATSSU/App/Sources/Notification/PushRoute.swift b/EATSSU/App/Sources/Notification/PushRoute.swift index 2168ad00..307902e1 100644 --- a/EATSSU/App/Sources/Notification/PushRoute.swift +++ b/EATSSU/App/Sources/Notification/PushRoute.swift @@ -18,6 +18,13 @@ struct PushRoute { /// ์•Œ๋ฆผํ•จ ํ•ญ๋ชฉ id. ์žˆ์œผ๋ฉด ์•Œ๋ฆผ์„ ๋ˆ„๋ฅธ ์‹œ์ ์— ์ฝ์Œ ์ฒ˜๋ฆฌํ•œ๋‹ค (์„œ๋ฒ„๊ฐ€ ์•Œ๋ฆผํ•จ์„ ์ง€์›ํ•˜๊ธฐ ์ „ ํ‘ธ์‹œ์—๋Š” ์—†๋‹ค) let notificationId: Int? + /// ์•ฑ์ด ์ง์ ‘ ๋งŒ๋“œ๋Š” ์ด๋™ (์˜ˆ: 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ โ†’ ์˜ค๋Š˜ ํ•™์‹) + init(date: Date, mealTime: MealTime?, notificationId: Int?) { + self.date = date + self.mealTime = mealTime + self.notificationId = notificationId + } + /// ์„œ๋ฒ„ ํ‘ธ์‹œ data: `type`(MENU_FAVORITE / DAILY_MENU), `date`(yyyyMMdd), `timePart`(์ฐœ ์•Œ๋ฆผ๋งŒ), `notificationId`(๋ฌธ์ž์—ด) init?(userInfo: [AnyHashable: Any]) { guard let type = userInfo["type"] as? String, @@ -53,6 +60,15 @@ final class PushRouter { return } guard let route = PushRoute(userInfo: userInfo) else { return } + handle(route: route) + } + + /// ํ•ด์„์ด ๋๋‚œ ์ด๋™์„ ๋ณด๊ด€ํ•˜๊ณ , ์•ฑ์ด ํ™œ์„ฑ ์ƒํƒœ๋ฉด ๋ฐ”๋กœ ์ ์šฉํ•œ๋‹ค + func handle(route: PushRoute) { + guard Thread.isMainThread else { + DispatchQueue.main.async { self.handle(route: route) } + return + } pendingRoute = (route, Date()) if UIApplication.shared.applicationState == .active { applyPendingRoute() diff --git a/EATSSU/App/Sources/Presentation/MyPage/ViewController/LanguageSettingViewController.swift b/EATSSU/App/Sources/Presentation/MyPage/ViewController/LanguageSettingViewController.swift index ab22ce68..0d1f66f0 100644 --- a/EATSSU/App/Sources/Presentation/MyPage/ViewController/LanguageSettingViewController.swift +++ b/EATSSU/App/Sources/Presentation/MyPage/ViewController/LanguageSettingViewController.swift @@ -121,6 +121,8 @@ final class LanguageSettingViewController: BaseViewController { AppLanguageManager.shared.changeLanguage(to: language) ServerLanguageSync.sync(language) + // ์˜ˆ์•ฝ๋œ 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ ๋ฌธ๊ตฌ๋ฅผ ์ƒˆ ์–ธ์–ด๋กœ ๋‹ค์‹œ ๋“ฑ๋กํ•œ๋‹ค + DailyMenuNotificationScheduler.sync() didChangeLanguage = true AnalyticsService.logEvent("change_language", parameters: ["language": language.rawValue]) diff --git a/EATSSU/App/Sources/Presentation/MyPage/ViewController/MyPageViewController.swift b/EATSSU/App/Sources/Presentation/MyPage/ViewController/MyPageViewController.swift index ed1eec2d..0f11dc50 100644 --- a/EATSSU/App/Sources/Presentation/MyPage/ViewController/MyPageViewController.swift +++ b/EATSSU/App/Sources/Presentation/MyPage/ViewController/MyPageViewController.swift @@ -379,8 +379,9 @@ extension MyPageViewController: UITableViewDelegate { // ์Šค์œ„์น˜ ์ƒํƒœ ์—…๋ฐ์ดํŠธ self.switchState = newState self.saveSwitchStateToUserDefaults() - // ์„œ๋ฒ„๋Š” ํ‘ธ์‹œ ํ† ํฐ ๋“ฑ๋ก ์—ฌ๋ถ€๋กœ ์•Œ๋ฆผ ํ—ˆ์šฉ์„ ํŒ๋‹จํ•œ๋‹ค (ON: ๋“ฑ๋ก, OFF: ์‚ญ์ œ) + // ์„œ๋ฒ„๋Š” ํ‘ธ์‹œ ํ† ํฐ ๋“ฑ๋ก ์—ฌ๋ถ€๋กœ ์•Œ๋ฆผ ํ—ˆ์šฉ์„ ํŒ๋‹จํ•œ๋‹ค (ON: ๋“ฑ๋ก, OFF: ์‚ญ์ œ). 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ๋„ ํ•จ๊ป˜ ๋งž์ถ˜๋‹ค PushTokenManager.shared.sync() + DailyMenuNotificationScheduler.sync() // UI ์—…๋ฐ์ดํŠธ if let cell = self.mypageView.myPageTableView.cellForRow(at: indexPath) as? NotificationSettingTableViewCell { diff --git a/EATSSU/App/Sources/Utility/Application/AppDelegate.swift b/EATSSU/App/Sources/Utility/Application/AppDelegate.swift index 2a585d19..89c10835 100644 --- a/EATSSU/App/Sources/Utility/Application/AppDelegate.swift +++ b/EATSSU/App/Sources/Utility/Application/AppDelegate.swift @@ -53,13 +53,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // FCM ์•Œ๋ฆผ์ธ์ง€ ํ™•์ธ if isFCMNotification(userInfo) { LaunchSourceManager.shared.setSource(.remoteNotification) + if isFavoritePush(userInfo) { + DailyMenuNotificationScheduler.didReceiveFavoritePush() + } + // ํ•™์‹ ์•Œ๋ฆผ์ด๋ฉด ํ•ด๋‹น ๋‚ ์งœยท์‹œ๊ฐ„๋Œ€์˜ ํ•™์‹ ํ™”๋ฉด์œผ๋กœ ์ด๋™ + PushRouter.shared.handle(userInfo: userInfo) } else { LaunchSourceManager.shared.setSource(.localNotification) + // 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์€ ์˜ค๋Š˜ ํ•™์‹ ํ™”๋ฉด์œผ๋กœ ์ด๋™ + if DailyMenuNotificationScheduler.isDailyMenuNotification(identifier: notification.request.identifier) { + PushRouter.shared.handle(route: PushRoute(date: Date(), mealTime: nil, notificationId: nil)) + } } - // ํ•™์‹ ์•Œ๋ฆผ์ด๋ฉด ํ•ด๋‹น ๋‚ ์งœยท์‹œ๊ฐ„๋Œ€์˜ ํ•™์‹ ํ™”๋ฉด์œผ๋กœ ์ด๋™ - PushRouter.shared.handle(userInfo: userInfo) - completionHandler() } @@ -117,13 +123,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD userInfo["fcm_options"] != nil } - /// ํ‘ธ์‹œ ์•Œ๋ฆผ ๊ถŒํ•œ์„ ์š”์ฒญํ•˜๊ณ  ์„ค์ •์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. + /// ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ + private func isFavoritePush(_ userInfo: [AnyHashable: Any]) -> Bool { + (userInfo["type"] as? String) == "MENU_FAVORITE" + } + + /// ์•Œ๋ฆผ ๊ถŒํ•œยทํ† ๊ธ€ ์ƒํƒœ์— ๋งž๊ฒŒ ์•Œ๋ฆผ ์˜ˆ์•ฝ๊ณผ ์„œ๋ฒ„ ๋“ฑ๋ก์„ ๋งž์ถฅ๋‹ˆ๋‹ค. private func setupNotificationPermissions() { - // 11์‹œ ์•Œ๋ฆผ์€ ์„œ๋ฒ„ ํ‘ธ์‹œ๋กœ ์˜ฎ๊ฒจ, ์ด์ „ ๋ฒ„์ „์ด ์˜ˆ์•ฝํ•œ ๋กœ์ปฌ ์•Œ๋ฆผ์€ ์ง€์šด๋‹ค - NotificationManager.shared.removeLegacyDailyNotifications() // ์‹œ์Šคํ…œ ์•Œ๋ฆผ ๊ถŒํ•œ์€ ์•ฑ ์‹คํ–‰ ์‹œ ๋ฐ”๋กœ ๋ฌป์ง€ ์•Š๋Š”๋‹ค. ๊ฐ€์ž… ์งํ›„ ์•ˆ๋‚ด ๋‹ค์ด์–ผ๋กœ๊ทธ(`NotificationPermissionPrompt`)๋‚˜ // ๋งˆ์ดํŽ˜์ด์ง€ ํ† ๊ธ€์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ–ˆ์„ ๋•Œ ์š”์ฒญํ•œ๋‹ค - // ๊ถŒํ•œยทํ† ๊ธ€ ์ƒํƒœ์— ๋งž๊ฒŒ ์„œ๋ฒ„ ํ‘ธ์‹œ ํ† ํฐ ๋“ฑ๋ก/์‚ญ์ œ (๋กœ๊ทธ์ธ ์ƒํƒœ์ผ ๋•Œ๋งŒ ๋™์ž‘) + // ๊ถŒํ•œยทํ† ๊ธ€ ์ƒํƒœ์— ๋งž๊ฒŒ ํ‰์ผ 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์„ ์˜ˆ์•ฝ/์ทจ์†Œํ•˜๊ณ , ์„œ๋ฒ„ ํ‘ธ์‹œ ํ† ํฐ์„ ๋“ฑ๋ก/์‚ญ์ œํ•œ๋‹ค (ํ† ํฐ์€ ๋กœ๊ทธ์ธ ์ƒํƒœ์ผ ๋•Œ๋งŒ) + DailyMenuNotificationScheduler.sync() PushTokenManager.shared.sync() } @@ -210,6 +220,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD extension AppDelegate { // Foreground์—์„œ๋„ ์•Œ๋ฆผ์ด ๋ณด์ด๋„๋ก ์„ค์ • func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + let request = notification.request + if DailyMenuNotificationScheduler.isDailyMenuNotification(identifier: request.identifier) { + // 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ: ๊ฐ™์€ ๋‚  ์ฐœ ์•Œ๋ฆผ์„ ์ด๋ฏธ ๋ฐ›์•˜์œผ๋ฉด ์ค‘๋ณต์ด๋ผ ๋„์šฐ์ง€ ์•Š๋Š”๋‹ค + guard DailyMenuNotificationScheduler.shouldPresent() else { + completionHandler([]) + return + } + } else if isFavoritePush(request.content.userInfo) { + // ์ฐœ ์•Œ๋ฆผ์ด ๋จผ์ € ์™”์œผ๋ฉด ๊ฐ™์€ ๋‚  11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์€ ์ง€์šฐ๊ฑฐ๋‚˜ ๊ฑฐ๋ฅธ๋‹ค + DailyMenuNotificationScheduler.didReceiveFavoritePush() + } // ์•ฑ ์‚ฌ์šฉ ์ค‘์— ์•Œ๋ฆผ์ด ์˜ค๋ฉด ํ•™์‹ ํƒญ ์•Œ๋ฆผ ์•„์ด์ฝ˜์— ์ ์„ ๋„์šด๋‹ค NotificationCenter.default.post(name: .didReceivePushNotification, object: nil) completionHandler([.banner, .sound, .badge]) diff --git a/EATSSU/App/Sources/Utility/Literal/TextLiteral.swift b/EATSSU/App/Sources/Utility/Literal/TextLiteral.swift index 55970db3..929754f4 100644 --- a/EATSSU/App/Sources/Utility/Literal/TextLiteral.swift +++ b/EATSSU/App/Sources/Utility/Literal/TextLiteral.swift @@ -1210,6 +1210,14 @@ enum TextLiteral { // MARK: - Notification enum Notification { + /// ๐Ÿค” ์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ + static var dailyWeekdayNotificationTitle: String { + Localization.localized("notification.dailyWeekdayNotificationTitle", fallback: "๐Ÿค” ์˜ค๋Š˜ ๋ฐฅ ๋ญ ๋จน์ง€โ€ฆ") + } + /// ์˜ค๋Š˜์˜ ํ•™์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š”! + static var dailyWeekdayNotificationBody: String { + Localization.localized("notification.dailyWeekdayNotificationBody", fallback: "์˜ค๋Š˜์˜ ํ•™์‹์„ ํ™•์ธํ•ด๋ณด์„ธ์š”!") + } /// ์•Œ๋ฆผ ๊ถŒํ•œ ํ•„์š” static var permissionDeniedMessage: String { Localization.localized( diff --git a/EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift b/EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift new file mode 100644 index 00000000..0594cad5 --- /dev/null +++ b/EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift @@ -0,0 +1,48 @@ +// +// DailyMenuNotificationSchedulerTests.swift +// EATSSUUnitTests +// +// Created by ํ™ฉ์ƒํ™˜ on 11/9/26. +// + +import XCTest +import UserNotifications + +@testable import EATSSU_DEV + +/// ํ‰์ผ 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์˜ ์˜ˆ์•ฝ ์กฐ๊ฑดยท์‹๋ณ„์ž ๊ทœ์น™ ๊ฒ€์ฆ +final class DailyMenuNotificationSchedulerTests: XCTestCase { + + func test_ํ† ๊ธ€์ด_์ผœ์ ธ_์žˆ๊ณ _๊ถŒํ•œ์ด_ํ—ˆ์šฉ๋ผ์•ผ_์˜ˆ์•ฝํ•œ๋‹ค() { + XCTAssertTrue(DailyMenuNotificationScheduler.shouldSchedule(authorization: .authorized, isToggleOn: true)) + XCTAssertTrue(DailyMenuNotificationScheduler.shouldSchedule(authorization: .provisional, isToggleOn: true)) + XCTAssertFalse(DailyMenuNotificationScheduler.shouldSchedule(authorization: .authorized, isToggleOn: false)) + XCTAssertFalse(DailyMenuNotificationScheduler.shouldSchedule(authorization: .denied, isToggleOn: true)) + XCTAssertFalse(DailyMenuNotificationScheduler.shouldSchedule(authorization: .notDetermined, isToggleOn: true)) + } + + func test_์›”์š”์ผ๋ถ€ํ„ฐ_๊ธˆ์š”์ผ๊นŒ์ง€_ํ•˜๋‚˜์”ฉ_์˜ˆ์•ฝํ•œ๋‹ค() { + XCTAssertEqual(DailyMenuNotificationScheduler.weekdays, [2, 3, 4, 5, 6]) + XCTAssertEqual(DailyMenuNotificationScheduler.hour, 11) + } + + func test_์‹๋ณ„์ž๋Š”_์š”์ผ๋กœ_๊ตฌ๋ถ„๋˜๊ณ _์ ‘๋‘์‚ฌ๋กœ_ํŒ๋ณ„ํ•œ๋‹ค() throws { + var calendar = Calendar(identifier: .gregorian) + calendar.timeZone = try XCTUnwrap(TimeZone(identifier: "Asia/Seoul")) + // 2026-11-09 ์›”์š”์ผ + let monday = try XCTUnwrap(calendar.date(from: DateComponents(year: 2026, month: 11, day: 9, hour: 11))) + let identifier = DailyMenuNotificationScheduler.identifier(for: monday, calendar: calendar) + XCTAssertEqual(identifier, "weekdayNotification-2") + XCTAssertTrue(DailyMenuNotificationScheduler.isDailyMenuNotification(identifier: identifier)) + XCTAssertFalse(DailyMenuNotificationScheduler.isDailyMenuNotification(identifier: "menuFavorite-20261109")) + } + + func test_๊ฐ™์€_๋‚ _์ฐœ_์•Œ๋ฆผ์„_๋ฐ›์•˜์œผ๋ฉด_๋กœ์ปฌ_์•Œ๋ฆผ์„_๋„์šฐ์ง€_์•Š๋Š”๋‹ค() throws { + let today = Date() + let yesterday = try XCTUnwrap(Calendar.current.date(byAdding: .day, value: -1, to: today)) + DailyMenuNotificationScheduler.didReceiveFavoritePush(on: yesterday) + XCTAssertTrue(DailyMenuNotificationScheduler.shouldPresent(dailyNotificationOn: today)) + DailyMenuNotificationScheduler.didReceiveFavoritePush(on: today) + XCTAssertFalse(DailyMenuNotificationScheduler.shouldPresent(dailyNotificationOn: today)) + } +} From c9bb6a125c4f31b82059a91d7e06750ae50844b3 Mon Sep 17 00:00:00 2001 From: Hwang Sang Hwan <137605270+Hrepay@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:06:34 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[#487]=20=EC=95=8C=EB=A6=BC=20=ED=97=88?= =?UTF-8?q?=EC=9A=A9=20=EA=B7=9C=EC=B9=99=EC=9D=84=20NotificationManager?= =?UTF-8?q?=EB=A1=9C=20=EB=AA=A8=EC=9C=BC=EA=B3=A0,=20=EC=B0=9C=20?= =?UTF-8?q?=EC=95=8C=EB=A6=BC=20=EB=82=A0=EC=A7=9C=20=EA=B8=B0=EC=A4=80=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EC=A0=9C=EA=B1=B0=EC=99=80=20=EC=98=88?= =?UTF-8?q?=EC=95=BD=C2=B7=EC=B7=A8=EC=86=8C=20=EC=88=9C=EC=84=9C=20?= =?UTF-8?q?=EB=B3=B4=EC=9E=A5=EC=9D=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DailyMenuNotificationScheduler.swift | 42 +++++++++---------- .../Notification/NotificationManager.swift | 22 ++++++++++ .../App/Sources/Notification/PushRoute.swift | 8 +++- .../Notification/PushTokenManager.swift | 13 +----- .../Utility/Application/AppDelegate.swift | 20 ++++----- .../DailyMenuNotificationSchedulerTests.swift | 30 ++++++++++--- EATSSU/Tests/UnitTests/PushRouteTests.swift | 6 +++ 7 files changed, 89 insertions(+), 52 deletions(-) diff --git a/EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift b/EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift index a0455158..952f9ce6 100644 --- a/EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift +++ b/EATSSU/App/Sources/Notification/DailyMenuNotificationScheduler.swift @@ -26,7 +26,7 @@ enum DailyMenuNotificationScheduler { static let hour = 11 /// ๋‹น์ผ ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์„ ๋ฐ›์€ ๋‚ ์งœ(yyyyMMdd). ๊ฐ™์€ ๋‚  ๋กœ์ปฌ ์•Œ๋ฆผ์€ ๋„์šฐ์ง€ ์•Š๋Š”๋‹ค - private static let favoritePushReceivedDayKey = "dailyMenuNotification.favoritePushReceivedDay" + static let favoritePushReceivedDayKey = "dailyMenuNotification.favoritePushReceivedDay" // MARK: - Decision @@ -43,28 +43,19 @@ enum DailyMenuNotificationScheduler { identifier.hasPrefix(identifierPrefix) } - /// ์˜ˆ์•ฝ ์กฐ๊ฑด: ๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ํ† ๊ธ€ ON + ๊ธฐ๊ธฐ ๊ถŒํ•œ ํ—ˆ์šฉ - static func shouldSchedule(authorization: UNAuthorizationStatus, isToggleOn: Bool) -> Bool { - guard isToggleOn else { return false } - switch authorization { - case .authorized, .provisional, .ephemeral: - return true - case .denied, .notDetermined: - return false - @unknown default: - return false - } - } - // MARK: - Sync - /// ํ˜„์žฌ ํ† ๊ธ€ยท๊ถŒํ•œ ์ƒํƒœ์— ๋งž๊ฒŒ ์˜ˆ์•ฝํ•˜๊ฑฐ๋‚˜ ์ทจ์†Œํ•œ๋‹ค + /// ์ง„ํ–‰ ์ค‘์ธ ๋งž์ถค ์ž‘์—…. ๋‹ค์Œ ํ˜ธ์ถœ์€ ์ด ์ž‘์—…์ด ๋๋‚œ ๋’ค ์‹คํ–‰ํ•ด, ํ† ๊ธ€์„ ๋น ๋ฅด๊ฒŒ ๊ป๋‹ค ์ผœ๋„ ์˜ˆ์•ฝยท์ทจ์†Œ ์ˆœ์„œ๊ฐ€ ๋’ค๋ฐ”๋€Œ์ง€ ์•Š๊ฒŒ ํ•œ๋‹ค + @MainActor private static var syncTask: Task? + + /// ํ˜„์žฌ ํ† ๊ธ€ยท๊ถŒํ•œ ์ƒํƒœ(`NotificationManager.isNotificationAllowed`)์— ๋งž๊ฒŒ ์˜ˆ์•ฝํ•˜๊ฑฐ๋‚˜ ์ทจ์†Œํ•œ๋‹ค /// ํ˜ธ์ถœ ์‹œ์ : ์•ฑ ์‹คํ–‰, ์•Œ๋ฆผ ๊ถŒํ•œ ๊ฒฐ๊ณผ, ๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ํ† ๊ธ€ ๋ณ€๊ฒฝ, ์–ธ์–ด ๋ณ€๊ฒฝ(๋ฌธ๊ตฌ ๊ฐฑ์‹ ) + @MainActor static func sync() { - Task { - let settings = await UNUserNotificationCenter.current().notificationSettings() - let isToggleOn = UserDefaults.standard.bool(forKey: TextLiteral.MyPage.pushNotificationUserSettingKey) - if shouldSchedule(authorization: settings.authorizationStatus, isToggleOn: isToggleOn) { + let previous = syncTask + syncTask = Task { + await previous?.value + if await NotificationManager.shared.isNotificationEnabled() { await schedule() } else { cancel() @@ -96,17 +87,22 @@ enum DailyMenuNotificationScheduler { } } + /// ์˜ˆ์•ฝ์„ ์ง€์šฐ๊ณ , ์˜ค๋Š˜ ์ด๋ฏธ ๋–  ์žˆ๋Š” ์•Œ๋ฆผ๋„ ์•Œ๋ฆผ ์„ผํ„ฐ์—์„œ ๋‚ด๋ฆฐ๋‹ค static func cancel() { - UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: weekdays.map(identifier(weekday:))) + let identifiers = weekdays.map(identifier(weekday:)) + let center = UNUserNotificationCenter.current() + center.removePendingNotificationRequests(withIdentifiers: identifiers) + center.removeDeliveredNotifications(withIdentifiers: identifiers) } // MARK: - ์ฐœ ์•Œ๋ฆผ๊ณผ ์ค‘๋ณต ์ œ๊ฑฐ - /// ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์„ ๋ฐ›์•˜์„ ๋•Œ ํ˜ธ์ถœ. ๊ฐ™์€ ๋‚  ์ด๋ฏธ ๋–  ์žˆ๋Š” ๋กœ์ปฌ ์•Œ๋ฆผ์„ ์•Œ๋ฆผ ์„ผํ„ฐ์—์„œ ์ง€์šฐ๊ณ , - /// ์•„์ง ์•ˆ ๋–ด๋‹ค๋ฉด `shouldPresent`๊ฐ€ ๊ฑฐ๋ฅด๋„๋ก ๋‚ ์งœ๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค + /// ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์„ ๋ฐ›์•˜์„ ๋•Œ ํ˜ธ์ถœ. ์•Œ๋ฆผ์˜ ์‹๋‹จ ๋‚ ์งœ(`date`) ๊ธฐ์ค€์œผ๋กœ ๊ฐ™์€ ๋‚  ์ด๋ฏธ ๋–  ์žˆ๋Š” ๋กœ์ปฌ ์•Œ๋ฆผ์„ + /// ์•Œ๋ฆผ ์„ผํ„ฐ์—์„œ ์ง€์šฐ๊ณ , ์•„์ง ์•ˆ ๋–ด๋‹ค๋ฉด `shouldPresent`๊ฐ€ ๊ฑฐ๋ฅด๋„๋ก ๋‚ ์งœ๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค /// + /// ๋ฐ›์€ ์‹œ๊ฐ์ด ์•„๋‹ˆ๋ผ ์•Œ๋ฆผ์˜ ๋‚ ์งœ๋ฅผ ์จ์•ผ, ์–ด์ œ ์˜จ ํ‘ธ์‹œ๋ฅผ ์˜ค๋Š˜ ๋ˆŒ๋ €์„ ๋•Œ ์˜ค๋Š˜ ๋กœ์ปฌ ์•Œ๋ฆผ๊นŒ์ง€ ๊ฑฐ๋ฅด์ง€ ์•Š๋Š”๋‹ค. /// ์•ฑ์ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์žˆ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ํ‘ธ์‹œ๋ฅผ ๋ˆ„๋ฅด์ง€ ์•Š์œผ๋ฉด ์ด ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์•„ ๋‘ ์•Œ๋ฆผ์ด ํ•จ๊ป˜ ๋‚จ์„ ์ˆ˜ ์žˆ๋‹ค - static func didReceiveFavoritePush(on date: Date = Date()) { + static func didReceiveFavoritePush(on date: Date) { UserDefaults.standard.set(DateFormatter.serverDay.string(from: date), forKey: favoritePushReceivedDayKey) UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [identifier(for: date)]) } diff --git a/EATSSU/App/Sources/Notification/NotificationManager.swift b/EATSSU/App/Sources/Notification/NotificationManager.swift index 7ca83505..fef36d13 100644 --- a/EATSSU/App/Sources/Notification/NotificationManager.swift +++ b/EATSSU/App/Sources/Notification/NotificationManager.swift @@ -26,6 +26,28 @@ class NotificationManager { return await UNUserNotificationCenter.current().notificationSettings() } + /// ์•Œ๋ฆผ์„ ๋ณด๋‚ด๋„ ๋˜๋Š” ์ƒํƒœ์ธ์ง€: ๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ํ† ๊ธ€ ON + ๊ธฐ๊ธฐ ์•Œ๋ฆผ ๊ถŒํ•œ ํ—ˆ์šฉ + /// ์„œ๋ฒ„ ํ‘ธ์‹œ ํ† ํฐ ๋“ฑ๋ก(`PushTokenManager`)๊ณผ 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ(`DailyMenuNotificationScheduler`)์ด ๊ฐ™์€ ๊ทœ์น™์„ ์“ด๋‹ค + static func isNotificationAllowed(authorization: UNAuthorizationStatus, isToggleOn: Bool) -> Bool { + guard isToggleOn else { return false } + switch authorization { + case .authorized, .provisional, .ephemeral: + return true + case .denied, .notDetermined: + return false + @unknown default: + return false + } + } + + /// ํ˜„์žฌ ํ† ๊ธ€ยท๊ถŒํ•œ ์ƒํƒœ๋กœ `isNotificationAllowed`๋ฅผ ํŒ๋‹จํ•œ๋‹ค + func isNotificationEnabled() async -> Bool { + let isToggleOn = UserDefaults.standard.bool(forKey: TextLiteral.MyPage.pushNotificationUserSettingKey) + guard isToggleOn else { return false } + let settings = await checkNotificationSetting() + return Self.isNotificationAllowed(authorization: settings.authorizationStatus, isToggleOn: isToggleOn) + } + /// ์•Œ๋ฆผ ๊ถŒํ•œ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด ํ† ๊ธ€์˜ ๋‹ค์Œ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜. ์„œ๋ฒ„ ๋ฐ˜์˜์€ `PushTokenManager.sync()`๊ฐ€ ํ•œ๋‹ค func handleNotificationToggle(currentState: Bool) async throws -> Bool { let settings = await checkNotificationSetting() diff --git a/EATSSU/App/Sources/Notification/PushRoute.swift b/EATSSU/App/Sources/Notification/PushRoute.swift index 307902e1..6afd9e79 100644 --- a/EATSSU/App/Sources/Notification/PushRoute.swift +++ b/EATSSU/App/Sources/Notification/PushRoute.swift @@ -36,7 +36,13 @@ struct PushRoute { self.notificationId = (userInfo["notificationId"] as? String).flatMap(Int.init) } - private static let supportedTypes: Set = ["MENU_FAVORITE", "DAILY_MENU"] + private static let favoriteType = "MENU_FAVORITE" + private static let supportedTypes: Set = [favoriteType, "DAILY_MENU"] + + /// ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์ธ์ง€. ์•Œ๋ฆผ ๋‚ ์งœ๊ฐ€ ์žˆ์–ด์•ผ ํ•˜๋ฏ€๋กœ ํ•ด์„์— ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ๋งŒ true + static func isFavorite(userInfo: [AnyHashable: Any]) -> Bool { + (userInfo["type"] as? String) == favoriteType && PushRoute(userInfo: userInfo) != nil + } } /// ์•Œ๋ฆผ์„ ๋ˆ„๋ฅธ ์‹œ์ ์— ํ™”๋ฉด์ด ์ค€๋น„๋ผ ์žˆ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์–ด(์•ฑ ์‹คํ–‰ ์งํ›„ ์Šคํ”Œ๋ž˜์‹œยท๋กœ๊ทธ์ธ ๋“ฑ) ์ด๋™ํ•  ๊ณณ์„ ๋ณด๊ด€ํ–ˆ๋‹ค๊ฐ€ ์ ์šฉํ•œ๋‹ค diff --git a/EATSSU/App/Sources/Notification/PushTokenManager.swift b/EATSSU/App/Sources/Notification/PushTokenManager.swift index 9122a7a4..df8fb99c 100644 --- a/EATSSU/App/Sources/Notification/PushTokenManager.swift +++ b/EATSSU/App/Sources/Notification/PushTokenManager.swift @@ -174,17 +174,8 @@ final class PushTokenManager { // MARK: - Private - /// ๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ํ† ๊ธ€ ON + ๊ธฐ๊ธฐ ์•Œ๋ฆผ ๊ถŒํ•œ ํ—ˆ์šฉ + /// ๋งˆ์ดํŽ˜์ด์ง€ ์•Œ๋ฆผ ํ† ๊ธ€ ON + ๊ธฐ๊ธฐ ์•Œ๋ฆผ ๊ถŒํ•œ ํ—ˆ์šฉ (๊ทœ์น™์€ `NotificationManager.isNotificationAllowed`) private static func isNotificationEnabled() async -> Bool { - guard UserDefaults.standard.bool(forKey: TextLiteral.MyPage.pushNotificationUserSettingKey) else { return false } - let settings = await UNUserNotificationCenter.current().notificationSettings() - switch settings.authorizationStatus { - case .authorized, .provisional, .ephemeral: - return true - case .denied, .notDetermined: - return false - @unknown default: - return false - } + await NotificationManager.shared.isNotificationEnabled() } } diff --git a/EATSSU/App/Sources/Utility/Application/AppDelegate.swift b/EATSSU/App/Sources/Utility/Application/AppDelegate.swift index 89c10835..2889df3a 100644 --- a/EATSSU/App/Sources/Utility/Application/AppDelegate.swift +++ b/EATSSU/App/Sources/Utility/Application/AppDelegate.swift @@ -53,11 +53,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD // FCM ์•Œ๋ฆผ์ธ์ง€ ํ™•์ธ if isFCMNotification(userInfo) { LaunchSourceManager.shared.setSource(.remoteNotification) - if isFavoritePush(userInfo) { - DailyMenuNotificationScheduler.didReceiveFavoritePush() + // ํ•™์‹ ์•Œ๋ฆผ์ด๋ฉด ํ•ด๋‹น ๋‚ ์งœยท์‹œ๊ฐ„๋Œ€์˜ ํ•™์‹ ํ™”๋ฉด์œผ๋กœ ์ด๋™. ์ฐœ ์•Œ๋ฆผ์ด๋ฉด ๊ฐ™์€ ๋‚  11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์€ ์ง€์šด๋‹ค + if let route = PushRoute(userInfo: userInfo) { + if PushRoute.isFavorite(userInfo: userInfo) { + DailyMenuNotificationScheduler.didReceiveFavoritePush(on: route.date) + } + PushRouter.shared.handle(route: route) } - // ํ•™์‹ ์•Œ๋ฆผ์ด๋ฉด ํ•ด๋‹น ๋‚ ์งœยท์‹œ๊ฐ„๋Œ€์˜ ํ•™์‹ ํ™”๋ฉด์œผ๋กœ ์ด๋™ - PushRouter.shared.handle(userInfo: userInfo) } else { LaunchSourceManager.shared.setSource(.localNotification) // 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์€ ์˜ค๋Š˜ ํ•™์‹ ํ™”๋ฉด์œผ๋กœ ์ด๋™ @@ -123,11 +125,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD userInfo["fcm_options"] != nil } - /// ์ฐœ ์•Œ๋ฆผ(์„œ๋ฒ„ ํ‘ธ์‹œ)์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ - private func isFavoritePush(_ userInfo: [AnyHashable: Any]) -> Bool { - (userInfo["type"] as? String) == "MENU_FAVORITE" - } - /// ์•Œ๋ฆผ ๊ถŒํ•œยทํ† ๊ธ€ ์ƒํƒœ์— ๋งž๊ฒŒ ์•Œ๋ฆผ ์˜ˆ์•ฝ๊ณผ ์„œ๋ฒ„ ๋“ฑ๋ก์„ ๋งž์ถฅ๋‹ˆ๋‹ค. private func setupNotificationPermissions() { // ์‹œ์Šคํ…œ ์•Œ๋ฆผ ๊ถŒํ•œ์€ ์•ฑ ์‹คํ–‰ ์‹œ ๋ฐ”๋กœ ๋ฌป์ง€ ์•Š๋Š”๋‹ค. ๊ฐ€์ž… ์งํ›„ ์•ˆ๋‚ด ๋‹ค์ด์–ผ๋กœ๊ทธ(`NotificationPermissionPrompt`)๋‚˜ @@ -227,9 +224,10 @@ extension AppDelegate { completionHandler([]) return } - } else if isFavoritePush(request.content.userInfo) { + } else if PushRoute.isFavorite(userInfo: request.content.userInfo), + let route = PushRoute(userInfo: request.content.userInfo) { // ์ฐœ ์•Œ๋ฆผ์ด ๋จผ์ € ์™”์œผ๋ฉด ๊ฐ™์€ ๋‚  11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์€ ์ง€์šฐ๊ฑฐ๋‚˜ ๊ฑฐ๋ฅธ๋‹ค - DailyMenuNotificationScheduler.didReceiveFavoritePush() + DailyMenuNotificationScheduler.didReceiveFavoritePush(on: route.date) } // ์•ฑ ์‚ฌ์šฉ ์ค‘์— ์•Œ๋ฆผ์ด ์˜ค๋ฉด ํ•™์‹ ํƒญ ์•Œ๋ฆผ ์•„์ด์ฝ˜์— ์ ์„ ๋„์šด๋‹ค NotificationCenter.default.post(name: .didReceivePushNotification, object: nil) diff --git a/EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift b/EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift index 0594cad5..52df315a 100644 --- a/EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift +++ b/EATSSU/Tests/UnitTests/DailyMenuNotificationSchedulerTests.swift @@ -13,12 +13,30 @@ import UserNotifications /// ํ‰์ผ 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ์˜ ์˜ˆ์•ฝ ์กฐ๊ฑดยท์‹๋ณ„์ž ๊ทœ์น™ ๊ฒ€์ฆ final class DailyMenuNotificationSchedulerTests: XCTestCase { - func test_ํ† ๊ธ€์ด_์ผœ์ ธ_์žˆ๊ณ _๊ถŒํ•œ์ด_ํ—ˆ์šฉ๋ผ์•ผ_์˜ˆ์•ฝํ•œ๋‹ค() { - XCTAssertTrue(DailyMenuNotificationScheduler.shouldSchedule(authorization: .authorized, isToggleOn: true)) - XCTAssertTrue(DailyMenuNotificationScheduler.shouldSchedule(authorization: .provisional, isToggleOn: true)) - XCTAssertFalse(DailyMenuNotificationScheduler.shouldSchedule(authorization: .authorized, isToggleOn: false)) - XCTAssertFalse(DailyMenuNotificationScheduler.shouldSchedule(authorization: .denied, isToggleOn: true)) - XCTAssertFalse(DailyMenuNotificationScheduler.shouldSchedule(authorization: .notDetermined, isToggleOn: true)) + /// ์ฐœ ์•Œ๋ฆผ ์ˆ˜์‹  ๋‚ ์งœ๋Š” ์‹ค์ œ UserDefaults์— ์ €์žฅ๋˜๋ฏ€๋กœ ํ…Œ์ŠคํŠธ ์ „ ๊ฐ’์„ ๋ณด๊ด€ํ–ˆ๋‹ค๊ฐ€ ๋˜๋Œ๋ฆฐ๋‹ค + private let favoritePushReceivedDayKey = DailyMenuNotificationScheduler.favoritePushReceivedDayKey + private var savedFavoritePushReceivedDay: String? + + override func setUp() { + super.setUp() + savedFavoritePushReceivedDay = UserDefaults.standard.string(forKey: favoritePushReceivedDayKey) + } + + override func tearDown() { + if let savedFavoritePushReceivedDay { + UserDefaults.standard.set(savedFavoritePushReceivedDay, forKey: favoritePushReceivedDayKey) + } else { + UserDefaults.standard.removeObject(forKey: favoritePushReceivedDayKey) + } + super.tearDown() + } + + func test_ํ† ๊ธ€์ด_์ผœ์ ธ_์žˆ๊ณ _๊ถŒํ•œ์ด_ํ—ˆ์šฉ๋ผ์•ผ_์•Œ๋ฆผ์„_๋ณด๋‚ธ๋‹ค() { + XCTAssertTrue(NotificationManager.isNotificationAllowed(authorization: .authorized, isToggleOn: true)) + XCTAssertTrue(NotificationManager.isNotificationAllowed(authorization: .provisional, isToggleOn: true)) + XCTAssertFalse(NotificationManager.isNotificationAllowed(authorization: .authorized, isToggleOn: false)) + XCTAssertFalse(NotificationManager.isNotificationAllowed(authorization: .denied, isToggleOn: true)) + XCTAssertFalse(NotificationManager.isNotificationAllowed(authorization: .notDetermined, isToggleOn: true)) } func test_์›”์š”์ผ๋ถ€ํ„ฐ_๊ธˆ์š”์ผ๊นŒ์ง€_ํ•˜๋‚˜์”ฉ_์˜ˆ์•ฝํ•œ๋‹ค() { diff --git a/EATSSU/Tests/UnitTests/PushRouteTests.swift b/EATSSU/Tests/UnitTests/PushRouteTests.swift index d7a4de23..e36c2d7f 100644 --- a/EATSSU/Tests/UnitTests/PushRouteTests.swift +++ b/EATSSU/Tests/UnitTests/PushRouteTests.swift @@ -29,6 +29,12 @@ final class PushRouteTests: XCTestCase { XCTAssertNil(route.mealTime) } + func test_์ฐœ_์•Œ๋ฆผ_์—ฌ๋ถ€๋Š”_ํƒ€์ž…๊ณผ_๋‚ ์งœ๊ฐ€_๋ชจ๋‘_๋งž์•„์•ผ_ํ•œ๋‹ค() { + XCTAssertTrue(PushRoute.isFavorite(userInfo: ["type": "MENU_FAVORITE", "date": "20261109", "timePart": "LUNCH"])) + XCTAssertFalse(PushRoute.isFavorite(userInfo: ["type": "DAILY_MENU", "date": "20261109"])) + XCTAssertFalse(PushRoute.isFavorite(userInfo: ["type": "MENU_FAVORITE"])) + } + func test_์•Œ_์ˆ˜_์—†๋Š”_ํƒ€์ž…์ด๋‚˜_์ž˜๋ชป๋œ_๋‚ ์งœ๋Š”_๋ฌด์‹œํ•œ๋‹ค() { XCTAssertNil(PushRoute(userInfo: ["type": "PROMOTION", "date": "20261005"])) XCTAssertNil(PushRoute(userInfo: ["type": "MENU_FAVORITE", "date": "2026-10-05"])) From e05a0685bd5d911c130942f554f68a2dbfc0acff Mon Sep 17 00:00:00 2001 From: Hwang Sang Hwan <137605270+Hrepay@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:09:37 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[#487]=20=ED=8F=AC=EA=B7=B8=EB=9D=BC?= =?UTF-8?q?=EC=9A=B4=EB=93=9C=20=EB=B3=B5=EA=B7=80=20=EC=8B=9C=2011?= =?UTF-8?q?=EC=8B=9C=20=EB=A1=9C=EC=BB=AC=20=EC=95=8C=EB=A6=BC=20=EC=98=88?= =?UTF-8?q?=EC=95=BD=EA=B3=BC=20=ED=91=B8=EC=8B=9C=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=EC=9D=84=20=EB=8B=A4=EC=8B=9C=20=EB=A7=9E?= =?UTF-8?q?=EC=B6=94=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EATSSU/App/Sources/Utility/Application/SceneDelegate.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/EATSSU/App/Sources/Utility/Application/SceneDelegate.swift b/EATSSU/App/Sources/Utility/Application/SceneDelegate.swift index 87d67a6d..ff6164ff 100644 --- a/EATSSU/App/Sources/Utility/Application/SceneDelegate.swift +++ b/EATSSU/App/Sources/Utility/Application/SceneDelegate.swift @@ -65,6 +65,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { func sceneWillEnterForeground(_ scene: UIScene) { // ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ํฌ๊ทธ๋ผ์šด๋“œ๋กœ ์ „ํ™˜ ์‹œ ํ•„์š”ํ•œ ์ž‘์—… ์ˆ˜ํ–‰ TokenManager.refreshIfNeededAsync() + // ์„ค์ • ์•ฑ์—์„œ ์•Œ๋ฆผ ๊ถŒํ•œ์„ ๋ฐ”๊พธ๊ณ  ๋Œ์•„์˜จ ๊ฒฝ์šฐ๋ฅผ ๋ฐ˜์˜ํ•ด 11์‹œ ๋กœ์ปฌ ์•Œ๋ฆผ ์˜ˆ์•ฝ๊ณผ ์„œ๋ฒ„ ํ‘ธ์‹œ ํ† ํฐ ๋“ฑ๋ก์„ ๋‹ค์‹œ ๋งž์ถ˜๋‹ค + DailyMenuNotificationScheduler.sync() + PushTokenManager.shared.sync() } func sceneDidBecomeActive(_ scene: UIScene) {