Skip to content

Instantly share code, notes, and snippets.

@wedgybo
Last active December 24, 2015 01:24
Show Gist options
  • Save wedgybo/7ee11834cc64b3f452f3 to your computer and use it in GitHub Desktop.
Save wedgybo/7ee11834cc64b3f452f3 to your computer and use it in GitHub Desktop.
if ('cordova' in window) {
// Create a sticky event for handling the app being opened via a custom URL
cordova.addStickyDocumentEventHandler('handleopenurl');
}
function handleOpenURL (url) {
cordova.fireDocumentEvent('handleopenurl', { url: url });
};
@jonlil
Copy link

jonlil commented Apr 20, 2015

Hi, thanks for this. I get the cold start to work but once the event is fired I cant fire it again. Any ideas?

@jonlil
Copy link

jonlil commented Apr 20, 2015

What I ended up with are something like this

(function() {
  var app = angular.module('mycoolapp', []);

  app.run(['$window', function($window) {
    // Subscribe to cold-start event
    document.addEventListener('handleurl', handleOpen, false);

    // re-register event handler
    $window.handleOpenURL = function(url) {
      handleOpen({
        url: url
      });
    };

    function handleOpen(e) {
      handleExternalOpenURL(e.url);
    }
  });
})();


if ('cordova' in window) {
  // Create a sticky event for handling the app being opened via a custom URL
  cordova.addStickyDocumentEventHandler('handleurl');
}

function handleOpenURL (url) {
  cordova.fireDocumentEvent('handleurl', { url: url });
}

@wedgybo
Copy link
Author

wedgybo commented Jun 10, 2015

Just saw this! Glad it helped out. There were a few typos previously that I think might have been causing some issues 😞

@symbiat
Copy link

symbiat commented Jun 12, 2015

@jonill @wedgybo Where is handleExternalOpenURL defined? And did you rename the event from handleopenurl to handleurl throughout your app?

@konami99
Copy link

Hi,

I couldn't get it working on cold start on android. handleOpenURL(url) didn't get called. I was testing push notification that contains a deep link. Here is the flow:

  1. Receive push notification, click on it
  2. App is launched
  3. Landing page loaded. Didn't go to the page I specified in deep link.

I think it's because handleOpenURL() isn't there yet, when I click push notification?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment