Skip to content

Instantly share code, notes, and snippets.

@sharvit
Last active January 26, 2016 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharvit/0b0a42f5ee7c85b56d4f to your computer and use it in GitHub Desktop.
Save sharvit/0b0a42f5ee7c85b56d4f to your computer and use it in GitHub Desktop.
(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