My app does the following when the app is opened from a remote notification. Basically, it saves article_id in UserDefaults so that I can use it when the app is launched:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
let userInfo = response.notification.request.content.userInfo
if let aps = userInfo["aps"] as? [String: AnyObject] {
let article_id = aps["article_id"]
UserDefaults.standard.set(article_id, forKey: "notification_article_id")
}
completionHandler()
}
}
However, this only works if the app is completely closed. If the app remains in the background and the user clicks the notification (e.g. from the lock screen), the function above will not be triggered. Thus, it will not save the data into my UserDefaults. Does any one know how to trigger a similar action in this situation? Thanks in advance!
