'iOS How to open Push Notification "link" when app is killed in swift

I want to click push notification and open "link" when app is Killed Where should I control when the app is killed? And how can I open the "link"?

Also works in foreground and background. I'm using this code.

I'd be really grateful if you could help,Thanks!

override func viewDidLoad() {
    super.viewDidLoad()
    print("viewDidLoad")

    // background
    NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { (Notification) in
        
        let userDefault = UserDefaults.standard
        let pushUrl:String? = userDefault.string(forKey: "PUSH_URL")
        

        if(pushUrl != nil){
            NSLog(pushUrl!)
            
            let myUrl = URL(string: pushUrl!)
            let myRequest = URLRequest(url: myUrl!)
            self.webView.load(myRequest)
            
            userDefault.removeObject(forKey: "PUSH_URL")
            userDefault.synchronize()
            
            
        }
     }
     // foreground
     NotificationCenter.default.addObserver(self, selector: #selector(didRecieveTestNotification(_:)), name: NSNotification.Name("TestNotification"), object: nil)

}

@objc func didRecieveTestNotification(_ notification: Notification) {
    print("Test Notification")
    let userDefault = UserDefaults.standard
    let pushUrl:String? = userDefault.string(forKey: "PUSH_URL")
    
    if(pushUrl != nil){
        NSLog(pushUrl!)
        
        let myUrl = URL(string: pushUrl!)
        let myRequest = URLRequest(url: myUrl!)
        self.webView.load(myRequest)
        
        userDefault.removeObject(forKey: "PUSH_URL")
        userDefault.synchronize()
        
        
    }


Solution 1:[1]

There is no control for push notifications while App is killed, after clicking on Notification

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

the method will be called and you can open your link, according to your notification type.

Solution 2:[2]

You can also use this method and fetch your notification.

    func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification, withCompletionHandler _: @escaping (UNNotificationPresentationOptions) -> Void) {}

let me know if it's work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vaibhav Khatri
Solution 2 Speedy