2016-01-04 14:11:24 -03:00
|
|
|
'use strict';
|
|
|
|
|
angular.module('copayApp.services')
|
|
|
|
|
.factory('pushNotificationsService', function($http, $log, profileService, storageService, lodash) {
|
|
|
|
|
var root = {};
|
2015-12-23 12:29:46 -03:00
|
|
|
|
2016-01-04 14:11:24 -03:00
|
|
|
root.pushNotificationsInit = function() {
|
2015-12-23 12:29:46 -03:00
|
|
|
|
2016-01-04 14:11:24 -03:00
|
|
|
var push = PushNotification.init({
|
|
|
|
|
android: {
|
|
|
|
|
senderID: "959259672122"
|
|
|
|
|
},
|
|
|
|
|
ios: {
|
|
|
|
|
alert: "true",
|
|
|
|
|
badge: true,
|
|
|
|
|
sound: 'false'
|
|
|
|
|
},
|
|
|
|
|
windows: {}
|
|
|
|
|
});
|
2015-12-23 15:33:56 -03:00
|
|
|
|
2016-01-04 14:11:24 -03:00
|
|
|
push.on('registration', function(data) {
|
|
|
|
|
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('Notification event: ', data.message);
|
|
|
|
|
/* 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-04 16:35:34 -03:00
|
|
|
lodash.forEach(profileService.getWallets('testnet'), function(wallets) {
|
2016-01-04 14:11:24 -03:00
|
|
|
var opts = {};
|
|
|
|
|
opts.user = wallets.id;
|
|
|
|
|
opts.type = (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "ios" : (navigator.userAgent.match(/Android/i)) == "Android" ? "android" : null;
|
|
|
|
|
opts.token = token;
|
|
|
|
|
|
|
|
|
|
root.subscribe(opts).then(function(response) {
|
|
|
|
|
$log.debug('Suscribed: ' + response.status);
|
|
|
|
|
},
|
|
|
|
|
function(err) {
|
|
|
|
|
$log.warn('Error: ' + err.status);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.disableNotifications = function() {
|
|
|
|
|
storageService.getDeviceToken(function(err, token) {
|
2016-01-04 16:35:34 -03:00
|
|
|
root.unsubscribeAll(token).then(function(response) {
|
2016-01-04 14:11:24 -03:00
|
|
|
$log.debug('Unsubscribed: ' + response.status);
|
|
|
|
|
},
|
|
|
|
|
function(err) {
|
|
|
|
|
$log.warn('Error: ' + err.status);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.subscribe = function(opts) {
|
2016-01-04 16:35:34 -03:00
|
|
|
return $http.post('http://192.168.1.121:8000/subscribe', opts);
|
2016-01-04 14:11:24 -03:00
|
|
|
}
|
|
|
|
|
|
2016-01-04 16:35:34 -03:00
|
|
|
root.unsubscribe = function(user) {
|
|
|
|
|
return $http.post('http://192.168.1.121:8000/unsubscribe', {
|
|
|
|
|
user: user
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.unsubscribeAll = function(token) {
|
|
|
|
|
return $http.post('http://192.168.1.121:8000/unsubscribe', {
|
2016-01-04 14:11:24 -03:00
|
|
|
token: token
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
|
|
|
|
|
});
|