refactor push object listeners and calls

This commit is contained in:
Javier 2016-03-10 10:58:58 -03:00
commit e180d0bbd4
3 changed files with 46 additions and 67 deletions

View file

@ -2,25 +2,8 @@
angular.module('copayApp.services')
.factory('pushNotificationsService', function($http, $rootScope, $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;
var push = PushNotification.init(defaults.pushNotifications.config);
push.on('registration', function(data) {
$log.debug('Starting push notification registration');
storageService.setDeviceToken(data.registrationId, function() {
$rootScope.$emit('Local/pushNotificationsReady');
});
});
};
root.enableNotifications = function(walletsClients) {
if (!usePushNotifications) return;
@ -28,19 +11,17 @@ angular.module('copayApp.services')
if (!config.pushNotifications.enabled) return;
storageService.getDeviceToken(function(err, token) {
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));
if (err) $log.warn('Subscription error: ' + err.message + ': ' + JSON.stringify(opts));
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
});
});