Created
May 28, 2015 15:18
-
-
Save chris08002/331900fa2fe3ee9f739d to your computer and use it in GitHub Desktop.
Branch.IO in ionic
This file contains 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
angular.module('jm.branchIo', ['ionic']) | |
.factory('BranchIo', function ($q, $ionicPlatform, $rootScope, env) { | |
// variables are for us, not required: | |
var deepLink = {}; | |
var referringUser = {}; | |
var onDevice = false; | |
var initialized = false; | |
var campaign_code = ""; | |
var job_id = null; | |
return { | |
init: function() { | |
var branch = window.Branch; | |
var deferred = $q.defer(); | |
branch.initSession(true, function(params) { | |
initialized = true; | |
deepLink = params; | |
// checking on different deep link params that we have defined: | |
if (params.referringUser) { | |
referringUser = params.referringUser; | |
} | |
if (params.campaign_code) { | |
campaign_code = params.campaign_code; | |
} | |
if (params.job_id) { | |
job_id = params.job_id; | |
} | |
$rootScope.$broadcast('branch_io_initialized', params); | |
deferred.resolve(params); | |
}); | |
return deferred.promise; | |
}, | |
pause: function() { | |
// resetting some of our variables: | |
deepLink = {}; | |
referringUser = {}; | |
campaign_code = ""; | |
job_id = null; | |
// this one is important: | |
var branch = window.Branch; | |
branch.closeSession(); | |
}, | |
setIdentity: function(identity) { | |
var branch = window.Branch; | |
if (window.cordova) { | |
// a little workaround to check if init already done, otherwise wait for init done event | |
if (initialized == true) { | |
branch.setIdentity(identity, null); | |
} else { | |
var deleteListener = $rootScope.$on('branch_io_initialized', function (e) { | |
console.log("set identity: listener received", identity); | |
var branch = window.Branch; | |
branch.setIdentity(identity, null); | |
deleteListener(); | |
}); | |
} | |
} | |
}, | |
logout: function() { | |
var branch = window.Branch; | |
if (window.cordova) { | |
branch.logout(); | |
} | |
}, | |
getRewardBalance: function(bucket) { | |
var deferred = $q.defer(); | |
if (window.cordova) { | |
var branch = window.Branch; | |
branch.loadRewards(function(changed) { | |
if (changed) { | |
// Pass the bucket name to retrieve the current balance of credits | |
} | |
branch.getCredits(bucket, function(credits) { | |
deferred.resolve(credits); | |
}); | |
}); | |
} | |
return deferred.promise; | |
}, | |
getShortUrl: function(params, channel, feature) { | |
var deferred = $q.defer(); | |
if (window.cordova) { | |
var branch = window.Branch; | |
branch.getShortUrl(params || {}, channel, feature, function(url) { | |
deferred.resolve(url); | |
}); | |
} | |
return deferred.promise; | |
}, | |
userCompletedAction: function(action) { | |
if (window.cordova) { | |
var branch = window.Branch; | |
branch.userCompletedAction(action, null); | |
} | |
} | |
}; | |
}) | |
.run(function($ionicPlatform, BranchIo, env, $state) { | |
$ionicPlatform.ready(function() { | |
var debugMode = true; | |
if (env.name == 'production') debugMode = false; | |
if (debugMode) appKey = 'xxxxxxx' | |
else appKey = 'yyyyyyyyy;' | |
var branch = window.Branch; | |
if (window.cordova) branch.getInstance(appKey, function() { | |
if (debugMode) { | |
console.log("set debug mode"); | |
branch.setDebug(); | |
} | |
// this is important: | |
document.addEventListener("pause", BranchIo.pause, false); | |
BranchIo.init().then(function(params) { | |
if (params) { | |
console.log("params: ", params); | |
} | |
}); | |
}); | |
// Disable BACK button on home | |
// Important on android, otherwise hardware back button exits the app without calling pause() which then causes wrong deeplinks at the next start | |
// We just check if we want to exit the app and in case we do, we call pause before | |
$ionicPlatform.registerBackButtonAction(function (event) { | |
if($state.current.name == "tab.searchjob" || $state.current.name == "tab.myjobs" || $state.current.name == "tab.createjob" || $state.current.name == "tab.account") { | |
BranchIo.pause(); | |
console.log("Branch paused"); | |
navigator.app.exitApp(); | |
} | |
else { | |
navigator.app.backHistory(); | |
} | |
}, 100); | |
}); | |
}); |
This file contains 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
angular.module('mlz.openurl', ['ionic']) | |
.factory('OpenUrlService', function ($log, $location, $rootScope, $ionicHistory, $timeout, $state, $ionicLoading, BranchIo) { | |
var setNextViewOptions = function() { | |
$ionicHistory.nextViewOptions({ | |
historyRoot: true, | |
disableBack: true, | |
disableAnimation: true | |
}); | |
} | |
var openUrl = function (url) { | |
// Stop it from caching the first view as one to return when the app opens | |
if (url) { | |
if (url.substr(10,4) == 'open') { | |
$ionicLoading.show({template: "Cargando parametros", hideOnStateChange: true, duration: 5000}); | |
BranchIo.init().then( function(res) { | |
$ionicLoading.hide(); | |
// check on different parameters that we might use in deep links: | |
if (res && res.job_id) { | |
setNextViewOptions(); | |
$state.go("tab.searchjob.showjob", {id: res.job_id}); | |
} else if (res && res.referringUser) { | |
setNextViewOptions(); | |
$state.go("tab.account", {referringUser: res.referringUser}); | |
} | |
}); | |
} | |
window.cordova.removeDocumentEventHandler('handleurl'); | |
window.cordova.addStickyDocumentEventHandler('handleurl'); | |
document.removeEventListener('handleurl', handleOpenUrl); | |
} | |
}; | |
var handleOpenUrl = function (e) { | |
// Check for the CustomEvent detail field for testing. | |
// Difficult to trigger cordova events under test | |
var url = e.url || e.detail.url; | |
console.log("handleOpenUrl, event", e); | |
openUrl(url); | |
}; | |
var onResume = function () { | |
console.log("onResume"); | |
document.addEventListener('handleurl', handleOpenUrl, false); | |
}; | |
return { | |
handleOpenUrl: handleOpenUrl, | |
onResume: onResume | |
}; | |
}).run(function (OpenUrlService) { | |
if (OpenUrlService) { | |
document.addEventListener('handleurl', OpenUrlService.handleOpenUrl, false); | |
document.addEventListener('resume', OpenUrlService.onResume, false); | |
} | |
}); |
This file contains 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
// When a user logs in we use this: | |
BranchIo.setIdentity({uuid: uuid}); |
Thanks for posting this! So this is using the deprecated Branch library, right? Not the new and improved WebSDK.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I hope this is understandable, not really an expert on gist :)