【问题标题】:Opening browser when push notification is tapped点击推送通知时打开浏览器
【发布时间】:2015-04-29 08:35:38
【问题描述】:

我想在用户点击推送通知时打开浏览器。

在我的应用委托中,我有:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {    
    var json = remoteNotif[UIApplicationLaunchOptionsRemoteNotificationKey] as! NSDictionary

    storyboard = UIStoryboard(name: "Main", bundle: nil)
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let navC = storyboard.instantiateViewControllerWithIdentifier("mainNav") as! NavViewController

    let data = json.objectForKey("data") as! NSDictionary
    let url = data.objectForKey("url") as! String
    UIApplication.sharedApplication().openURL(NSURL(string: url)!)

    self.window?.rootViewController = navC
    self.window?.makeKeyAndVisible() 
}

当用户点击推送通知时,这会给我黑屏 20 秒,然后应用程序打开,然后浏览器打开。 如何让点击推送通知只打开浏览器,或者至少打开速度超过 20 秒?

谢谢。

【问题讨论】:

  • 这个问题解决了吗?
  • no... 最后用 webView 制作了一个新的 UIViewController
  • 好的,感谢您分享替代方式。 :) !
  • 你应该在块中得到响应然后尝试。像这样 let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_global_queue(priority, 0)) { dispatch_async(dispatch_get_main_queue()) { } }

标签: ios swift apple-push-notifications appdelegate


【解决方案1】:

虽然我只是打开一个 URL,但遇到了同样的问题。我试图打开 URL(即网络任务)而不将代码放在后台线程中的问题。只需使用它,它会在点击推送通知后立即打开:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
    UIApplication.sharedApplication().openURL(NSURL(string: "myURL")!)
})

【讨论】:

    【解决方案2】:

    您无法直接打开其他应用 - 您只能使用快速应用切换。 20 秒的黑屏听起来很奇怪 - 在多个设备上是否一致?

    如果是这样,您应该真正运行仪器,看看哪些功能需要这么长时间才能工作。听起来您的应用出现之前的加载由于某种原因非常繁重。

    如果 Instruments 没有给你任何东西(我很确定它会),你应该看看你将应用程序再次链接到什么 - 它是一个巨大的框架列表吗?

    作为一个快速技巧,以防加载情节提要是繁重的部分 - 您可以尝试在加载情节提要之前执行 openURL 本身。

    确保应用委托中的 shouldOpenURL 方法非常高效,并且不会执行任何同步网络任务或类似的时髦操作。

    【讨论】:

    • 我将运行仪器,但应用程序加载速度很快,我得到的不同推送会打开应用程序本身(我没有发布所有 os 代码)。只有当我调用 UIApplication.sharedApplication().openURL(NSURL(string: url)!) 我得到黑屏
    • 您可以在黑屏后几秒钟按下 Xcode 上的停止并在调试导航器上检查您的调用堆栈,也许这会有所帮助。
    【解决方案3】:

    当应用程序进入活动状态时,问题似乎与 RunLoop 有关。所以你只需要把你的代码放在当前循环的末尾。

    下面是 Swift 3.2 代码,用于在 iOS 用户点击推送通知时从推送通知中打开 URL:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [AnyHashable : Any] {
            self.application(application, didReceiveRemoteNotification: remoteNotification, fetchCompletionHandler: {_ in })
        }
        return true
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        if UIApplication.shared.applicationState == .inactive ||
            UIApplication.shared.applicationState == .active { //the app is bringing to an user
            if let urlString = ((userInfo["aps"] as? [String : Any])?["content"] as? [String: Any])?["link"] as? String, let url = URL(string: urlString) {
                DispatchQueue.main.async {
                    if UIApplication.shared.canOpenURL(url) {
                        UIApplication.shared.openURL(url)
                    }
                }
            }
        }
        completionHandler(UIBackgroundFetchResult.noData)
    }
    

    PS:基于 Marcos Reboucas 的回答

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多