2016-01-04 14:11:24 -03:00
|
|
|
'use strict';
|
|
|
|
|
angular.module('copayApp.services')
|
2016-04-07 12:19:51 -03:00
|
|
|
.factory('pushNotificationsService', function($log, isMobile, storageService, configService, lodash, isCordova) {
|
2016-01-04 14:11:24 -03:00
|
|
|
var root = {};
|
2016-01-22 00:35:11 -03:00
|
|
|
var usePushNotifications = isCordova && !isMobile.Windows();
|
2015-12-23 12:29:46 -03:00
|
|
|
|
2016-01-26 14:44:17 -03:00
|
|
|
root.enableNotifications = function(walletsClients) {
|
2016-01-20 16:06:15 -03:00
|
|
|
if (!usePushNotifications) return;
|
|
|
|
|
|
2016-01-26 15:42:07 -03:00
|
|
|
var config = configService.getSync();
|
|
|
|
|
if (!config.pushNotifications.enabled) return;
|
|
|
|
|
|
2016-01-04 14:11:24 -03:00
|
|
|
storageService.getDeviceToken(function(err, token) {
|
2016-02-22 19:32:24 -03:00
|
|
|
if (err || !token) {
|
|
|
|
|
$log.warn('No token available for this device. Cannot set push notifications');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 14:44:17 -03:00
|
|
|
lodash.forEach(walletsClients, function(walletClient) {
|
2016-01-04 14:11:24 -03:00
|
|
|
var opts = {};
|
2016-01-13 10:12:10 -03:00
|
|
|
opts.type = isMobile.iOS() ? "ios" : isMobile.Android() ? "android" : null;
|
2016-01-04 14:11:24 -03:00
|
|
|
opts.token = token;
|
2016-01-26 14:44:17 -03:00
|
|
|
root.subscribe(opts, walletClient, function(err, response) {
|
2016-03-10 10:58:58 -03:00
|
|
|
if (err) $log.warn('Subscription error: ' + err.message + ': ' + JSON.stringify(opts));
|
2016-01-20 16:25:06 -03:00
|
|
|
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
|
2016-01-15 11:59:29 -03:00
|
|
|
});
|
2016-01-04 14:11:24 -03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 14:44:17 -03:00
|
|
|
root.disableNotifications = function(walletsClients) {
|
2016-01-20 16:06:15 -03:00
|
|
|
if (!usePushNotifications) return;
|
|
|
|
|
|
2016-01-26 14:44:17 -03:00
|
|
|
lodash.forEach(walletsClients, function(walletClient) {
|
|
|
|
|
root.unsubscribe(walletClient, function(err) {
|
2016-01-25 11:50:08 -03:00
|
|
|
if (err) $log.warn('Unsubscription error: ' + err.message);
|
2016-01-20 16:25:06 -03:00
|
|
|
else $log.debug('Unsubscribed from push notifications service');
|
2016-01-15 11:59:29 -03:00
|
|
|
});
|
2016-01-04 14:11:24 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 14:44:17 -03:00
|
|
|
root.subscribe = function(opts, walletClient, cb) {
|
2016-01-28 17:22:22 -03:00
|
|
|
if (!usePushNotifications) return cb();
|
2016-01-20 16:06:15 -03:00
|
|
|
|
2016-01-26 15:42:07 -03:00
|
|
|
var config = configService.getSync();
|
|
|
|
|
if (!config.pushNotifications.enabled) return;
|
|
|
|
|
|
2016-01-15 11:59:29 -03:00
|
|
|
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-26 14:44:17 -03:00
|
|
|
root.unsubscribe = function(walletClient, cb) {
|
2016-01-28 17:22:22 -03:00
|
|
|
if (!usePushNotifications) return cb();
|
2016-01-20 16:06:15 -03:00
|
|
|
|
2016-01-20 16:25:06 -03:00
|
|
|
walletClient.pushNotificationsUnsubscribe(function(err) {
|
2016-01-15 11:59:29 -03:00
|
|
|
if (err) return cb(err);
|
2016-01-20 16:25:06 -03:00
|
|
|
return cb(null);
|
2016-01-04 14:11:24 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
|
|
|
|
|
});
|