Open custom url when user clicks push notifications in swift

2 answers

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!

Received answers to this question:
The best answer according to the author of the question:

In a simple way, just find your view controller in view controller's hierarchy, pass "ntid" value to stored property and call "test" method.

first define enum as

enum Identifiers {   static let viewAction = "cat1"   static let newsCategory = "cat2" } 

then in your UNUserNotificationCenterDelegate

extension AppDelegate: UNUserNotificationCenterDelegate {   func userNotificationCenter(     _ center: UNUserNotificationCenter,     didReceive response: UNNotificationResponse,     withCompletionHandler completionHandler: @escaping () -> Void) {      // 1     let userInfo = response.notification.request.content.userInfo      // 2     if let aps = userInfo["aps"] as? [String: AnyObject],       let newsItem = NewsItem.makeNewsItem(aps) {        (window?.rootViewController as? UITabBarController)?.selectedIndex = 1        // 3       if response.actionIdentifier == Identifiers.viewAction,         let url = URL(string: newsItem.link) {         let safari = SFSafariViewController(url: url)         window?.rootViewController?.present(safari, animated: true,                                             completion: nil)       }     }      // 4     completionHandler()   } } 

add category in your push service like that

{   "aps": {     "alert": "messaga",     "sound": "default",     "link_url": "www.google.com",     "category": "cat1",    } } 

it will check the aps type then push view controller if category type 1 or 2. you can add many as you want by doing this way