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,46 +13,49 @@ angular.module('copayApp.controllers').controller('indexController', function($r
document.addEventListener('deviceready', function() { document.addEventListener('deviceready', function() {
var push = PushNotification.init({ if (isCordova) {
android: { var push = PushNotification.init({
senderID: "959259672122" android: {
}, senderID: "959259672122"
ios: { },
alert: "true", ios: {
badge: true, alert: "true",
sound: 'false' badge: true,
}, sound: 'false'
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;
pushNotificationsService.subscribe(opts).then(function(response) { storageService.setNotificationsOptions(JSON.stringify(opts), function() {
console.log(response); pushNotificationsService.subscribe(opts).then(function(response) {
}, $log.debug('Suscribed: ' + response.status);
function(err) { },
console.log(err); function(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,9 +1246,25 @@ 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) {
self.updateAll(); 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();
});
}); });
$rootScope.$on('Local/FeeLevelUpdated', function(event, level) { $rootScope.$on('Local/FeeLevelUpdated', function(event, 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);
}; };