How to send user activity data to server in background when user closed/killed the application.

I have an application, in which I want to send data of user activity to our server when the user killed/closed the application. If it is unsuccessful in sending data to the server in the background then I will send all data when the user relaunches the app again. Is there a method to do this?

How to send user activity data to server in background when user closed/killed the application.
 
 
Q