Skip to content

How to open application webView on message tap (Old)

Davor Komušanac edited this page Dec 5, 2022 · 1 revision

1. Initialize tap handler when user taps on the received notification

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(MessagesManager.handleTapNotification),
                                           name: NSNotification.Name(rawValue: MMNotificationMessageTapped),
                                           object: nil)

Link to example code

2. Handle tap event and open in-app WebView

    func handleTapNotification(_ notification: Notification) {
        guard let userInfo = notification.userInfo,
        let message = userInfo[MMNotificationKeyMessage] as? MM_MTMessage else {
            return
        }
 
        // checking if we have "url" in custom payload
        guard let url = message.customPayload?["url"] as? String else {
            return
        }
 
        let webViewController = WebViewController()
        webViewController.url = url
        UIApplication.shared.keyWindow?.visibleViewController?.present(webViewController, animated: true)
    }

Link to example code

3. Implement ViewContoller with UIWebView component

    class WebViewController: UIViewController, UIWebViewDelegate {
        private static let toolbarHeight: CGFloat = 64
        public var url: String?
 
        override func viewDidLoad() {
            super.viewDidLoad()
 
            guard let url = url,
                let targetUrl = URL(string: url)  else {
                    print("URL is missing")
                    return
            }       
 
            // init toolbar
            let toolbarView = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: WebViewController.toolbarHeight))
            toolbarView.autoresizingMask = [.flexibleWidth]
            toolbarView.setItems([UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(close))],
                                 animated: true)
 
            view.addSubview(toolbarView)
 
            // init web-view
            let webView = UIWebView()
            webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            webView.frame = CGRect(x: 0,
                                   y: WebViewController.toolbarHeight,
                                   width: view.frame.width,
                                   height: view.frame.height - WebViewController.toolbarHeight)
 
            webView.loadRequest(URLRequest(url: targetUrl))
            webView.delegate = self
            view.addSubview(webView)
        }
 
        func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
            print("Error occurred while page loading: \(error)")
        }
 
        func webViewDidStartLoad(_ webView: UIWebView) {
            print("Page loaded successfully")
        }
 
        func close() {
            dismiss(animated: true)
        }
    }

Link to example code

4. Now you can send push message with custom payload and "url" field with api or through portal

    "customPayload":{
       "url": "http://infobip.com/"
    }
Clone this wiki locally