Merge pull request #5564 from cmgustavo/feat/new-pushnotifications-01

Feat/new pushnotifications 01
This commit is contained in:
Matias Alejo Garcia 2017-03-10 17:12:27 -03:00 committed by GitHub
commit 1c01a99945
14 changed files with 147 additions and 154 deletions

View file

@ -70,22 +70,7 @@ angular.module('copayApp.services').factory('configService', function(storageSer
url: 'https://api.github.com/repos/bitpay/copay/releases/latest'
},
pushNotifications: {
enabled: true,
config: {
android: {
senderID: '1036948132229',
icon: 'push',
iconColor: '#2F4053'
},
ios: {
alert: 'true',
badge: 'true',
sound: 'true',
},
windows: {},
}
},
pushNotificationsEnabled: true,
emailNotifications: {
enabled: false,

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.services')
.factory('profileService', function profileServiceFactory($rootScope, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, pushNotificationsService, gettextCatalog, bwcError, uxLanguage, platformInfo, txFormatService, $state) {
.factory('profileService', function profileServiceFactory($rootScope, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, gettextCatalog, bwcError, uxLanguage, platformInfo, txFormatService, $state) {
var isChromeApp = platformInfo.isChromeApp;
@ -274,9 +274,6 @@ angular.module('copayApp.services')
if (!val) {
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
}
var config = configService.getSync();
if (config.pushNotifications.enabled && usePushNotifications)
root.pushNotificationsInit();
return cb();
});
});
@ -292,35 +289,6 @@ angular.module('copayApp.services')
return cb();
};
root.pushNotificationsInit = function() {
var defaults = configService.getDefaults();
var push = pushNotificationsService.init(root.wallet);
if (!push) return;
push.on('notification', function(data) {
if (!data.additionalData.foreground) {
$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');
}, 100);
}
});
push.on('error', function(e) {
$log.warn('Error with push notifications:' + e.message);
});
};
root.loadAndBindProfile = function(cb) {
storageService.getProfile(function(err, profile) {
if (err) {
@ -481,11 +449,6 @@ angular.module('copayApp.services')
var walletId = client.credentials.walletId;
var config = configService.getSync();
if (config.pushNotifications.enabled)
pushNotificationsService.unsubscribe(root.getWallet(walletId), function(err) {
if (err) $log.warn('Unsubscription error: ' + err.message);
else $log.debug('Unsubscribed from push notifications service');
});
$log.debug('Deleting Wallet:', client.credentials.walletName);
client.removeAllListeners();
@ -556,9 +519,6 @@ angular.module('copayApp.services')
saveBwsUrl(function() {
storageService.storeProfile(root.profile, function(err) {
var config = configService.getSync();
if (config.pushNotifications.enabled)
pushNotificationsService.enableNotifications(root.wallet);
return cb(err, client);
});
});

View file

@ -1,87 +1,130 @@
'use strict';
angular.module('copayApp.services')
.factory('pushNotificationsService', function($log, platformInfo, storageService, configService, lodash) {
var root = {};
var isCordova = platformInfo.isCordova;
var isWP = platformInfo.isWP;
var isIOS = platformInfo.isIOS;
var isAndroid = platformInfo.isAndroid;
angular.module('copayApp.services').factory('pushNotificationsService', function pushNotificationsService($log, $state, $ionicHistory, sjcl, platformInfo, lodash, appConfigService, profileService, configService) {
var root = {};
var isIOS = platformInfo.isIOS;
var isAndroid = platformInfo.isAndroid;
var usePushNotifications = platformInfo.isCordova && !platformInfo.isWP;
var usePushNotifications = isCordova && !isWP;
var _token = null;
root.init = function(walletsClients) {
var defaults = configService.getDefaults();
try {
var push = PushNotification.init(defaults.pushNotifications.config);
} catch(e) {
$log.error(e);
return;
};
root.init = function() {
if (!usePushNotifications || _token) return;
configService.whenAvailable(function(config) {
if (!config.pushNotificationsEnabled) return;
$log.debug('Starting push notification registration...');
push.on('registration', function(data) {
$log.debug('Starting push notification registration');
root.token = data.registrationId;
var config = configService.getSync();
if (config.pushNotifications.enabled) root.enableNotifications(walletsClients);
});
//Keep in mind the function will return null if the token has not been established yet.
FCMPlugin.getToken(function(token) {
$log.debug('Get token for push notifications: ' + token);
_token = token;
root.enable();
});
});
};
return push;
root.enable = function() {
if (!_token) {
$log.warn('No token available for this device. Cannot set push notifications. Needs registration.');
return;
}
root.enableNotifications = function(walletsClients) {
if (!usePushNotifications) return;
var wallets = profileService.getWallets();
lodash.forEach(wallets, function(walletClient) {
_subscribe(walletClient);
});
};
var config = configService.getSync();
if (!config.pushNotifications.enabled) return;
root.disable = function() {
if (!_token) {
$log.warn('No token available for this device. Cannot disable push notifications.');
return;
}
if (!root.token) {
$log.warn('No token available for this device. Cannot set push notifications. Needs registration.');
return;
var wallets = profileService.getWallets();
lodash.forEach(wallets, function(walletClient) {
_unsubscribe(walletClient);
});
_token = null;
};
root.unsubscribe = function(walletClient) {
if (!_token) return;
_unsubscribe(walletClient);
};
var _subscribe = function(walletClient) {
var opts = {
token : _token,
platform: isIOS ? 'ios' : isAndroid ? 'android' : null,
packageName : appConfigService.packageNameId
};
walletClient.pushNotificationsSubscribe(opts, function(err) {
if (err) $log.error(walletClient.name + ': Subscription Push Notifications error. ', JSON.stringify(err));
else $log.debug(walletClient.name + ': Subscription Push Notifications success.');
});
};
var _unsubscribe = function(walletClient, cb) {
walletClient.pushNotificationsUnsubscribe(_token, function(err) {
if (err) $log.error(walletClient.name + ': Unsubscription Push Notifications error. ', JSON.stringify(err));
else $log.debug(walletClient.name + ': Unsubscription Push Notifications Success.');
});
};
var _openWallet = function(walletIdHashed) {
var wallets = profileService.getWallets();
var wallet = lodash.find(wallets, function(w) {
return (lodash.isEqual(walletIdHashed, sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(w.id))));
});
if (!wallet) return;
if (!wallet.isComplete()) {
return $state.go('tabs.copayers', {
walletId: wallet.id
});
}
$state.go('tabs.wallet', {
walletId: wallet.id
});
};
if (usePushNotifications) {
FCMPlugin.onTokenRefresh(function(token) {
if (!_token) return;
$log.debug('Refresh and update token for push notifications...');
_token = token;
root.enable();
});
FCMPlugin.onNotification(function(data) {
if (!_token) return;
$log.debug('New Event Push onNotification: ' + JSON.stringify(data));
if(data.wasTapped) {
// Notification was received on device tray and tapped by the user.
var walletIdHashed = data.walletId;
if (!walletIdHashed) return;
$ionicHistory.nextViewOptions({
disableAnimate: true,
historyRoot: true
});
$ionicHistory.clearHistory();
$state.go('tabs.home', {}, {
'reload': true,
'notify': $state.current.name == 'tabs.home' ? false : true
}).then(function() {
_openWallet(walletIdHashed);
});
} else {
// TODO
// Notification was received in foreground. Maybe the user needs to be notified.
}
});
}
lodash.forEach(walletsClients, function(walletClient) {
var opts = {};
opts.type = isIOS ? "ios" : isAndroid ? "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));
});
});
}
return root;
root.disableNotifications = function(walletsClients) {
if (!usePushNotifications) return;
lodash.forEach(walletsClients, function(walletClient) {
root.unsubscribe(walletClient, function(err) {
if (err) $log.warn('Unsubscription error: ' + err.message);
else $log.debug('Unsubscribed from push notifications service');
});
});
}
root.subscribe = function(opts, walletClient, cb) {
if (!usePushNotifications) return cb();
var config = configService.getSync();
if (!config.pushNotifications.enabled) return;
walletClient.pushNotificationsSubscribe(opts, function(err, resp) {
if (err) return cb(err);
return cb(null, resp);
});
}
root.unsubscribe = function(walletClient, cb) {
if (!usePushNotifications) return cb();
walletClient.pushNotificationsUnsubscribe(function(err) {
if (err) return cb(err);
return cb(null);
});
}
return root;
});
});