-0.1 C
New York
Sunday, January 12, 2025

ios – How save knowledge in UserDefaults when the consumer press UNNotificationAction and the app is closed?


If the app is opened or within the background, userNotificationCenter get triggered and the information get saved simply high-quality, but when the app is closed, when the notification comes, and I press one of many motion buttons, nothing occurs, the information does not get saved.

That is the code:

UNUserNotificationCenter.present().delegate = self
        
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound, .criticalAlert]
        UNUserNotificationCenter.present().requestAuthorization(choices: authOptions, completionHandler: { (granted, error) in
                if granted {
                    
                }
            })
        
        let PKW = UNNotificationAction(identifier: "PKW", title: "Komme per PKW", choices: [])
        let Fuß = UNNotificationAction(identifier: "Fuß", title: "Komme per Fuß", choices: [])
        let Rad = UNNotificationAction(identifier: "Rad", title: "Komme per Rad", choices: [])
        let nicht = UNNotificationAction(identifier: "Nicht", title: "Komme per Komme nicht", choices: [])
        let class = UNNotificationCategory(identifier: "ComeBy", actions: [PKW, Fuß, Rad, nicht], intentIdentifiers: [], choices: [])
        UNUserNotificationCenter.present().setNotificationCategories()

        utility.registerForRemoteNotifications()
func userNotificationCenter(_ heart: UNUserNotificationCenter,
                                willPresent: UNNotification,
                                withCompletionHandler: @escaping (UNNotificationPresentationOptions)->()) {
        withCompletionHandler([.alert, .sound, .badge])
    }
    
    func userNotificationCenter(_ heart: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler: @escaping ()->()) {
        let eventData = response.actionIdentifier
        change eventData {
        case "PKW", "Fuß", "Rad", "Nicht":
            UserDefaults.normal.set(eventData, forKey: prefixedKey(UserDefaultsKeys.comeBy.key))
        default:
            break
        }

        
        withCompletionHandler()
    }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles