Merge pull request #3988 from JDonadio/fix/tap-on-notification-03

Fix tap on notification
This commit is contained in:
Gustavo Maximiliano Cortez 2016-04-11 10:15:03 -03:00
commit 9a10394cd9
6 changed files with 66 additions and 64 deletions

View file

@ -1,9 +1,10 @@
'use strict';
angular.module('copayApp.services')
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, pushNotificationsService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, bitcore) {
.factory('profileService', function profileServiceFactory($rootScope, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, notificationService, pushNotificationsService, isChromeApp, isCordova, isMobile, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, bitcore) {
var root = {};
var errors = bwcService.getErrors();
var usePushNotifications = isCordova && !isMobile.Windows();
var FOREGROUND_UPDATE_PERIOD = 5;
var BACKGROUND_UPDATE_PERIOD = 30;
@ -133,22 +134,42 @@ angular.module('copayApp.services')
if (err) return cb(err);
root._setFocus(focusedWalletId, function() {
$rootScope.$emit('Local/ProfileBound');
storageService.getDeviceToken(function(err, token) {
if (!token)
pushNotificationsService.pushNotificationsInit();
root.isDisclaimerAccepted(function(val) {
if (!val) {
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
} else {
return cb();
}
});
if (usePushNotifications)
root.pushNotificationsInit();
root.isDisclaimerAccepted(function(val) {
if (!val) {
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
} else {
return cb();
}
});
});
});
});
};
root.pushNotificationsInit = function() {
var defaults = configService.getDefaults();
var push = pushNotificationsService.init(root.walletClients);
push.on('notification', function(data) {
if (!data.additionalData.foreground) {
window.ignoreMobilePause = true;
$log.debug('Push notification event: ', data.message);
$timeout(function() {
var wallets = root.getWallets();
var walletToFind = data.additionalData.walletId;
var walletFound = lodash.find(wallets, function(w) {
return (lodash.isEqual(walletToFind, sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(w.id))));
});
if (!walletFound) return $log.debug('Wallet not found');
root.setAndStoreFocus(walletFound.id, function() {});
}, 100);
}
});
};

View file

@ -1,25 +1,23 @@
'use strict';
angular.module('copayApp.services')
.factory('pushNotificationsService', function($http, $rootScope, $log, isMobile, storageService, configService, lodash, isCordova) {
.factory('pushNotificationsService', function($log, isMobile, storageService, configService, lodash, isCordova) {
var root = {};
var defaults = configService.getDefaults();
var usePushNotifications = isCordova && !isMobile.Windows();
root.pushNotificationsInit = function() {
if (!usePushNotifications) return;
var config = configService.getSync();
if (!config.pushNotifications.enabled) return;
root.init = function(walletsClients) {
var defaults = configService.getDefaults();
var push = PushNotification.init(defaults.pushNotifications.config);
push.on('registration', function(data) {
if (root.token) return;
$log.debug('Starting push notification registration');
storageService.setDeviceToken(data.registrationId, function() {
$rootScope.$emit('Local/pushNotificationsReady');
});
root.token = data.registrationId;
var config = configService.getSync();
if (config.pushNotifications.enabled) root.enableNotifications(walletsClients);
});
};
return push;
}
root.enableNotifications = function(walletsClients) {
if (!usePushNotifications) return;
@ -27,22 +25,18 @@ angular.module('copayApp.services')
var config = configService.getSync();
if (!config.pushNotifications.enabled) return;
storageService.getDeviceToken(function(err, token) {
if (!root.token) {
$log.warn('No token available for this device. Cannot set push notifications');
return;
}
if (err || !token) {
$log.warn('No token available for this device. Cannot set push notifications');
return;
}
lodash.forEach(walletsClients, function(walletClient) {
var opts = {};
opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null;
opts.token = token;
root.subscribe(opts, walletClient, function(err, response) {
if (err) $log.warn('Subscription error: ' + err.message + ': ' + JSON.stringify(opts));
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
});
lodash.forEach(walletsClients, function(walletClient) {
var opts = {};
opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null;
opts.token = root.token;
root.subscribe(opts, walletClient, function(err, response) {
if (err) $log.warn('Subscription error: ' + err.message + ': ' + JSON.stringify(opts));
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
});
});
}

View file

@ -231,14 +231,6 @@ angular.module('copayApp.services')
storage.get('addressbook-' + network, cb);
};
root.setDeviceToken = function(token, cb) {
storage.set('pushToken', token, cb);
}
root.getDeviceToken = function(cb) {
storage.get('pushToken', cb);
}
root.removeAddressbook = function(network, cb) {
storage.remove('addressbook-' + network, cb);
};