Open custom url when user clicks push notifications in swift
I am trying to develop an IOS / Swift application with WKWebView and Firebase Push. I want to send notifications using PHP and when a user clicks on the notification to open a custom URL in webview. default URL is
let url = URL(string: "https://mywebsite.com/index.php?token=\(token)")!
and i want to pass in this url an id like this
let url = URL(string: "https://client.gazduire.net/app3/index.php?token=\(token)&ntid=(id that is send with push notification, ex.:1)")!
My code in appdelegate.swift
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print(userInfo) let notificationName = Notification.Name("test") NotificationCenter.default.post(name: notificationName, object: nil,userInfo: userInfo) let ntid = userInfo["ntid"] as! String print("\(ntid)") completionHandler()
}
ViewController.swift
@objc func test() { let appDelegate = UIApplication.shared.delegate as! AppDelegate let ntid = appDelegate.ntid let token = Messaging.messaging().fcmToken guard let url = URL(string: "https://mywebsite.com/index.php?token=\(token ?? "")&ntid=\(ntid)") else { print("Invalid URL") return } let request = URLRequest(url: url) webView.load(request) }
Can i send the ntid(ntid is recived and is printed ok) from appdelegate to viewcontroller when user tap push notification?Thank You!