New push notifications

This commit is contained in:
Gustavo Maximiliano Cortez 2017-02-03 18:03:29 -03:00
commit 4e8bd0634c
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
13 changed files with 139 additions and 153 deletions

4
.gitignore vendored
View file

@ -118,3 +118,7 @@ cordova/ios/splash/
cordova/ios/icons/ cordova/ios/icons/
cordova/project/ cordova/project/
## Firebase
GoogleService-Info.plist
google-services.json

View file

@ -1,6 +1,7 @@
{ {
"packageName": "bitpay", "packageName": "bitpay",
"packageDescription": "Secure Bitcoin Wallet", "packageDescription": "Secure Bitcoin Wallet",
"packageNameId": "com.bitpay.wallet",
"userVisibleName": "BitPay", "userVisibleName": "BitPay",
"purposeLine": "Secure Bitcoin Wallet", "purposeLine": "Secure Bitcoin Wallet",
"bundleName": "wallet", "bundleName": "wallet",

View file

@ -61,12 +61,11 @@
<variable name="URL_SCHEME" value="bitcoin" /> <variable name="URL_SCHEME" value="bitcoin" />
<variable name="SECOND_URL_SCHEME" value="*APPURI*" /> <variable name="SECOND_URL_SCHEME" value="*APPURI*" />
</plugin> </plugin>
<plugin name="phonegap-plugin-push" spec="~1.8.2">
<variable name="SENDER_ID" value="*PUSHSENDERID*"/>
</plugin>
<plugin name="cordova-custom-config" spec="~3.0.5" /> <plugin name="cordova-custom-config" spec="~3.0.5" />
<plugin name="cordova-plugin-queries-schemes" spec="~0.1.5" /> <plugin name="cordova-plugin-queries-schemes" spec="~0.1.5" />
<plugin name="cordova-plugin-fcm" spec="2.1.1" />
<!-- Supported Platforms --> <!-- Supported Platforms -->
<engine name="ios" spec="~4.2.1" /> <engine name="ios" spec="~4.2.1" />
<engine name="android" spec="~5.2.2" /> <engine name="android" spec="~5.2.2" />

View file

@ -1,6 +1,7 @@
{ {
"packageName": "copay", "packageName": "copay",
"packageDescription": "Copay Bitcoin Wallet", "packageDescription": "Copay Bitcoin Wallet",
"packageNameId": "com.bitpay.copay",
"userVisibleName": "Copay", "userVisibleName": "Copay",
"purposeLine": "Copay Bitcoin Wallet", "purposeLine": "Copay Bitcoin Wallet",
"bundleName": "copay", "bundleName": "copay",

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('advancedSettingsController', function($scope, $rootScope, $log, $window, lodash, configService, uxLanguage, platformInfo, pushNotificationsService, profileService, feeService, storageService, $ionicHistory, $timeout, $ionicScrollDelegate) { angular.module('copayApp.controllers').controller('advancedSettingsController', function($scope, $rootScope, $log, $window, lodash, configService, uxLanguage, platformInfo, profileService, feeService, storageService, $ionicHistory, $timeout, $ionicScrollDelegate) {
var updateConfig = function() { var updateConfig = function() {
var config = configService.getSync(); var config = configService.getSync();

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('collectEmailController', function($scope, $state, $timeout, $stateParams, $ionicConfig, profileService, configService, walletService, platformInfo) { angular.module('copayApp.controllers').controller('collectEmailController', function($scope, $state, $timeout, $stateParams, $ionicConfig, profileService, configService, walletService, platformInfo, pushNotificationsService) {
$scope.$on("$ionicView.beforeLeave", function() { $scope.$on("$ionicView.beforeLeave", function() {
$ionicConfig.views.swipeBackEnabled(true); $ionicConfig.views.swipeBackEnabled(true);
@ -46,7 +46,7 @@ angular.module('copayApp.controllers').controller('collectEmailController', func
walletId: walletId walletId: walletId
}); });
} else { } else {
profileService.pushNotificationsInit(); pushNotificationsService.init();
$state.go('onboarding.backupRequest', { $state.go('onboarding.backupRequest', {
walletId: walletId walletId: walletId
}); });

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('notificationsController', function($scope, $state, $timeout, $stateParams, $ionicConfig, profileService, configService, $interval) { angular.module('copayApp.controllers').controller('notificationsController', function($scope, $state, $timeout, $stateParams, $ionicConfig, profileService, configService, $interval, pushNotificationsService) {
$scope.$on("$ionicView.enter", function() { $scope.$on("$ionicView.enter", function() {
$ionicConfig.views.swipeBackEnabled(false); $ionicConfig.views.swipeBackEnabled(false);
@ -15,7 +15,7 @@ angular.module('copayApp.controllers').controller('notificationsController', fun
$scope.allowNotif = function() { $scope.allowNotif = function() {
$scope.notificationDialogOpen = true; $scope.notificationDialogOpen = true;
$timeout(function() { $timeout(function() {
profileService.pushNotificationsInit(); pushNotificationsService.init();
}); });
$scope.notificationPromise = $interval(function() { $scope.notificationPromise = $interval(function() {
PushNotification.hasPermission(function(data) { PushNotification.hasPermission(function(data) {

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('preferencesDeleteWalletController', angular.module('copayApp.controllers').controller('preferencesDeleteWalletController',
function($scope, $ionicHistory, gettextCatalog, lodash, profileService, $state, ongoingProcess, popupService) { function($scope, $ionicHistory, gettextCatalog, lodash, profileService, $state, ongoingProcess, popupService, pushNotificationsService) {
$scope.$on("$ionicView.beforeEnter", function(event, data) { $scope.$on("$ionicView.beforeEnter", function(event, data) {
if (!data.stateParams || !data.stateParams.walletId) { if (!data.stateParams || !data.stateParams.walletId) {
@ -36,6 +36,7 @@ angular.module('copayApp.controllers').controller('preferencesDeleteWalletContro
if (err) { if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err.message || err); popupService.showAlert(gettextCatalog.getString('Error'), err.message || err);
} else { } else {
pushNotificationsService.unsubscribe($scope.wallet);
$ionicHistory.nextViewOptions({ $ionicHistory.nextViewOptions({
disableAnimate: true, disableAnimate: true,
historyRoot: true historyRoot: true

View file

@ -9,7 +9,7 @@ angular.module('copayApp.controllers').controller('preferencesNotificationsContr
$scope.isIOSApp = platformInfo.isIOS && platformInfo.isCordova; $scope.isIOSApp = platformInfo.isIOS && platformInfo.isCordova;
$scope.pushNotifications = { $scope.pushNotifications = {
value: config.pushNotifications.enabled value: config.pushNotificationsEnabled
}; };
$scope.latestEmail = { $scope.latestEmail = {
@ -31,16 +31,14 @@ angular.module('copayApp.controllers').controller('preferencesNotificationsContr
$scope.pushNotificationsChange = function() { $scope.pushNotificationsChange = function() {
if (!$scope.pushNotifications) return; if (!$scope.pushNotifications) return;
var opts = { var opts = {
pushNotifications: { pushNotificationsEnabled: $scope.pushNotifications.value
enabled: $scope.pushNotifications.value
}
}; };
configService.set(opts, function(err) { configService.set(opts, function(err) {
if (opts.pushNotifications.enabled)
profileService.pushNotificationsInit();
else
pushNotificationsService.disableNotifications(profileService.getWallets());
if (err) $log.debug(err); if (err) $log.debug(err);
if (opts.pushNotificationsEnabled)
pushNotificationsService.init();
else
pushNotificationsService.disable();
}); });
}; };

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('tabHomeController', angular.module('copayApp.controllers').controller('tabHomeController',
function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, $window, gettextCatalog, lodash, popupService, ongoingProcess, externalLinkService, latestReleaseService, profileService, walletService, configService, $log, platformInfo, storageService, txpModalService, appConfigService, startupService, addressbookService, feedbackService, bwcError, nextStepsService, buyAndSellService, homeIntegrationsService, bitpayCardService) { function($rootScope, $timeout, $scope, $state, $stateParams, $ionicModal, $ionicScrollDelegate, $window, gettextCatalog, lodash, popupService, ongoingProcess, externalLinkService, latestReleaseService, profileService, walletService, configService, $log, platformInfo, storageService, txpModalService, appConfigService, startupService, addressbookService, feedbackService, bwcError, nextStepsService, buyAndSellService, homeIntegrationsService, bitpayCardService, pushNotificationsService) {
var wallet; var wallet;
var listeners = []; var listeners = [];
var notifications = []; var notifications = [];
@ -105,8 +105,7 @@ angular.module('copayApp.controllers').controller('tabHomeController',
$scope.bitpayCardItems = cards; $scope.bitpayCardItems = cards;
}); });
configService.whenAvailable(function() { configService.whenAvailable(function(config) {
var config = configService.getSync();
$scope.recentTransactionsEnabled = config.recentTransactions.enabled; $scope.recentTransactionsEnabled = config.recentTransactions.enabled;
if ($scope.recentTransactionsEnabled) getNotifications(); if ($scope.recentTransactionsEnabled) getNotifications();
@ -116,6 +115,8 @@ angular.module('copayApp.controllers').controller('tabHomeController',
$scope.nextStepsItems = nextStepsService.get(); $scope.nextStepsItems = nextStepsService.get();
} }
pushNotificationsService.init();
$timeout(function() { $timeout(function() {
$ionicScrollDelegate.resize(); $ionicScrollDelegate.resize();
$scope.$apply(); $scope.$apply();

View file

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

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
angular.module('copayApp.services') 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; var isChromeApp = platformInfo.isChromeApp;
@ -274,9 +274,6 @@ angular.module('copayApp.services')
if (!val) { if (!val) {
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer')); return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
} }
var config = configService.getSync();
if (config.pushNotifications.enabled && usePushNotifications)
root.pushNotificationsInit();
return cb(); return cb();
}); });
}); });
@ -292,35 +289,6 @@ angular.module('copayApp.services')
return cb(); 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) { root.loadAndBindProfile = function(cb) {
storageService.getProfile(function(err, profile) { storageService.getProfile(function(err, profile) {
if (err) { if (err) {
@ -481,11 +449,6 @@ angular.module('copayApp.services')
var walletId = client.credentials.walletId; var walletId = client.credentials.walletId;
var config = configService.getSync(); 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); $log.debug('Deleting Wallet:', client.credentials.walletName);
client.removeAllListeners(); client.removeAllListeners();
@ -556,9 +519,6 @@ angular.module('copayApp.services')
saveBwsUrl(function() { saveBwsUrl(function() {
storageService.storeProfile(root.profile, function(err) { storageService.storeProfile(root.profile, function(err) {
var config = configService.getSync();
if (config.pushNotifications.enabled)
pushNotificationsService.enableNotifications(root.wallet);
return cb(err, client); return cb(err, client);
}); });
}); });

View file

@ -1,87 +1,123 @@
'use strict'; 'use strict';
angular.module('copayApp.services') angular.module('copayApp.services').factory('pushNotificationsService', function pushNotificationsService($log, $state, $ionicHistory, platformInfo, lodash, appConfigService, profileService, configService) {
.factory('pushNotificationsService', function($log, platformInfo, storageService, configService, lodash) { var root = {};
var root = {}; var isIOS = platformInfo.isIOS;
var isCordova = platformInfo.isCordova; var isAndroid = platformInfo.isAndroid;
var isWP = platformInfo.isWP; var usePushNotifications = platformInfo.isCordova && !platformInfo.isWP;
var isIOS = platformInfo.isIOS;
var isAndroid = platformInfo.isAndroid;
var usePushNotifications = isCordova && !isWP; var _token = null;
root.init = function(walletsClients) { root.init = function() {
var defaults = configService.getDefaults(); if (!usePushNotifications || _token) return;
try { configService.whenAvailable(function(config) {
var push = PushNotification.init(defaults.pushNotifications.config); if (!config.pushNotificationsEnabled) return;
} catch(e) {
$log.error(e); $log.debug('Starting push notification registration...');
return;
};
push.on('registration', function(data) { //Keep in mind the function will return null if the token has not been established yet.
$log.debug('Starting push notification registration'); FCMPlugin.getToken(function(token) {
root.token = data.registrationId; $log.debug('Get token for push notifications...');
var config = configService.getSync(); _token = token;
if (config.pushNotifications.enabled) root.enableNotifications(walletsClients); 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) { var wallets = profileService.getWallets();
if (!usePushNotifications) return; lodash.forEach(wallets, function(walletClient) {
_subscribe(walletClient);
});
};
var config = configService.getSync(); root.disable = function() {
if (!config.pushNotifications.enabled) return; if (!_token) {
$log.warn('No token available for this device. Cannot disable push notifications.');
return;
}
if (!root.token) { var wallets = profileService.getWallets();
$log.warn('No token available for this device. Cannot set push notifications. Needs registration.'); lodash.forEach(wallets, function(walletClient) {
return; _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(walletId) {
var wallet = profileService.getWallet(walletId);
if (!wallet) return;
if (!wallet.isComplete()) {
return $state.go('tabs.copayers', {
walletId: walletId
});
}
$state.go('tabs.wallet', {
walletId: walletId
});
};
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));
var walletId, copayerId;
if(data.wasTapped) {
// Notification was received on device tray and tapped by the user.
var walletId = data ? data.walletId : null;
if (!walletId) return;
$ionicHistory.nextViewOptions({
disableAnimate: true,
historyRoot: true
});
$ionicHistory.clearHistory();
$state.go('tabs.home').then(function() {
_openWallet(walletId);
});
} else {
// TODO
// Notification was received in foreground. Maybe the user needs to be notified.
} }
});
}
lodash.forEach(walletsClients, function(walletClient) { return root;
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));
});
});
}
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;
});