push notifications service addapted to bws

This commit is contained in:
Gabriel Bazán 2016-01-15 11:59:29 -03:00
commit 425d8ffb33
4 changed files with 44 additions and 37 deletions

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, feeService, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile, addressbookService) { angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, isMobile, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, feeService, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile, addressbookService) {
var self = this; var self = this;
var SOFT_CONFIRMATION_LIMIT = 12; var SOFT_CONFIRMATION_LIMIT = 12;
self.isCordova = isCordova; self.isCordova = isCordova;
@ -12,7 +12,7 @@ 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 (isMobile.Android() || isMobile.iOS()) {
storageService.getDeviceToken(function(err, token) { storageService.getDeviceToken(function(err, token) {
$timeout(function() { $timeout(function() {
if (!token) pushNotificationsService.pushNotificationsInit(); if (!token) pushNotificationsService.pushNotificationsInit();
@ -31,15 +31,24 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.menu = [{ self.menu = [{
'title': gettext('Receive'), 'title': gettext('Receive'),
'icon': {false:'icon-receive', true: 'icon-receive-active'}, 'icon': {
false: 'icon-receive',
true: 'icon-receive-active'
},
'link': 'receive' 'link': 'receive'
}, { }, {
'title': gettext('Activity'), 'title': gettext('Activity'),
'icon': {false:'icon-activity',true: 'icon-activity-active'}, 'icon': {
false: 'icon-activity',
true: 'icon-activity-active'
},
'link': 'walletHome' 'link': 'walletHome'
}, { }, {
'title': gettext('Send'), 'title': gettext('Send'),
'icon': {false:'icon-send', true: 'icon-send-active'}, 'icon': {
false: 'icon-send',
true: 'icon-send-active'
},
'link': 'send' 'link': 'send'
}]; }];
@ -403,7 +412,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.availableMaxBalance = strip((self.availableBalanceSat - feeToSendMaxSat) * self.satToUnit); self.availableMaxBalance = strip((self.availableBalanceSat - feeToSendMaxSat) * self.satToUnit);
self.feeToSendMaxStr = profileService.formatAmount(feeToSendMaxSat) + ' ' + self.unitName; self.feeToSendMaxStr = profileService.formatAmount(feeToSendMaxSat) + ' ' + self.unitName;
} }
if (cb) return cb(self.currentFeePerKb, self.availableMaxBalance, self.feeToSendMaxStr); if (cb) return cb(self.currentFeePerKb, self.availableMaxBalance, self.feeToSendMaxStr);
}); });
} }
@ -1277,8 +1286,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r
pushNotificationsService.enableNotifications(); pushNotificationsService.enableNotifications();
}); });
$rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId) { $rootScope.$on('Local/UnsubscribeNotifications', function(event) {
pushNotificationsService.unsubscribe(walletId); pushNotificationsService.unsubscribe(null, function(err, response) {
if (err) $log.warn('Error: ' + err.code);
$log.debug('Unsubscribed: ' + response);
});
}); });
self.debouncedUpdate = lodash.throttle(function() { self.debouncedUpdate = lodash.throttle(function() {
@ -1482,5 +1494,5 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$rootScope.$apply(); $rootScope.$apply();
}); });
}); });
}); });

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, pushNotificationsService) { function($scope, $rootScope, $log, configService, isMobile, uxLanguage, pushNotificationsService) {
this.init = function() { this.init = function() {
var config = configService.getSync(); var config = configService.getSync();
@ -17,7 +17,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
$scope.notifications = config.notifications ? config.notifications.enabled : true; $scope.notifications = config.notifications ? config.notifications.enabled : true;
}; };
if (isCordova) $scope.mobile = true; if (isMobile.Android() || isMobile.iOS()) $scope.mobile = true;
else $scope.mobile = false; else $scope.mobile = false;
var unwatchSpendUnconfirmed = $scope.$watch('spendUnconfirmed', function(newVal, oldVal) { var unwatchSpendUnconfirmed = $scope.$watch('spendUnconfirmed', function(newVal, oldVal) {
@ -36,7 +36,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
var unwatchNotification = $scope.$watch('notifications', function(newVal, oldVal) { var unwatchNotification = $scope.$watch('notifications', function(newVal, oldVal) {
if (newVal == oldVal) return; if (newVal == oldVal) return;
var opts = { var opts = {
pushNotifications: { pushNotifications: {
enabled: newVal enabled: newVal
} }
}; };

View file

@ -304,6 +304,8 @@ angular.module('copayApp.services')
root.deleteWalletFC = function(opts, cb) { root.deleteWalletFC = function(opts, cb) {
var fc = root.focusedClient; var fc = root.focusedClient;
var walletId = fc.credentials.walletId; var walletId = fc.credentials.walletId;
$rootScope.$emit('Local/UnsubscribeNotifications');
$log.debug('Deleting Wallet:', fc.credentials.walletName); $log.debug('Deleting Wallet:', fc.credentials.walletName);
fc.removeAllListeners(); fc.removeAllListeners();
@ -326,8 +328,6 @@ angular.module('copayApp.services')
if (err) $log.warn(err); if (err) $log.warn(err);
}); });
$rootScope.$emit('Local/UnsubscribeNotifications', walletId);
$timeout(function() { $timeout(function() {
root.setWalletClients(); root.setWalletClients();
root.setAndStoreFocus(null, function() { root.setAndStoreFocus(null, function() {

View file

@ -34,43 +34,38 @@ angular.module('copayApp.services')
storageService.getDeviceToken(function(err, token) { storageService.getDeviceToken(function(err, token) {
lodash.forEach(profileService.getWallets('testnet'), function(wallets) { lodash.forEach(profileService.getWallets('testnet'), function(wallets) {
var opts = {}; var opts = {};
opts.user = wallets.id + '$' + wallets.copayerId;
opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null; opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null;
opts.token = token; opts.token = token;
root.subscribe(opts).then(function(response) { root.subscribe(opts, wallets.id, function(err, response) {
$log.debug('Suscribed: ' + response.status); if (err) $log.warn('Error: ' + err.code);
}, $log.debug('Suscribed: ' + JSON.stringify(response));
function(err) { });
$log.warn('Error: ' + err.status);
});
}); });
}); });
} }
root.disableNotifications = function() { root.disableNotifications = function() {
storageService.getDeviceToken(function(err, token) { storageService.getDeviceToken(function(err, token) {
root.unsubscribeAll(token).then(function(response) { root.unsubscribe(token, function(err, response) {
$log.debug('Unsubscribed: ' + response.status); if (err) $log.warn('Error: ' + err.code);
}, $log.debug('Unsubscribed: ' + response);
function(err) { });
$log.warn('Error: ' + err.status);
});
}); });
} }
root.subscribe = function(opts) { root.subscribe = function(opts, walletId, cb) {
return $http.post(defaults.pushNotifications.url + '/subscribe', opts); var walletClient = profileService.getClient(walletId);
} walletClient.pushNotificationsSubscribe(opts, function(err, resp) {
if (err) return cb(err);
root.unsubscribe = function(user) { return cb(null, resp);
return $http.post(defaults.pushNotifications.url + '/unsubscribe', {
user: user
}); });
} }
root.unsubscribeAll = function(token) { root.unsubscribe = function(opts, cb) {
return $http.post(defaults.pushNotifications.url + '/unsubscribe', { var walletClient = profileService.focusedClient;
token: token walletClient.pushNotificationsUnsubscribe(opts, function(err, resp) {
if (err) return cb(err);
return cb(null, resp);
}); });
} }