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()
}
2
Answers
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.
You can also use this method and fetch your notification.
let me know if it’s work.