2016-01-04 14:11:24 -03:00
|
|
|
'use strict';
|
|
|
|
|
angular.module('copayApp.services')
|
2016-01-26 14:44:17 -03:00
|
|
|
.factory('pushNotificationsService', function($http, $log, isMobile, storageService, configService, lodash, isCordova) {
|
2016-01-04 14:11:24 -03:00
|
|
|
var root = {};
|
2016-01-13 10:12:10 -03:00
|
|
|
var defaults = configService.getDefaults();
|
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.pushNotificationsInit = function(walletClients) {
|
2016-01-20 16:06:15 -03:00
|
|
|
if (!usePushNotifications) return;
|
|
|
|
|
|
2016-01-13 10:12:10 -03:00
|
|
|
var push = PushNotification.init(defaults.pushNotifications.config);
|
2015-12-23 15:33:56 -03:00
|
|
|
|
2016-01-04 14:11:24 -03:00
|
|
|
push.on('registration', function(data) {
|
2016-01-13 10:12:10 -03:00
|
|
|
$log.debug('Starting push notification registration');
|
2016-01-04 14:11:24 -03:00
|
|
|
storageService.setDeviceToken(data.registrationId, function() {
|
2016-01-26 14:44:17 -03:00
|
|
|
root.enableNotifications(walletsClients);
|
2016-01-04 14:11:24 -03:00
|
|
|
});
|
|
|
|
|
});
|
2015-12-23 12:29:46 -03:00
|
|
|
|
2016-01-04 14:11:24 -03:00
|
|
|
push.on('notification', function(data) {
|
2016-01-13 10:12:10 -03:00
|
|
|
$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);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
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-04 14:11:24 -03:00
|
|
|
storageService.getDeviceToken(function(err, token) {
|
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-01-20 16:25:06 -03:00
|
|
|
if (err) $log.warn('Subscription error: ' + err.code);
|
|
|
|
|
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-20 16:25:06 -03:00
|
|
|
if (err) $log.warn('Subscription error: ' + err.code);
|
|
|
|
|
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) {
|
|
|
|
|
if (!usePushNotifications) return;
|
2016-01-20 16:06:15 -03:00
|
|
|
|
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) {
|
|
|
|
|
if (!usePushNotifications) return;
|
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;
|
|
|
|
|
|
|
|
|
|
});
|