nordnet / cordova-universal-links-plugin

[DEPRECATED] - Cordova plugin to support Universal/Deep Links for iOS/Android.
https://github.com/nordnet/cordova-universal-links-plugin/issues/160
MIT License
348 stars 531 forks source link

Handle event on appLaunch #21

Closed tidusIO closed 8 years ago

tidusIO commented 8 years ago

Really cool plugin, but i have an issue.

I build an ionic app. For testing I added the event to both: first inside and second outside of the module.

It works great, if the app is already running in the background. If the app is not running, then the app is started, but the event is not triggered for handling the linkData.

nikDemyankov commented 8 years ago

I really need to implement https://github.com/nordnet/cordova-universal-links-plugin/issues/18 :)

On what platform you are testing: iOS or Android? Can you show some code where you subscribing to the event?

tidusIO commented 8 years ago

Currently I'm testing on Android. Here is the code ;)

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },

    // Bind Event Listeners
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener('openLivingAppsPage', this.onLivingAppsPageRequested, false);
        document.addEventListener('ul_didLaunchAppFromLink', didLaunchAppFromLink, false);
    },

    // deviceready Event Handler
    onDeviceReady: function() {
        console.log('Handle deviceready event if needed.');
    },

    // openLivingAppsPage Event Handler
    onLivingAppsPageRequested: function(event) {
        var linkData = event.detail;
        console.log('Showing to living apps page: ' + linkData.path);
        // do some work to show detailed page
    },

    // ul_didLaunchAppFromLink Event Handler
    onApplicationDidLaunchFromLink: function(event) {
        var linkData = event.detail;
        console.log('Did launch app from the link: ' + linkData.url);
    }
};
app.initialize();
nikDemyankov commented 8 years ago

This looks like more Cordova style, then Ionic... Or you just testing the plugin from scratch?

Also,

document.addEventListener('ul_didLaunchAppFromLink', didLaunchAppFromLink, false);

And then:

// ul_didLaunchAppFromLink Event Handler
    onApplicationDidLaunchFromLink: function(event) {

Shouldn't it be like so:

// ul_didLaunchAppFromLink Event Handler
    didLaunchAppFromLink: function(event) {
tidusIO commented 8 years ago

Okay, that was a little fail in my code. But currently i only use the event 'openLivingAppsPage'.

At first my code was something like this.

document.addEventListener('ul_didLaunchAppFromLink', didLaunchAppFromLink, false);

// this function should be is executed when we will launch the app from the link
function didLaunchAppFromLink(event) {
    var urlData = event.detail;
    console.log('Did launch application from the link: ' + urlData.url);
};

angular.module('livingapps', [
    'ionic',
    'ionic.service.core',
    'ionic.service.push',
...

The code was placed before my angular module.

I think it's the problem like this #3.

I will test to place the code before plugins like cordova, ionic and so on.

18 would be the best solution. ;)

nikDemyankov commented 8 years ago

Good, hope it will work)

tidusIO commented 8 years ago

Nice, it's works ;) A little bit hacky, but for the beginning a workable solution.

The code will be called before plugins and ionic.

document.addEventListener('ul_didLaunchAppFromLink', didLaunchAppFromLink, false);

// this function should be is executed when we will launch the app from the link
function didLaunchAppFromLink(event) {
    window.sessionStorage.setItem('ul_didLaunchAppFromLink', JSON.stringify(event.detail));
};

In a run method of angular.

// UniversalLinks
.run(function($ionicPlatform, $sessionStorage, UniversalLinkService) {
    $ionicPlatform.ready(function() {
        var ul_handleLink = function() {
            var key = 'ul_didLaunchAppFromLink';
            if(window.sessionStorage.getItem(key)) {
                var urlData = JSON.parse(window.sessionStorage.getItem(key));
                UniversalLinkService.open(urlData).then(function() {
                    window.sessionStorage.removeItem(key);
                });
            }
        }
        document.addEventListener("deviceready", function() {
            console.log("The application is ready");
            setTimeout(function() {
                ul_handleLink();
            }, 0);
        }, false);
        document.addEventListener("resume", function() {
            console.log("The application is resuming from the background");
            setTimeout(function() {
                ul_handleLink();
            }, 0);
        }, false);
    });
})

The service 'UniversalLinkService' handles the path.

Thanks for your support ;)

nikDemyankov commented 8 years ago

Nice, thanks for sharing a workaround )