【问题标题】:Send notification payload to a UIViewController将通知负载发送到 UIViewController
【发布时间】:2017-07-16 22:33:43
【问题描述】:

我正在使用 Firebase 向 WebViewController() 发送推送通知。
收到的通知有一个 URL 有效负载,我已成功从有效负载中获取 URL,但现在当用户点击通知时,我无法将其发送到我的 WebViewController()。

这是当用户点击通知时调用的有效负载方法的方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo

    if let aps = userInfo["aps"] as? [String: Any] {
        if let alertPayload = aps["alert"] as? String {
            let webViewController = WebViewController()
            webViewController.urlString = alertPayload
            let url = URL(string: alertPayload)
            let urlRequest = URLRequest(url: url!)
            webViewController.webView.loadRequest(urlRequest)
        }
    }

    completionHandler()
}

这里是WebViewController()

class WebViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()


    }
}

请注意,我已成功收到通知,但当我每次调用此通知时单击通知时应用程序崩溃:

webViewController.webView.loadRequest(urlRequest) 说found nil

【问题讨论】:

    标签: ios swift firebase notifications firebase-cloud-messaging


    【解决方案1】:

    发生崩溃是因为webView 尚未加载到您的webViewController 中。我们不能简单地使用let webViewController = WebViewController() 进行实例化,尤其是在您使用故事板时。

    处理推送通知和重新加载新内容(在您的情况下重新加载新 url)的常用方法是使用 NotificationCenter(假设您使用的是 Swift 3)。 NotificationCenter 就像您在控制器中添加侦听器/观察者并等待事件发生

    首先,将观察者添加到您想要在 viewDidLoad 方法中进行更改的 webViewController。

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // some of your codes here
        // ...
        // ...
    
        NotificationCenter.default.addObserver(self, selector: #selector(WebViewController.reloadWebview(notification:)), name: Notification.Name("NotificationReloadWebView"), object: nil)
    }
    

    然后添加方法来处理如果通知发生

    func reloadWebview(notification: Notification) {
        if let url = notification.userInfo?["url"] as? UIImage {
            // load the url to webview
            let urlRequest = URLRequest(url: url!)
            self.webView.loadRequest(urlRequest)
        }
    }
    

    最后,一旦收到新的推送通知,通过在AppDelegate 中发送 Post Notification 来触发观察者

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
    
        if let aps = userInfo["aps"] as? [String: Any] {
            if let newUrl = aps["alert"] as? String {
                let info = ["url": newUrl]
    
                // post notification with info (url)
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationReloadWebView"), object: nil, userInfo: info)
            }
        }
    
        completionHandler()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-31
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多