Created
September 12, 2014 17:24
-
-
Save yuripiratello/57db6567944f5ce67b98 to your computer and use it in GitHub Desktop.
Exemplo de controller
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
.controller('AppCtrl', function ($scope, $ionicModal, $timeout, AuthFactory, localStorage, $cordovaPush, $cordovaDevice, $cordovaDialogs, $cordovaToast, $cordovaVibration, MensagemFactory, $ionicLoading, $rootScope) { | |
// Form data for the login modal | |
$scope.loginData = {}; | |
// Create the login modal that we will use later | |
$ionicModal.fromTemplateUrl('templates/login.html', { | |
scope: $scope | |
}).then(function (modal) { | |
$scope.modal = modal; | |
}); | |
// Triggered in the login modal to close it | |
$scope.closeLogin = function () { | |
$scope.modal.hide(); | |
}; | |
// Open the login modal | |
$scope.login = function () { | |
$scope.modal.show(); | |
}; | |
// Perform the login action when the user submits the login form | |
$scope.doLogin = function () { | |
console.log('Doing login', $scope.loginData); | |
AuthFactory.setUser($loginData.username); | |
// Simulate a login delay. Remove this and replace with your login | |
// code if using a login system | |
$timeout(function () { | |
$scope.closeLogin(); | |
}, 1000); | |
}; | |
window.onNotification = function (e) { | |
console.log(e); | |
if (e.event == 'registered') { | |
MensagemFactory.registrarDevice(e.regid) | |
.success(function (retorno) { | |
if (retorno.status == 'success') { | |
$cordovaToast.show('Registrado com sucesso!', 'long', 'bottom'); | |
} else { | |
console.info(retorno); | |
$cordovaToast.show('Não foi possível registrar o aparelho!', 'long', 'bottom'); | |
} | |
}) | |
.error(function (retorno) { | |
console.info(retorno); | |
$cordovaToast.show('Não foi possível registrar o aparelho!', 'long', 'bottom'); | |
}); | |
} else if (e.event == 'message') { | |
if (e.payload.data.type == 'mensagem') { | |
$cordovaToast.show('Nova mensagem', 'long', 'top').then(function (success) { | |
$cordovaVibration.vibrate(500); | |
}); | |
localStorage.set('ultima_verificacao_' + e.payload.data.de, new Date().toJSON().slice(0, 18).replace('T', ' ')); | |
var mensagens = localStorage.getObject('mensagens_' + e.payload.data.de, []); | |
mensagens.push({ | |
'content': '<p>' + e.payload.data.data.content + '</p>', | |
'owner': e.payload.data.de == AuthFactory.getUser().nick ? 'mine' : 'his' | |
}); | |
localStorage.setObject('mensagens_' + e.payload.data.de, mensagens); | |
$rootScope.$emit('atualiza-mensagens'); | |
$ionicLoading.hide(); | |
} else { | |
console.info('Payload que não é mensagem'); | |
console.info(e.payload); | |
} | |
} | |
}; | |
var androidConfig = { | |
"senderID": '954130818861', | |
"ecb": "onNotification" | |
}; | |
var iosConfig = { | |
"badge": "true", | |
"sound": "true", | |
"alert": "true", | |
"ecb": "onNotificationAPN" | |
}; | |
function onDeviceReady() { | |
$cordovaPush.register(androidConfig).then(function (result) { | |
console.info(result); | |
}, function (err) { | |
console.error(err); | |
}); | |
} | |
document.addEventListener('deviceready', onDeviceReady, false); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment