Wallet/src/js/services/pushNotificationsService.js

92 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-01-04 14:11:24 -03:00
'use strict';
angular.module('copayApp.services')
2016-01-26 15:42:07 -03:00
.factory('pushNotificationsService', function($http, $rootScope, $log, isMobile, storageService, configService, lodash, isCordova) {
2016-01-04 14:11:24 -03:00
var root = {};
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
root.pushNotificationsInit = function() {
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;
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() {
2016-01-26 15:42:07 -03:00
$rootScope.$emit('Local/pushNotificationsReady');
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) {
$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(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) {
lodash.forEach(walletsClients, function(walletClient) {
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;
root.subscribe(opts, walletClient, function(err, response) {
2016-01-22 18:16:50 -03:00
if (err) $log.warn('Subscription error: ' + err.message);
2016-01-20 16:25:06 -03:00
else $log.debug('Subscribed to push notifications service: ' + JSON.stringify(response));
});
2016-01-04 14:11:24 -03:00
});
});
}
root.disableNotifications = function(walletsClients) {
2016-01-20 16:06:15 -03:00
if (!usePushNotifications) return;
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-04 14:11:24 -03:00
});
}
root.subscribe = function(opts, walletClient, cb) {
if (!usePushNotifications) return;
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;
walletClient.pushNotificationsSubscribe(opts, function(err, resp) {
if (err) return cb(err);
return cb(null, resp);
2016-01-04 16:35:34 -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) {
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;
});