when my users pause their app, I save the date in a window.localStorage object so that when the app gets resumed I can send a request to my server and see if there are new messages for them. this works but when they close the app and restart, I need to set the date again so I can’t check if there are messages between the time they closed it and restarted, since this is different from pause and resume. here’s a code sample to show a bit what I mean.
This is when the app get’s started:
$ionicPlatform.ready(function() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var days = now.getDate();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var now = year + '-' + month + '-' + days + ' ' + hours + ':' + minutes + ':' + seconds;
window.localStorage.setItem('lastActive',now);
});
And these are the onpause/resume functions
document.addEventListener("resume", onResume, false);
document.addEventListener("pause", onPause, false);
function onResume() {
if(window.localStorage.getItem('lastActive')){
var lastActive = window.localStorage.getItem('lastActive');
console.log('last seen on: '+lastActive);
$http({method: 'get', url: '***/api.php/inbox/'+window.localStorage.getItem('auth')+'/'+lastActive}).
success(function(data, status, headers, config) {
if(data.new < 1){
$scope.newcount = data.new
}else{
$scope.newcount = data.new
}
}).error(function(data, status, headers, config) {
$cordovaToast.showShortTop("are you connected to the internet?").then(function(success) {
}, function (error) {
alert("are you connected to the internet?");
});
});
}else{
console.log('lastst online: unkown');
}
}
function onPause() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var days = now.getDate();
var hours = now.getHours() + 1;
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var now = year + '-' + month + '-' + days + ' ' + hours + ':' + minutes + ':' + seconds;
window.localStorage.setItem('lastActive',now);
window.localStorage.setItem('lastActivejs',new Date());
}
now when the app get’s closed and restarted, the ionicplatform.ready function get’s fired again to set the last active date since I can’t set one on close. i’m sure someone else had to deal with this and would like to know how you tackled it.