From d02afef341547ec4c686bb90f628924cda9a824c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Wed, 23 Dec 2015 15:33:56 -0300 Subject: [PATCH] addapt switch to suscribe/unsuscribe notifications --- src/js/controllers/index.js | 81 +++++++++++++-------- src/js/services/pushNotificationsService.js | 6 ++ src/js/services/storageService.js | 8 ++ 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index c6047c022..b048052db 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -13,46 +13,49 @@ angular.module('copayApp.controllers').controller('indexController', function($r document.addEventListener('deviceready', function() { - var push = PushNotification.init({ - android: { - senderID: "959259672122" - }, - ios: { - alert: "true", - badge: true, - sound: 'false' - }, - windows: {} - }); + if (isCordova) { + var push = PushNotification.init({ + android: { + senderID: "959259672122" + }, + ios: { + alert: "true", + badge: true, + sound: 'false' + }, + windows: {} + }); + } push.on('registration', function(data) { var opts = {}; opts.user = "Gabriel"; opts.type = "android"; opts.token = data.registrationId; - pushNotificationsService.subscribe(opts).then(function(response) { - console.log(response); - }, - function(err) { - console.log(err); - }); + storageService.setNotificationsOptions(JSON.stringify(opts), function() { + pushNotificationsService.subscribe(opts).then(function(response) { + $log.debug('Suscribed: ' + response.status); + }, + function(err) { + $log.warn('Error: ' + err); + }); + }); + }); push.on('notification', function(data) { - console.log("notification event"); - alert(data.message); - - // data.message, - // data.title, - // data.count, - // data.sound, - // data.image, - // data.additionalData + $log.debug('Notification event: ', data.message); + /* data.message, + data.title, + data.count, + data.sound, + data.image, + data.additionalData + */ }); push.on('error', function(e) { - console.log("error pushhhhhh"); - alert(e.message); + $log.warn('Error trying to push notifications: ', e); }); }); @@ -1243,9 +1246,25 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.updateAll(); }); - $rootScope.$on('Local/EnableNotifications', function(event, vale) { - //make suscribe/unsuscribe - self.updateAll(); + $rootScope.$on('Local/EnableNotifications', function(event, val) { + 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(); + }); }); $rootScope.$on('Local/FeeLevelUpdated', function(event, level) { diff --git a/src/js/services/pushNotificationsService.js b/src/js/services/pushNotificationsService.js index 3aed74598..73076c312 100644 --- a/src/js/services/pushNotificationsService.js +++ b/src/js/services/pushNotificationsService.js @@ -7,6 +7,12 @@ 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; }); diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index 3d00f82a9..175e06b52 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -231,6 +231,14 @@ angular.module('copayApp.services') 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) { storage.remove('addressbook-' + network, cb); };