From a8525a507587116f73dd7d18ee27f84bf862e0c9 Mon Sep 17 00:00:00 2001 From: Rafal Kolarz Date: Sat, 7 Nov 2026 23:28:24 +0000 Subject: [PATCH] Avoid repeating push messages after opening notifications --- Kasztelan.xcodeproj/project.pbxproj | 4 ++-- Kasztelan/KasztelanApp.swift | 2 ++ Kasztelan/NotificationService.swift | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Kasztelan.xcodeproj/project.pbxproj b/Kasztelan.xcodeproj/project.pbxproj index 264956d..c61cb68 100644 --- a/Kasztelan.xcodeproj/project.pbxproj +++ b/Kasztelan.xcodeproj/project.pbxproj @@ -294,7 +294,7 @@ CODE_SIGN_STYLE = Automatic; CODE_SIGN_ENTITLEMENTS = Kasztelan/Kasztelan.entitlements; APS_ENVIRONMENT = development; - CURRENT_PROJECT_VERSION = 17; + CURRENT_PROJECT_VERSION = 18; GENERATE_INFOPLIST_FILE = NO; INFOPLIST_FILE = Kasztelan/Info.plist; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.food-and-drink"; @@ -320,7 +320,7 @@ CODE_SIGN_STYLE = Automatic; CODE_SIGN_ENTITLEMENTS = Kasztelan/Kasztelan.entitlements; APS_ENVIRONMENT = production; - CURRENT_PROJECT_VERSION = 17; + CURRENT_PROJECT_VERSION = 18; GENERATE_INFOPLIST_FILE = NO; INFOPLIST_FILE = Kasztelan/Info.plist; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.food-and-drink"; diff --git a/Kasztelan/KasztelanApp.swift b/Kasztelan/KasztelanApp.swift index 01bbb73..5846d9d 100644 --- a/Kasztelan/KasztelanApp.swift +++ b/Kasztelan/KasztelanApp.swift @@ -105,6 +105,7 @@ final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCent willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void ) { + NotificationService.recordReceivedPush(notification.request.content) completionHandler([.banner, .list, .sound]) } @@ -113,6 +114,7 @@ final class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCent didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void ) { + NotificationService.recordReceivedPush(response.notification.request.content) NotificationCenter.default.post(name: .kasztelanOpenMessages, object: nil) completionHandler() } diff --git a/Kasztelan/NotificationService.swift b/Kasztelan/NotificationService.swift index 9ee3c8a..320decc 100644 --- a/Kasztelan/NotificationService.swift +++ b/Kasztelan/NotificationService.swift @@ -40,7 +40,17 @@ enum NotificationService { try? await APIClient().unregisterPush(deviceToken: deviceToken, token: session) } + static func recordReceivedPush(_ content: UNNotificationContent) { + guard let id = content.userInfo["message_id"] as? Int, id > 0 else { return } + var received = UserDefaults.standard.array(forKey: "kasztelan.push.receivedIDs") as? [Int] ?? [] + received.removeAll { $0 == id } + received.append(id) + UserDefaults.standard.set(Array(received.suffix(100)), forKey: "kasztelan.push.receivedIDs") + } + static func show(_ message: KasztelanMessage) async { + let received = UserDefaults.standard.array(forKey: "kasztelan.push.receivedIDs") as? [Int] ?? [] + guard !received.contains(message.id) else { return } let settings = await UNUserNotificationCenter.current().notificationSettings() guard settings.authorizationStatus == .authorized || settings.authorizationStatus == .provisional else { return } let delivered = await UNUserNotificationCenter.current().deliveredNotifications()