Is there a way to dismiss only the selected push notification? (Swift)

26 Views Asked by At

For instance, if there were 4 push notifications received and the user selected the 2nd push notification to enter the app, is there a way to leave the remaining 1st, 3rd, and 4th push notifications in the notification center? My code is as follows.

@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
    private var isPushedBackground = true
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        HTTPCookieStorage.shared.cookieAcceptPolicy = HTTPCookie.AcceptPolicy.always
        //FireBase Setting
        FirebaseApp.configure()
        UNUserNotificationCenter.current().delegate = self
        Messaging.messaging().delegate = self
        application.registerForRemoteNotifications()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
}

extension AppDelegate: MessagingDelegate, UNUserNotificationCenterDelegate {
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().setAPNSToken(deviceToken,
                                           type: .sandbox)
    }
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        guard let firebaseToken = fcmToken else {
            return
        }
        UserDefaults.standard.setValue(firebaseToken, forKey: UserDefaultsKeys.fcmToken.keyString)
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.list, .badge, .sound, .banner])
        self.isPushedBackground = false
    }
    
    //백그라운드 작동
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        guard let pushId = userInfo["pushId"] as? String else {
            return
        }
        
        let pushAlarmInfo = Notification.PushAlarm.PushAlarmInfo(pushId: pushId,
                                                                 isPushedBackground: self.isPushedBackground)
        Notification.PushAlarm.postPushAlarm(pushAlarmInfo: pushAlarmInfo)
        completionHandler()
    }
    
    func application(_ application: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        completionHandler (.newData)
    }
}

ChatGPT provided me with such a code, but it didn't work. The code is as follows.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        let center = UNUserNotificationCenter.current()
        center.getDeliveredNotifications { (notifications) in
            // 푸시 알림을 확인 처리하고, 터치한 알림은 사라지지만 나머지는 유지
            completionHandler(.newData)
        }
    }
0

There are 0 best solutions below