I have the following fund in app delegate:
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
// Print notification payload data
print("Push notification received: \(data)")
let dictPayload = data as NSDictionary
if let data = dictPayload.value(forKey: "aps") as? NSDictionary {
let link = data.value(forKey: "Link") as? String
print("link: \(link!)")
UserDefaults.standard.set(link, forKey: "link_from_push")
UserDefaults.standard.synchronize()
}
Basically, the push notification contains data with a link in an app. When user opens app, I need to log user on automatically, do a bunch of stuff and then open the link.
My code works fine when app is open. I receive the push, extract the link no problem.
On the other hand when app is closed or in the background, didReceiveRemoteNotification is not being called, and i am guessing it isn't being called because UserDefaults.standard forKey: "link_from_push") is not being set.
How can I set the value of link as UserDefaults.standard forKey:"link_from_push" when app is in the background or completely closed. Is there another method I need to use for that instead of didReceiveRemoteNotification
This is not a duplicate since the answer in the question: didReceiveRemoteNotification not called , iOS 10 addresses obj c and not swift, and the answer given for swift did not work.
please help
thank you