Detect the opening of the app

Ok, so you mean when the app is in the background (e.g. after pressing the Home button) or in idle mode?

Then this extension might help: