This commit is contained in:
Gabriel Bazán 2016-01-04 14:11:24 -03:00
commit 171665afe6
4 changed files with 93 additions and 93 deletions

View file

@ -12,62 +12,15 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.prevState = 'walletHome'; self.prevState = 'walletHome';
document.addEventListener('deviceready', function() { document.addEventListener('deviceready', function() {
if (self.isCordova) { if (self.isCordova) {
storageService.getDeviceToken(function(err, token) { storageService.getDeviceToken(function(err, token) {
if (!token) pushNotificationInit(); $timeout(function() {
if (!token) pushNotificationsService.pushNotificationsInit();
}, 5000);
}); });
} }
}); });
function pushNotificationInit() {
var push = PushNotification.init({
android: {
senderID: "959259672122"
},
ios: {
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
});
push.on('registration', function(data) {
var fc = profileService.focusedClient;
var opts = {};
opts.user = fc.credentials.walletId;
opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null;
opts.token = data.registrationId;
storageService.setDeviceToken(data.registrationId, function() {
pushNotificationsService.subscribe(opts).then(function(response) {
$log.debug('Suscribed: ' + response.status);
},
function(err) {
$log.warn('Error: ' + err);
});
});
});
push.on('notification', function(data) {
$log.debug('Notification event: ', data.message);
/* data.message,
data.title,
data.count,
data.sound,
data.image,
data.additionalData
*/
});
push.on('error', function(e) {
$log.warn('Error trying to push notifications: ', e);
});
}
function strip(number) { function strip(number) {
return (parseFloat(number.toPrecision(12))); return (parseFloat(number.toPrecision(12)));
}; };
@ -1254,32 +1207,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateAll(); self.updateAll();
}); });
$rootScope.$on('Local/EnableNotifications', function(event, val) {
storageService.getDeviceToken(function(err, token) {
var fc = profileService.focusedClient;
var opts = {};
opts.user = [fc.credentials.walletId];
opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null;
opts.token = token;
if (val) {
pushNotificationsService.subscribe(opts).then(function(response) {
$log.debug('Suscribed: ' + response.status);
},
function(err) {
$log.warn('Error: ' + err);
});
} else {
pushNotificationsService.unsubscribe(token).then(function(response) {
$log.debug('Unsuscribed: ' + response.status);
},
function(err) {
$log.warn('Error: ' + err);
});
}
self.updateAll();
});
});
$rootScope.$on('Local/FeeLevelUpdated', function(event, level) { $rootScope.$on('Local/FeeLevelUpdated', function(event, level) {
self.setCurrentFeeLevel(level); self.setCurrentFeeLevel(level);
}); });
@ -1346,6 +1273,10 @@ angular.module('copayApp.controllers').controller('indexController', function($r
go.walletHome(); go.walletHome();
}); });
$rootScope.$on('Local/EnableNotifications', function(event) {
pushNotificationsService.enableNotifications();
});
self.debouncedUpdate = lodash.throttle(function() { self.debouncedUpdate = lodash.throttle(function() {
self.updateAll({ self.updateAll({
quiet: true quiet: true

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('preferencesGlobalController', angular.module('copayApp.controllers').controller('preferencesGlobalController',
function($scope, $rootScope, $log, configService, uxLanguage, isCordova) { function($scope, $rootScope, $log, configService, uxLanguage, isCordova, pushNotificationsService) {
this.init = function() { this.init = function() {
var config = configService.getSync(); var config = configService.getSync();
@ -41,7 +41,10 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
} }
}; };
configService.set(opts, function(err) { configService.set(opts, function(err) {
$rootScope.$emit('Local/EnableNotifications', opts.notifications.enabled); if (opts.notifications.enabled)
pushNotificationsService.enableNotifications();
else
pushNotificationsService.disableNotifications();
if (err) $log.debug(err); if (err) $log.debug(err);
}); });
}); });

View file

@ -397,7 +397,7 @@ angular.module('copayApp.services')
handleImport(function() { handleImport(function() {
root.setAndStoreFocus(walletId, function() { root.setAndStoreFocus(walletId, function() {
storageService.storeProfile(root.profile, function(err) { storageService.storeProfile(root.profile, function(err) {
$rootScope.$emit('Local/EnableNotifications', true); $rootScope.$emit('Local/EnableNotifications');
return cb(err, walletId); return cb(err, walletId);
}); });
}); });

View file

@ -1,18 +1,84 @@
'use strict'; 'use strict';
angular.module('copayApp.services') angular.module('copayApp.services')
.factory('pushNotificationsService', function($http) { .factory('pushNotificationsService', function($http, $log, profileService, storageService, lodash) {
var root = {}; var root = {};
root.subscribe = function(opts) { root.pushNotificationsInit = function() {
return $http.post('http://192.168.1.126:8000/subscribe', opts);
}
root.unsubscribe = function(token) { var push = PushNotification.init({
return $http.post('http://192.168.1.126:8000/unsubscribe', { android: {
token: token senderID: "959259672122"
}); },
} ios: {
alert: "true",
badge: true,
sound: 'false'
},
windows: {}
});
return root; push.on('registration', function(data) {
storageService.setDeviceToken(data.registrationId, function() {
root.enableNotifications();
});
});
}); push.on('notification', function(data) {
$log.debug('Notification event: ', data.message);
/* data.message,
data.title,
data.count,
data.sound,
data.image,
data.additionalData
*/
});
push.on('error', function(e) {
$log.warn('Error trying to push notifications: ', e);
});
};
root.enableNotifications = function() {
storageService.getDeviceToken(function(err, token) {
lodash.forEach(profileService.getWallets('livenet'), function(wallets) {
var opts = {};
opts.user = wallets.id;
opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null;
opts.token = token;
root.subscribe(opts).then(function(response) {
$log.debug('Suscribed: ' + response.status);
},
function(err) {
$log.warn('Error: ' + err.status);
});
});
});
}
root.disableNotifications = function() {
storageService.getDeviceToken(function(err, token) {
root.unsubscribe(token).then(function(response) {
$log.debug('Unsubscribed: ' + response.status);
},
function(err) {
$log.warn('Error: ' + err.status);
});
});
}
root.subscribe = function(opts) {
return $http.post('http://192.168.1.128:8000/subscribe', opts);
}
root.unsubscribe = function(token) {
return $http.post('http://192.168.1.128:8000/unsubscribe', {
token: token
});
}
return root;
});