addapt switch to suscribe/unsuscribe notifications

This commit is contained in:
Gabriel Bazán 2015-12-23 15:33:56 -03:00
commit d02afef341
3 changed files with 64 additions and 31 deletions

View file

@ -13,6 +13,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
document.addEventListener('deviceready', function() { document.addEventListener('deviceready', function() {
if (isCordova) {
var push = PushNotification.init({ var push = PushNotification.init({
android: { android: {
senderID: "959259672122" senderID: "959259672122"
@ -24,35 +25,37 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}, },
windows: {} windows: {}
}); });
}
push.on('registration', function(data) { push.on('registration', function(data) {
var opts = {}; var opts = {};
opts.user = "Gabriel"; opts.user = "Gabriel";
opts.type = "android"; opts.type = "android";
opts.token = data.registrationId; opts.token = data.registrationId;
storageService.setNotificationsOptions(JSON.stringify(opts), function() {
pushNotificationsService.subscribe(opts).then(function(response) { pushNotificationsService.subscribe(opts).then(function(response) {
console.log(response); $log.debug('Suscribed: ' + response.status);
}, },
function(err) { function(err) {
console.log(err); $log.warn('Error: ' + err);
}); });
}); });
});
push.on('notification', function(data) { push.on('notification', function(data) {
console.log("notification event"); $log.debug('Notification event: ', data.message);
alert(data.message); /* data.message,
data.title,
// data.message, data.count,
// data.title, data.sound,
// data.count, data.image,
// data.sound, data.additionalData
// data.image, */
// data.additionalData
}); });
push.on('error', function(e) { push.on('error', function(e) {
console.log("error pushhhhhh"); $log.warn('Error trying to push notifications: ', e);
alert(e.message);
}); });
}); });
@ -1243,10 +1246,26 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateAll(); self.updateAll();
}); });
$rootScope.$on('Local/EnableNotifications', function(event, vale) { $rootScope.$on('Local/EnableNotifications', function(event, val) {
//make suscribe/unsuscribe storageService.getNotificationsOptions(function(err, opts) {
if (val.notifications.enabled) {
pushNotificationsService.subscribe(opts).then(function(response) {
$log.debug('Suscribed: ' + response.status);
},
function(err) {
$log.warn('Error: ' + err);
});
} else {
pushNotificationsService.unsubscribe(JSON.parse(opts).token).then(function(response) {
$log.debug('Unsuscribed: ' + response.status);
},
function(err) {
$log.warn('Error: ' + err);
});
}
self.updateAll(); self.updateAll();
}); });
});
$rootScope.$on('Local/FeeLevelUpdated', function(event, level) { $rootScope.$on('Local/FeeLevelUpdated', function(event, level) {
self.setCurrentFeeLevel(level); self.setCurrentFeeLevel(level);

View file

@ -7,6 +7,12 @@
return $http.post('http://192.168.1.120:8000/subscribe', opts); return $http.post('http://192.168.1.120:8000/subscribe', opts);
} }
root.unsubscribe = function(token) {
return $http.post('http://192.168.1.120:8000/unsubscribe', {
token: token
});
}
return root; return root;
}); });

View file

@ -231,6 +231,14 @@ angular.module('copayApp.services')
storage.get('addressbook-' + network, cb); storage.get('addressbook-' + network, cb);
}; };
root.setNotificationsOptions = function(opts, cb) {
storage.set('notifications', opts, cb);
}
root.getNotificationsOptions = function(cb) {
storage.get('notifications', cb);
}
root.removeAddressbook = function(network, cb) { root.removeAddressbook = function(network, cb) {
storage.remove('addressbook-' + network, cb); storage.remove('addressbook-' + network, cb);
}; };