Cordova check close app event and save date/time

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, 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.

This may not be ideal for you but I got around this by setting the current time to localStorage once a minute, then when my app is opened the first thing my ready() function does is check the difference between the localStorage value and Date.now(). This sounds like a performance killer but it works fine in my app – best to test in yours before relying on it. I do it thus:

function ticker() {
  var n=Date.now();
  if (n%60000<20) {
    localStorage.setItem("last_tick",n);
  }
  requestAnimationFrame(ticker);
}

Call ticker() to start it going. requestAnimationFrame() allows the browser to optimise. However, if your app goes into the background, the WebKit engine will slow down the frequency at which ticker() will be called and may stop it altogether so you may find you need to use setInterval(), which may bring its own performance problems (although I still think it would be fine). Your question was how did other people get around this and this is what works for me.