Last active
January 26, 2016 08:55
-
-
Save sharvit/0b0a42f5ee7c85b56d4f to your computer and use it in GitHub Desktop.
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('app.services') | |
.factory('$ionicPlatformManager', $ionicPlatformManager) | |
; | |
/* @ngInject */ | |
function $ionicPlatformManager ($rootScope, $window, $ionicAnalytics, PushManager, DEVELOPMENT_MODE) { | |
var service = { | |
init: init, | |
user: null, | |
push: null | |
}; | |
return service; | |
//////////////// | |
function init () { | |
console.log('Initializing ionic platforms...'); | |
// kick off the platform web client | |
Ionic.io(); | |
initIonicUser(); | |
initIonicPush(); | |
initIonicAnalytics(); | |
$rootScope.$watch('user', onUserChanged); | |
} | |
function initIonicUser () { | |
// this will give you a fresh user or the previously saved 'current user' | |
service.user = Ionic.User.current(); | |
} | |
function initIonicPush () { | |
// init the ionic push service | |
service.push = new Ionic.Push({ | |
debug: DEVELOPMENT_MODE === true, | |
onNotification: PushManager.onNotificationArrived, | |
pluginConfig: { | |
android: { | |
icon: 'icon' | |
}, | |
ios: { | |
sound: true, | |
vibration: true, | |
badge: true, | |
clearBadge: true | |
} | |
} | |
}); | |
// register to recive push notification | |
service.push.register(function (data) { | |
console.log('$ionicPlatformManager Got token', data); | |
service.push.addTokenToUser(service.user); | |
$rootScope.deviceToken = { token: data['_token'], platform: $window.ionic.Platform.platform() }; | |
}); | |
} | |
function initIonicAnalytics () { | |
// register to ionic analytics | |
$ionicAnalytics.register({ | |
// Use dryRun for development | |
dryRun: DEVELOPMENT_MODE === true | |
}); | |
} | |
function onUserChanged (newUser) { | |
console.log('$ionicPlatformManager::onUserChanged', newUser); | |
// update the ionic user | |
updateIonicUserWithAppUser(newUser); | |
// save the ionic user | |
service.user.save(); | |
} | |
function updateIonicUserWithAppUser (appUser) { | |
if (angular.isObject(appUser)) { | |
service.user.id = appUser.id.toString(); | |
service.user.set('name', appUser.name); | |
service.user.set('image', 'https://graph.facebook.com/' + appUser.facebookId + '/picture'); | |
service.user.set('facebook_id', appUser.facebookId); | |
service.user.set('is_tester', appUser.isTester); | |
} | |
// if the user doesn't have an id, you'll need to give it one. | |
if (!service.user.id) { | |
service.user.id = Ionic.User.anonymousId(); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment