Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening browser when push notification is tapped

I want to open the browser when the user taps a push notification.

In my app delegate I have:

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() 
}

When the user taps the push notification this gives me a black screen for 20 seconds, then the app opens, and then the browser opens. How can I make the tap on the push notification open only the browser, or at least open faster than 20 seconds?

Thank you.

like image 513
ilan Avatar asked Nov 10 '22 14:11

ilan


1 Answers

Had the same issue although I am just opening an URL. The problem I was trying to open the URL (that's network task) without putting the code in a background thread. Just use this and it's opened right after the push notification is clicked:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
    UIApplication.sharedApplication().openURL(NSURL(string: "myURL")!)
})
like image 57
Marcos Reboucas Avatar answered Nov 15 '22 04:11

Marcos Reboucas