push notification subscribe refactor
This commit is contained in:
parent
012fb8021b
commit
2d07cf0e55
4 changed files with 45 additions and 36 deletions
|
|
@ -13,28 +13,35 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
|
|
||||||
document.addEventListener('deviceready', function() {
|
document.addEventListener('deviceready', function() {
|
||||||
|
|
||||||
if (isCordova) {
|
if (self.isCordova) {
|
||||||
var push = PushNotification.init({
|
storageService.getDeviceToken(function(err, token) {
|
||||||
android: {
|
if (!token) pushNotificationInit();
|
||||||
senderID: "959259672122"
|
|
||||||
},
|
|
||||||
ios: {
|
|
||||||
alert: "true",
|
|
||||||
badge: true,
|
|
||||||
sound: 'false'
|
|
||||||
},
|
|
||||||
windows: {}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
function pushNotificationInit() {
|
||||||
|
var push = PushNotification.init({
|
||||||
|
android: {
|
||||||
|
senderID: "959259672122"
|
||||||
|
},
|
||||||
|
ios: {
|
||||||
|
alert: "true",
|
||||||
|
badge: true,
|
||||||
|
sound: 'false'
|
||||||
|
},
|
||||||
|
windows: {}
|
||||||
|
});
|
||||||
|
|
||||||
push.on('registration', function(data) {
|
push.on('registration', function(data) {
|
||||||
var fc = profileService.focusedClient;
|
var fc = profileService.focusedClient;
|
||||||
var opts = {};
|
var opts = {};
|
||||||
var deviceType = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null;
|
opts.user = fc.credentials.walletId;
|
||||||
opts.user = fc.credentials.copayerId + "||" + fc.credentials.walletId;
|
opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null;
|
||||||
opts.type = deviceType;
|
|
||||||
opts.token = data.registrationId;
|
opts.token = data.registrationId;
|
||||||
storageService.setNotificationsOptions(JSON.stringify(opts), function() {
|
|
||||||
|
storageService.setDeviceToken(data.registrationId, function() {
|
||||||
pushNotificationsService.subscribe(opts).then(function(response) {
|
pushNotificationsService.subscribe(opts).then(function(response) {
|
||||||
$log.debug('Suscribed: ' + response.status);
|
$log.debug('Suscribed: ' + response.status);
|
||||||
},
|
},
|
||||||
|
|
@ -59,8 +66,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
push.on('error', function(e) {
|
push.on('error', function(e) {
|
||||||
$log.warn('Error trying to push notifications: ', e);
|
$log.warn('Error trying to push notifications: ', e);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
|
||||||
|
|
||||||
function strip(number) {
|
function strip(number) {
|
||||||
return (parseFloat(number.toPrecision(12)));
|
return (parseFloat(number.toPrecision(12)));
|
||||||
|
|
@ -1249,8 +1255,13 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
});
|
});
|
||||||
|
|
||||||
$rootScope.$on('Local/EnableNotifications', function(event, val) {
|
$rootScope.$on('Local/EnableNotifications', function(event, val) {
|
||||||
storageService.getNotificationsOptions(function(err, opts) {
|
storageService.getDeviceToken(function(err, token) {
|
||||||
if (val.notifications.enabled) {
|
var fc = profileService.focusedClient;
|
||||||
|
var opts = {};
|
||||||
|
opts.user = [fc.credentials.walletId];
|
||||||
|
opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null;
|
||||||
|
opts.token = token;
|
||||||
|
if (val) {
|
||||||
pushNotificationsService.subscribe(opts).then(function(response) {
|
pushNotificationsService.subscribe(opts).then(function(response) {
|
||||||
$log.debug('Suscribed: ' + response.status);
|
$log.debug('Suscribed: ' + response.status);
|
||||||
},
|
},
|
||||||
|
|
@ -1258,7 +1269,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
||||||
$log.warn('Error: ' + err);
|
$log.warn('Error: ' + err);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
pushNotificationsService.unsubscribe(JSON.parse(opts).token).then(function(response) {
|
pushNotificationsService.unsubscribe(token).then(function(response) {
|
||||||
$log.debug('Unsuscribed: ' + response.status);
|
$log.debug('Unsuscribed: ' + response.status);
|
||||||
},
|
},
|
||||||
function(err) {
|
function(err) {
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
configService.set(opts, function(err) {
|
configService.set(opts, function(err) {
|
||||||
$rootScope.$emit('Local/EnableNotifications', opts);
|
$rootScope.$emit('Local/EnableNotifications', opts.notifications.enabled);
|
||||||
if (err) $log.debug(err);
|
if (err) $log.debug(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ angular.module('copayApp.services')
|
||||||
|
|
||||||
root.bindProfile = function(profile, cb) {
|
root.bindProfile = function(profile, cb) {
|
||||||
root.profile = profile;
|
root.profile = profile;
|
||||||
|
|
||||||
configService.get(function(err) {
|
configService.get(function(err) {
|
||||||
$log.debug('Preferences read');
|
$log.debug('Preferences read');
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
@ -128,20 +128,19 @@ angular.module('copayApp.services')
|
||||||
root._setFocus(focusedWalletId, function() {
|
root._setFocus(focusedWalletId, function() {
|
||||||
$rootScope.$emit('Local/ProfileBound');
|
$rootScope.$emit('Local/ProfileBound');
|
||||||
root.isDisclaimerAccepted(function(val) {
|
root.isDisclaimerAccepted(function(val) {
|
||||||
if (!val) {
|
if (!val) {
|
||||||
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
|
return cb(new Error('NONAGREEDDISCLAIMER: Non agreed disclaimer'));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
root.loadAndBindProfile = function(cb) {
|
root.loadAndBindProfile = function(cb) {
|
||||||
storageService.getProfile(function(err, profile) {
|
storageService.getProfile(function(err, profile) {
|
||||||
if (err) {
|
if (err) {
|
||||||
$rootScope.$emit('Local/DeviceError', err);
|
$rootScope.$emit('Local/DeviceError', err);
|
||||||
|
|
@ -398,6 +397,7 @@ angular.module('copayApp.services')
|
||||||
handleImport(function() {
|
handleImport(function() {
|
||||||
root.setAndStoreFocus(walletId, function() {
|
root.setAndStoreFocus(walletId, function() {
|
||||||
storageService.storeProfile(root.profile, function(err) {
|
storageService.storeProfile(root.profile, function(err) {
|
||||||
|
$rootScope.$emit('Local/EnableNotifications', true);
|
||||||
return cb(err, walletId);
|
return cb(err, walletId);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -542,16 +542,14 @@ angular.module('copayApp.services')
|
||||||
if (err) $log.error(err);
|
if (err) $log.error(err);
|
||||||
return cb(true);
|
return cb(true);
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return cb();
|
return cb();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
root.importLegacyWallet = function(username, password, blob, cb) {
|
root.importLegacyWallet = function(username, password, blob, cb) {
|
||||||
|
|
|
||||||
|
|
@ -231,12 +231,12 @@ angular.module('copayApp.services')
|
||||||
storage.get('addressbook-' + network, cb);
|
storage.get('addressbook-' + network, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
root.setNotificationsOptions = function(opts, cb) {
|
root.setDeviceToken = function(token, cb) {
|
||||||
storage.set('notifications', opts, cb);
|
storage.set('token', token, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
root.getNotificationsOptions = function(cb) {
|
root.getDeviceToken = function(cb) {
|
||||||
storage.get('notifications', cb);
|
storage.get('token', cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
root.removeAddressbook = function(network, cb) {
|
root.removeAddressbook = function(network, cb) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue