0

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

Johny D Good
  • 427
  • 1
  • 8
  • 26
  • Possible duplicate of [didReceiveRemoteNotification not called , iOS 10](https://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10) – mfaani Aug 20 '17 at 09:44
  • not a duplicate. – Johny D Good Aug 20 '17 at 12:49
  • Are you setting content-available = 1 in your push payload? If not then the push will not be delivered to your app when it is suspended. – Paulw11 Aug 20 '17 at 12:52
  • I have not. will my didReceiveRemoteNotification work as is when app is closed if I add this setting? – Johny D Good Aug 20 '17 at 13:15
  • 1
    @Paulw11 see *his* comment. Johny you must use the AT sign if there are more than 1 people commenting others Paulw won't know. Johny you're using the **deprecated** function. you must instead use: [`application(_:didReceiveRemoteNotification:fetchCompletionHandler:)`](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application). Additionally as Paul said you must set the `content-available` to `1`. For more see [here](https://stackoverflow.com/questions/42275060/what-is-difference-between-remote-notification-and-silent-notification-in-ios/42302369#42302369) – mfaani Aug 20 '17 at 14:05

0 Answers0