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';
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 SOFT_CONFIRMATION_LIMIT = 12;
self.isCordova = isCordova;
@ -12,7 +12,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.prevState = 'walletHome';
document.addEventListener('deviceready', function() {
if (self.isCordova) {
if (isMobile.Android() || isMobile.iOS()) {
storageService.getDeviceToken(function(err, token) {
$timeout(function() {
if (!token) pushNotificationsService.pushNotificationsInit();
@ -31,15 +31,24 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.menu = [{
'title': gettext('Receive'),
'icon': {false:'icon-receive', true: 'icon-receive-active'},
'icon': {
false: 'icon-receive',
true: 'icon-receive-active'
},
'link': 'receive'
}, {
'title': gettext('Activity'),
'icon': {false:'icon-activity',true: 'icon-activity-active'},
'icon': {
false: 'icon-activity',
true: 'icon-activity-active'
},
'link': 'walletHome'
}, {
'title': gettext('Send'),
'icon': {false:'icon-send', true: 'icon-send-active'},
'icon': {
false: 'icon-send',
true: 'icon-send-active'
},
'link': 'send'
}];
@ -403,7 +412,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.availableMaxBalance = strip((self.availableBalanceSat - feeToSendMaxSat) * self.satToUnit);
self.feeToSendMaxStr = profileService.formatAmount(feeToSendMaxSat) + ' ' + self.unitName;
}
if (cb) return cb(self.currentFeePerKb, self.availableMaxBalance, self.feeToSendMaxStr);
});
}
@ -1277,8 +1286,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r
pushNotificationsService.enableNotifications();
});
$rootScope.$on('Local/UnsubscribeNotifications', function(event, walletId) {
pushNotificationsService.unsubscribe(walletId);
$rootScope.$on('Local/UnsubscribeNotifications', function(event) {
pushNotificationsService.unsubscribe(null, function(err, response) {
if (err) $log.warn('Error: ' + err.code);
$log.debug('Unsubscribed: ' + response);
});
});
self.debouncedUpdate = lodash.throttle(function() {
@ -1482,5 +1494,5 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$rootScope.$apply();
});
});
});

View file

@ -1,7 +1,7 @@
'use strict';
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() {
var config = configService.getSync();
@ -17,7 +17,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
$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;
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) {
if (newVal == oldVal) return;
var opts = {
pushNotifications: {
pushNotifications: {
enabled: newVal
}
};

View file

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

View file

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