- Home /
Other
Getting a Code to Run at a Specific Time of Day Even if App is Off.
Hello, I am using the code below in order to query the correct time on a server. Its a 24 hour timer that never stops. It is working perfectly and I am displaying the correct time on Unity editor with no errors. Except, the problem is I need this timer to run a specific code at exactly 24.00:00 every single day - I mean it works, except you have to be on the app running the scene in order for it to work, which makes sense. How do I get it to run the code at 24.00:00 even if the app is off?
For Example - Lets say you press a button at 21.00:00 and the button disappears. And the only way for the button to reappear is if the timer passes 24:00:00. With the code below you would have to be on the app on the scene at exactly 24.00:00 to turn the button back on. How do I get the button to reappear, when it passes 24.00:00 even if the app was off. So if they get on at 2.00:00 the button is back on and if they press the button again it disappears and they have to wait until the timer hits 24.00:00 for the button to turn back on again but they don't have to have the app open.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public Text myText;
string url = "http://www.website.com/gettime.php";
void Update () {
WWW www = new WWW(url);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
myText.text = www.data;
if (www.data == "24.00:00") {
Debug.Log("CODE I NEED TO RUN");
}
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}
Answer by fafase · Aug 26, 2015 at 07:14 AM
You cannot. Imagine if any developer could start his app without your agreement.
What you can do is store the time when the user quits and when he comes back compare the elapsed time. If more than x min/s/h/days then perform appropriate action, in your case show the button.
Or you can send notification which is the way most do. You know those annoying bling in the middle of the night to let you know you have refill your heart. But even for those a user may not allow them.
"What you can do is store the time when the user quits and when he comes back compare the elapsed time. If more than x $$anonymous$$/s/h/days then perform appropriate action, in your case show the button."
Yes, this is what I am looking for. Can you be a little more specific with how to implement this in my code?
void Awake(){
CheckTimer();
}
private void CheckTimer(){
// Check if player prefs contains QuitTime key
string time =PlayerPrefs.GetString("QuitTime");
// Convert to DateTime
// Compare and do action
}
void OnApplicationPause(bool pause) {
if(pause == false) CheckTimer();
}
void OnApplicationQuit(){
PlayerPrefs.SetString("QuitTime", DateTime.Now.ToString());
}
The reason I went with checking the time on a server is so my app could be secure. Player prefs are easy to hack don't you think?
Then OnQuit you send the data to the server. And on resume you compare.
Any advice on how I would be able to save it to a server?