Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepLinkHandler is global, how can i call my service? #128

Closed
yonihei opened this issue Apr 26, 2016 · 7 comments
Closed

DeepLinkHandler is global, how can i call my service? #128

yonihei opened this issue Apr 26, 2016 · 7 comments

Comments

@yonihei
Copy link

yonihei commented Apr 26, 2016

Hello,
We have a problem about DeepLinkHandler . Since we are forced to make it a global function, we do not know how to use our angular services that we want.

  1. Does DeepLinkHandler really need to be global to be used?
  2. if so, how do we call our own angular services/controller functions in order to log in the user etc? A global function does not "know" them since it is not in its scope...

(already emailed brunch about it and didn't get a real answer)
Thanks,
Jonathan

@jstoup111
Copy link

(function() {
    'use strict';

    var core = angular.module('myapp.core');

    var config = {
        appErrorPrefix: '[myapp Error] ',
        appTitle:       'myApp'
    };

    core.value('config', config);

    core.run(CoreRun)
        .config(CoreConfig);

    CoreRun.$inject = ['$ionicPlatform'];

    function CoreRun($ionicPlatform) {
        $ionicPlatform.ready(function() {
            if(window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false); // jshint ignore:line
                cordova.plugins.Keyboard.disableScroll(true); // jshint ignore:line
            }

            if(window.StatusBar) {
                // org.apache.cordova.statusbar required
                StatusBar.styleDefault(); // jshint ignore:line
            }
        });
    }
})();

// Set Location of opening location
function handleOpenURL(url) {
    if(url.indexOf('myapp://') !== -1) {
        cordova.fireDocumentEvent('handleopenurl', {url: url});
    }
}

if('cordova' in window) {
    cordova.addStickyDocumentEventHandler('handleopenurl');
}

function DeepLinkHandler(link) {
    if(link.url) {
      if(link.url === 'myapp://') {
          link.url = 'myapp://link';
      }

        handleOpenURL(link.url);
    }
}

function NonBranchLinkHandler(link) {
    if(link.url) {
      if(link.url === 'myapp://') {
          link.url = 'myapp://';
      }

        handleOpenURL(link.url);
    }
}
(function() {
  'use strict';

  angular
    .module('myapp.openUrl', ['myapp.myapp-injection'])
    .factory('openUrlService', ['$location', '$rootScope', '$ionicHistory', openUrlService])
    .run(['openUrlService', run]);

  function openUrlService($location, $rootScope, $ionicHistory) {
    return {
      handleOpenUrl: handleOpenUrl,
      onResume:      onResume
    };

    function openUrl(url) {
      $ionicHistory.nextViewOptions({
        historyRoot:      true,
        disableBack:      true,
        disableAnimation: true
      });

         window.location.hash = url.replace('myapp://', '');
        $rootScope.$broadcast('handleopenurl', url);

        window.cordova.removeDocumentEventHandler('handleopenurl');
        window.cordova.addStickyDocumentEventHandler('handleopenurl');
        document.removeEventListener('handleopenurl', handleOpenUrl);
    }

    function handleOpenUrl(e) {
      openUrl(e.url);
    }

    function onResume() {
      document.addEventListener('handleopenurl', handleOpenUrl, false);
    }
  }

  function run(openUrlService) {
    if(openUrlService) {
      document.addEventListener('handleopenurl', openUrlService.handleOpenUrl, false);
      document.addEventListener('resume', openUrlService.onResume, false);
    }
  }

})();

@yonihei
Copy link
Author

yonihei commented Apr 26, 2016

ohh, so it is basically sending an event signal from javascript to the listener somewhere inside my platform ready.
thanks :)

@renesansz
Copy link
Contributor

Issue resolved, closing now.

@yonihei
Copy link
Author

yonihei commented May 2, 2016

note, the name handleOpenUrl seems to be colliding with facebook4 plugin

@jstoup111
Copy link

You might be using https://github.com/EddyVerbruggen/Custom-URL-scheme plugin. handleOpenUrl function name is irrelevant in this case since Branch handles branch and non-branch links. Just rename the function.

@haxpor
Copy link

haxpor commented Oct 29, 2016

Super thanks to @jstoup111 's solution. I modified his code and made it into general one and use 'sidemenu' template from ionic 1. See here https://gist.github.com/haxpor/644893a22f79bc910db37d79d36a2f00.

@ethanneff
Copy link
Contributor

This is the solution I have been using to integrate Branch's global DeepLinkHandler into Ionic/Angular.

Global DeepLinkHandler into Angular

listen to Branch data, save to an angular DeepLink factory

// must be a global function
function DeepLinkHandler(data) {
  if (data) {
    // access the angular Factory('DeepLink')
    angular.element(document.querySelector('[ng-app]')).injector().get('DeepLink').set(data);
    console.log('Data Link handler response: ' + JSON.stringify(data));
  } else {
    console.error('Data Link handler no data');
  }
}

angular DeepLink factory

angular.module('starter.services', [])

.factory('DeepLink', function($window, $timeout) {
  var data = {};

  return {
    get: function() {
      return data;
    },
    set: function(json) {
      // use the angular version of timeout
      $timeout(function() {
        // set the data
        data = json;
        // navigate example
        $window.location = "#/tab/chats/3";
      }, 0);
    }
  };
});

access angular DeepLink Factory

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope, DeepLink) {
  $scope.content = {}
  $scope.buttonPressed = function() {
    // put branch data into a label that has ng-model content.data
    $scope.content.data = JSON.stringify(DeepLink.get());
  };
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants