Wallet/src/js/services/pushNotificationsService.js

76 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-01-04 14:11:24 -03:00
'use strict';
angular.module('copayApp.services')
.factory('pushNotificationsService', function($http, $log, isMobile, profileService, storageService, configService, lodash) {
2016-01-04 14:11:24 -03:00
var root = {};
var defaults = configService.getDefaults();
2015-12-23 12:29:46 -03:00
2016-01-04 14:11:24 -03:00
root.pushNotificationsInit = function() {
var push = PushNotification.init(defaults.pushNotifications.config);
2016-01-04 14:11:24 -03:00
push.on('registration', function(data) {
$log.debug('Starting push notification registration');
2016-01-04 14:11:24 -03:00
storageService.setDeviceToken(data.registrationId, function() {
root.enableNotifications();
});
});
2015-12-23 12:29:46 -03:00
2016-01-04 14:11:24 -03:00
push.on('notification', function(data) {
$log.debug('Push notification event: ', data.message);
2016-01-04 14:11:24 -03:00
/* data.message,
data.title,
data.count,
data.sound,
data.image,
data.additionalData
*/
});
push.on('error', function(e) {
$log.warn('Error trying to push notifications: ', e);
});
};
root.enableNotifications = function() {
storageService.getDeviceToken(function(err, token) {
2016-01-15 17:09:50 -03:00
lodash.forEach(profileService.getWallets('testnet'), function(wallet) {
2016-01-04 14:11:24 -03:00
var opts = {};
opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null;
2016-01-04 14:11:24 -03:00
opts.token = token;
2016-01-15 17:09:50 -03:00
root.subscribe(opts, wallet.id, function(err, response) {
if (err) $log.warn('Error: ' + err.code);
$log.debug('Suscribed: ' + JSON.stringify(response));
});
2016-01-04 14:11:24 -03:00
});
});
}
root.disableNotifications = function() {
storageService.getDeviceToken(function(err, token) {
2016-01-15 17:09:50 -03:00
lodash.forEach(profileService.getWallets('testnet'), function(wallet) {
root.unsubscribe(token, wallet.id, function(err, response) {
if (err) $log.warn('Error: ' + err.code);
$log.debug('Unsubscribed: ' + response);
});
});
2016-01-04 14:11:24 -03:00
});
}
root.subscribe = function(opts, walletId, cb) {
var walletClient = profileService.getClient(walletId);
walletClient.pushNotificationsSubscribe(opts, function(err, resp) {
if (err) return cb(err);
return cb(null, resp);
2016-01-04 16:35:34 -03:00
});
}
2016-01-15 17:09:50 -03:00
root.unsubscribe = function(token, walletId, cb) {
var walletClient = profileService.getClient(walletId);
walletClient.pushNotificationsUnsubscribe(token, function(err, resp) {
if (err) return cb(err);
return cb(null, resp);
2016-01-04 14:11:24 -03:00
});
}
return root;
});