angular.module('app.app.services', [
'ionic',
'ngCordova.plugins.nativeStorage',
'ngCordova.plugins.appVersion',
//'ngCordova.plugins.push_v5'
])
.factory('_', function () {
window._.paginate = function(arr, size) {
var pages = [];
while (arr.length) {
pages.push(arr.splice(0, size));
}
return pages;
};
return window._;
})
.service('utilities', function () {
var that = this;
this.geodistance = function (lat1, lon1, lat2, lon2) {
var R = 6378.137; // Radius of earth in KM
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d; // kms
};
})
.service('notifications', function (
$rootScope,
$cordovaAppVersion,
//$cordovaPushV5,
$http,
$location,
$ionicActionSheet,
$timeout,
//localStorageService,
$cordovaNativeStorage,
API_END_POINT
) {
var that = this;
this.registered = false;
this.register = function (callback) {
var update_data_app = false;
if(ionic.Platform.isWebView()) {
$cordovaAppVersion.getVersionNumber().then(function (version) {
if(!$rootScope.user.app_version || $rootScope.user.app_version != version)
update_data_app = true;
});
}
if (ionic.Platform.isWebView() && (!that.registered || update_data_app)) {
window.FirebasePlugin.getToken(function(token) {
//$cordovaPushV5.register().then(function (token) {
$cordovaAppVersion.getVersionNumber().then(function (version) {
var data = {
app_token: token
, app_notifications: 1
, app: version
, guid: window.device ? window.device.uuid : null
, platform: window.device ? window.device.platform : null
, version: window.device ? window.device.version : null
, model: window.device ? window.device.model : null
, name: window.device ? window.device.name : null
, uuid: window.device ? window.device.uuid : null
};
//localStorageService.set('notifications', data);
console.log("S71_set");
$cordovaNativeStorage.setItem('notifications', data).then(function (value) {
console.log("S71_set OK");
}, function (error) {
console.log("S71_set ERROR");
console.log(JSON.stringify(error));
});
$http({
url: API_END_POINT + '/token',
method: "POST",
data: data,
headers: {
'Authorization': 'Basic ' + btoa($rootScope.user.id + ':' + $rootScope.user.card),
'Content-Type': 'application/json'
},
timeout: 10000
}).success(function(data, status, headers, config) {
that.registered = true;
if (callback) {
callback(token);
}
});
});
}, function (err) {
console.error('$cordovaPushV5:REGISTER_ERROR', err);
});
}
};
this.initialized = false;
this.initialize = function () {
if (ionic.Platform.isWebView() && !that.initialized) {
if (ionic.Platform.isWebView() && ionic.Platform.isIOS()) {
window.FirebasePlugin.grantPermission();
}
that.initialized = true;
window.FirebasePlugin.onNotificationOpen(function(notification) {
console.log("notifica: "+JSON.stringify(notification));
if (notification.additionalData && notification.additionalData.popup_title) {
var buttons = [];
if (notification.additionalData.popup_button) {
buttons = [{text: (notification.additionalData.popup_button_icon ? '<i class="' + notification.additionalData.popup_button_icon + '"></i> ' : '') +
notification.additionalData.popup_button}];
}
hideSheet = $ionicActionSheet.show({
titleText: notification.additionalData.popup_title,
cancelText: '<i class="fa fa-times red"></i> Annulla',
cancel: function() {
hideSheet();
},
buttons: buttons,
buttonClicked: function(index) {
if (index === 0 && notification.additionalData.location) {
$timeout(function () {
if (notification.additionalData.location.indexOf("cadoro.it") === 0) {
window.open(notification.additionalData.location, '_blank');
} else if (notification.additionalData.location.indexOf("http") === 0) {
window.open(notification.additionalData.location, '_system', 'location=yes');
} else {
$location.path(notification.additionalData.location);
}
}, 10);
}
}
});
} else if (notification.additionalData && notification.additionalData.location) {
$location.path(notification.additionalData.location);
}
}, function(error) {
console.error(error);
});
/*
var push = $cordovaPushV5.initialize({
android: {
senderID: '419075471866'
},
ios: {
alert: "true",
badge: true,
sound: 'true'
},
windows: {}
}).then(function () {
$cordovaPushV5.onError();
$cordovaPushV5.onNotification();
$rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification) {
if (notification.count) {
$cordovaPushV5.setBadgeNumber(notification.count);
}
if (notification.additionalData && notification.additionalData.popup_title) {
var buttons = [];
if (notification.additionalData.popup_button) {
buttons = [{text: (notification.additionalData.popup_button_icon ? '<i class="' + notification.additionalData.popup_button_icon + '"></i> ' : '') +
notification.additionalData.popup_button}];
}
hideSheet = $ionicActionSheet.show({
titleText: notification.additionalData.popup_title,
cancelText: '<i class="fa fa-times red"></i> Annulla',
cancel: function() {
hideSheet();
},
buttons: buttons,
buttonClicked: function(index) {
if (index === 0 && notification.additionalData.location) {
$timeout(function () {
if (notification.additionalData.location.indexOf("cadoro.it") === 0) {
window.open(notification.additionalData.location, '_blank');
} else if (notification.additionalData.location.indexOf("http") === 0) {
window.open(notification.additionalData.location, '_system', 'location=yes');
} else {
$location.path(notification.additionalData.location);
}
}, 10);
}
}
});
} else if (notification.additionalData && notification.additionalData.location) {
$location.path(notification.additionalData.location);
}
});
});
*/
}
};
});