Last active
November 9, 2016 17:34
-
-
Save haxpor/644893a22f79bc910db37d79d36a2f00 to your computer and use it in GitHub Desktop.
Deeplink integrated with Branch.io with ionic 1 (sidemenu template)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ionic Starter App | |
// angular.module is a global place for creating, registering and retrieving Angular modules | |
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html) | |
// the 2nd parameter is an array of 'requires' | |
// 'starter.controllers' is found in controllers.js | |
angular.module('starter', ['ionic', 'starter.controllers', 'starter.openUrl']) | |
.run(function($ionicPlatform) { | |
$ionicPlatform.ready(function() { | |
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard | |
// for form inputs) | |
if (window.cordova && window.cordova.plugins.Keyboard) { | |
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); | |
cordova.plugins.Keyboard.disableScroll(true); | |
// init branch.io | |
console.log("Branch: ", Branch); | |
Branch.initSession(); | |
document.addEventListener("resume", function() { | |
Branch.initSession(); | |
}, false); | |
} | |
if (window.StatusBar) { | |
// org.apache.cordova.statusbar required | |
StatusBar.styleDefault(); | |
} | |
}); | |
}) | |
.config(function($provide, $stateProvider, $urlRouterProvider) { | |
$stateProvider | |
.state('app', { | |
url: '/app', | |
abstract: true, | |
templateUrl: 'templates/menu.html', | |
controller: 'AppCtrl' | |
}) | |
.state('app.search', { | |
url: '/search', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/search.html' | |
} | |
} | |
}) | |
.state('app.browse', { | |
url: '/browse', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/browse.html' | |
} | |
} | |
}) | |
.state('app.playlists', { | |
url: '/playlists', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/playlists.html', | |
controller: 'PlaylistsCtrl' | |
} | |
} | |
}) | |
.state('app.single', { | |
url: '/playlists/:playlistId', | |
views: { | |
'menuContent': { | |
templateUrl: 'templates/playlist.html', | |
controller: 'PlaylistCtrl' | |
} | |
} | |
}); | |
// if none of the above states are matched, use this as the fallback | |
$urlRouterProvider.otherwise('/app/playlists'); | |
}); | |
// Set Location of opening location | |
function handleOpenURL(url) { | |
console.log("handleOpenURL: " + url); | |
cordova.fireDocumentEvent('handleopenurl', {url: url}); | |
} | |
if('cordova' in window) { | |
cordova.addStickyDocumentEventHandler('handleopenurl'); | |
console.log("passed cordova checked to add sticky document event handler"); | |
} | |
function DeepLinkHandler(data) { | |
if (data != null) { | |
console.log("branch: " + data); | |
console.log("branch's data: " + data.data); | |
// this should be your first default page | |
// but it goes to playlist/1 just for example | |
// your marketing campaign of using this link should be only to solve problem of deeplink in case of users didn't install the app yet | |
// as this functionality is not supported in Cordova/Ionic Sdk | |
var url = "ionic2link://app/playlists/1"; | |
console.log("hadle open url for DeepLinkHandler: " + url); | |
handleOpenURL(url); | |
} | |
else { | |
console.log("branch link data is null"); | |
} | |
} | |
function NonBranchLinkHandler(data) { | |
if (data != null) { | |
var jsonData = JSON.stringify(data); | |
console.log("nonbranch jsonData: " + jsonData); | |
// (same comment with above) this should be your first default page | |
// but it goes to playlist/1 just for example | |
// your marketing campaign of using this link should be only to solve problem of deeplink in case of users didn't install the app yet | |
// as this functionality is not supported in Cordova/Ionic Sdk | |
var url = "ionic2link://app/playlists/1"; | |
console.log("handle open url for NonBranchLinkHandler: " + url); | |
handleOpenURL(url); | |
} | |
else { | |
console.log("non-branch link data is null"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular | |
.module('starter.openUrl', []) | |
.factory('openUrlService', ['$location', '$rootScope', '$ionicHistory', openUrlService]) | |
.run(['openUrlService', run]); | |
function openUrlService($location, $rootScope, $ionicHistory) { | |
return { | |
handleOpenUrl: handleOpenUrl, | |
onResume: onResume | |
}; | |
function openUrl(url) { | |
console.log("try to openUrl: " + url); | |
window.location.hash = url.replace('ionic2link://', ''); | |
$rootScope.$broadcast('handleopenurl', url); | |
window.cordova.removeDocumentEventHandler('handleopenurl'); | |
window.cordova.addStickyDocumentEventHandler('handleopenurl'); | |
document.removeEventListener('handleopenurl', handleOpenUrl); | |
console.log("finish openUrl operation"); | |
} | |
function handleOpenUrl(e) { | |
console.log("try to handle url: " + e.url); | |
openUrl(e.url); | |
} | |
function onResume() { | |
document.addEventListener('handleopenurl', handleOpenUrl, false); | |
console.log("add event listener onResume"); | |
} | |
} | |
function run(openUrlService) { | |
if(openUrlService) { | |
document.addEventListener('handleopenurl', openUrlService.handleOpenUrl, false); | |
document.addEventListener('resume', openUrlService.onResume, false); | |
console.log("'openUrlService': registered for event listeners"); | |
} | |
else { | |
console.log("'openUrlService': openUrl service is null so don't do anything"); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment