Run a function before closing the app. Is it possible?

Hy!

Is there any possibility to execute some code before closing the application? (swiping the app in android or in iOs)
I want to send a last message to server before closing the application, like the user is not online anymore.

Thank you for any kind of help.

That’s a dangerous thing to rely on. What if the device is offline at that point? I think you’re better off with some sort of periodic ping and a timeout.

I agree @rapropos 's comment.
I use Observable timer. And Server catch last ping.

@Injectable()
export class AuthInterval
{
    constructor(
        public http:Http
    ){}

    public get_status()
    {
        return Observable
            .timer(0, 1000)
            .flatMap(() => this.http.get(URL + "v1/account/status"))
            .map(res => {
                if(res.status == 204) {
                    throw new Error('This request get no contents');
                } else {
                    return res.json();
                }
            }).catch(error =>{
                return Observable.throw(error);
            });
    }
}

Yepp. This is true. I will do in this way. First I tought that making ping is not the best option but now I have to change my mind.
Thank you for answers!

1 Like