Avoid repeating push messages after opening notifications

This commit is contained in:
Rafal Kolarz 2026-11-07 23:28:24 +00:00
parent aab402f612
commit a8525a5075
3 changed files with 14 additions and 2 deletions

View File

@ -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";

View File

@ -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()
}

View File

@ -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()