推送通知点击获取 url 并在 View Controller , iOS , Swift4.2 中显示

Posted

技术标签:

【中文标题】推送通知点击获取 url 并在 View Controller , iOS , Swift4.2 中显示【英文标题】:Push Notification Get url on tap and display it in View Controller , iOS , Swift4.2 【发布时间】:2019-02-22 11:32:41 【问题描述】:

我在 App 委托中的 didReceiveRemoteNotification 是,

 private func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) 

           print("UserInfo: \(userInfo)")

            switch application.applicationState 
            case .active:

                let content = UNMutableNotificationContent()
                if let title = userInfo["title"]
                
                    content.title = title as! String
                
                 content.userInfo = userInfo
                content.sound = UNNotificationSound.default

                let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 0.5, repeats: false)
                let request = UNNotificationRequest(identifier:"rig", content: content, trigger: trigger)

                UNUserNotificationCenter.current().delegate = self
                UNUserNotificationCenter.current().add(request)  (error) in
                    if let getError = error 
                        print(getError.localizedDescription)
                    
                
            case .inactive:
                break
            case .background:
                break
            
            

推送通知后我的回复是,

 [AnyHashable("google.c.a.e"): 1, AnyHashable("content"): https://staging.travelsheriff.de, AnyHashable("aps"): 
        alert =     
            body = "Friend Request";
            title = "Dave has  sent you friend request";
        ;
        category = "https://staging.travelsheriff.de";
        sound = default;
    , AnyHashable("gcm.notification.url"): https://staging.travelsheriff.de]

我必须将“内容”存储为字符串并将其传递给我的视图控制器(作为 url)来显示。我怎么能这样做....

【问题讨论】:

【参考方案1】:

为了从推送通知中获取 url,请在您的 App 委托中尝试此操作 (这将从推送通知响应中保存内容(url)。)

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 
        let content: UNNotificationContent = response.notification.request.content
        let userInfo = content.userInfo
        if let url = userInfo["content"] as? String
            NotificationCenter.default.post(name: Notification.Name("url"), object: url)
            UserDefaults.standard.set(url, forKey: "urlNotification")
            let storyBoard : UIStoryboard = UIStoryboard(name: "yourStoryboardName", bundle:nil)
            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "yourPushViewController") as! yourPushViewController
            window?.rootViewController = nextViewController
        
        completionHandler()
    

然后在 viewdidload 中像这样在你的推送视图控制器调用中,

    override func viewDidLoad() 
            super.viewDidLoad()
          pushUrl()// this is function to call the notification url in web view 

        
    func pushUrl() 
    let getPushURL = UserDefaults.standard.value(forKey: "urlNotification") as? String
   if let url = URL(string: getPushURL) 
                let request = URLRequest(url: url)
                webView.load(request)
      
    

【讨论】:

它工作正常,获取 url 并可以在我的 pushviewcontroller 中显示它。谢谢 有什么办法可以在通知警报中将此网址显示为超链接????单击该超链接打开一个网络视图。(在网络视图上显示网址的这一部分已完成)唯一的事情是我不知道我是否可以在远程通知上显示超链接文本。【参考方案2】:

在您的didReceiveRemoteNotification 方法中,

if let url = userInfo["category"] as? String
NotificationCenter.default.post(name: Notification.Name("url"), object: url)


在您的 ViewController 中,在 viewDidLoad 中添加这一行

 NotificationCenter.default.addObserver(self, selector: #selector(self.gotUrl(string:)), name: Notification.Name(rawValue: "url"), object: nil)

然后实现这个方法:

 @objc func gotUrl(string : String)
    print(string)

如果你想保存它,那么在你的 didReceiveRemoteNotification 方法中

if let url = userInfo["category"] as? String
   UserDefaults.standard.set(url, forKey: "url")

    

然后检索:

if let url =  UserDefaults.standard.value(forKey: "url") as? String 
        print(url)
    

【讨论】:

您能否建议我如何调用函数以在视图控制器中显示意味着如何呈现 vc 我尝试在视图控制器中做同样的事情,但它在完成处理程序中显示错误。

以上是关于推送通知点击获取 url 并在 View Controller , iOS , Swift4.2 中显示的主要内容,如果未能解决你的问题,请参考以下文章

Parse -Android 获取多个推送通知

保存推送通知并在 UITableView 中显示

Chrome推送通知 - 点击后如何打开URL地址?

如何使用解析推送通知打开 URL?

当应用程序处于后台时,从推送通知中插入 coredata 记录

MVC中的传参并在View中获取