Closed nikDemyankov closed 8 years ago
Actually it does work, you can close this @nikDemyankov I was registering to the js event at the wrong time (inside device ready)
With pleasure :)
I still have this problem. I have a long index.html (old app) and have tried to add the event listener direct in a script part in the page and also in the onLoad event. In both cases the UL link event is fired when the app is already loaded in the background. When i kill it and try again, the app opens, but without the event. I am trying with Android. Any idea?
Can you show some code, how you do the subscription and where?
I have made some tests. As i wrote, it is an old and big app. It has one big index.html. I include the cordova.ja and 33 other scripts at the beginning. After that i had this code:
<script>
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
console.log('deviceready');
document.addEventListener("resume", doResumeApp, false);
document.addEventListener("pause", doPauseApp, false);
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
console.log('eventhandler installed')
Init();
};
function onULOpen(event) {
alert('onULOpen');
}
document.addEventListener('ULOpen', onULOpen, false);
</script>
It has not worked, when the app was not running in the background. Now i have moved the UL block between the include of cordova.js and the other scripts.
<script>
document.addEventListener('ULOpen', onULOpen, false);
function onULOpen(event) {
alert('onULOpen');
}
</script>
And this works. I have tested with a Samsung Galaxy Note 4.
So now it's fine?
Yes. I have now the event in front before loading other scripts.
Is it possible that the event is fired before the page is loaded?
Maybe you should add some information, when the event is called. In my tests i see it before resume and before deviceReady.
When app is launched from the link - event is saved until plugins JS module is initialised. And this can happen before the deviceready
in the index page. So, you need to subscribe to the event as soon as possible.
As a workaround I can add to the plugin JS module (i.e., universalLinks
), and people can use it like so:
function onDeviceReady() {
console.log('deviceready');
// set handler for the event
universalLinks.on('event_name', onULOpen);
};
function onULOpen(event) {
// handle the event
}
Internally, it will store the events, received from the native side; and then universalLinks.on(...)
is called - send them to the callback. Basically, you are subscribing to the event through the plugins module.
What do you think about that?
This would be a good solution, because in the event all cordova stuff is available, when it is called after deviceReady. This also prevents for timing issues.
PS: Thank you for your work.
Thanks) Closing this issue. Created a separate one with the required feature.
If you launch the application first from the app icon, close it (by pressing home button on the phone) and then click on the UL link - JS event is captured.
But if you kill the app from task manager, and then click on the link - event is missed.