- Home /
Time.deltaTime not running in background (for local notification)
Hello, I'm trying to make an app that sends a local notification to the user after some time.
I tried with time.deltatime:
var startTime = 0.0;
var tempo1 = 50.0;
function Start()
{
var notif = new LocalNotification();
notif.alertBody = "Che ora è?";
NotificationServices.ScheduleLocalNotification(notif);
}
function Update()
{
startTime += Time.deltaTime;
if (startTime >= tempo1) {
Debug.Log ("About to play aiff.");
var notif = new LocalNotification();
notif.alertAction = "alert";
notif.alertBody = "Playing aiff sound";
notif.applicationIconBadgeNumber = -1;
notif.soundName = notif.defaultSoundName;
NotificationServices.ScheduleLocalNotification(notif);
}
}
but it seems that the time is not passing while the app is in background
do you have any suggestions? thanks in advance
Answer by Bunny83 · Mar 31, 2014 at 09:04 PM
If an app is not in the foreground, it doesn't run. Only services can run in the background. Your app is freezed while not active. If you want to schedule a notification you should set the **fireDate** to a point in the future. You can't run your own logic in the background. A Unity-app is a foreground application.
To run something in the background you would need to create a service which isn't possible with Unity. On Android you would need to create this in Java (not UnityScript or Javascript, the real Java) and for iOS you probably need to do this in Objective C in xcode.
However this has nothing to do with Unity and is device specific.
Thanks a lot Bunny83, It seems that with fireDate now it works
here is how I resolved it:
function OnApplicationPause(pauseStatus: boolean) {
while(true) {
Debug.Log ("About to play aiff.");
var notif = new LocalNotification();
notif.alertAction = "alert";
notif.alertBody = "Playing aiff sound";
notif.fireDate = System.DateTime.Now.AddSeconds(120);
notif.applicationIconBadgeNumber = -1;
notif.soundName = notif.defaultSoundName;
NotificationServices.ScheduleLocalNotification(notif);
break;
}
}
@$$anonymous$$albo$$anonymous$$: Yes that's what i ment, however you don't need the while loop at all. Just create your notification directly in OnApplicationPause.
Could you tick the answer as solution to close the question?
Answer by frarees · Mar 31, 2014 at 10:55 AM
You can use OnApplicationPause and OnApplicationFocus and deal there with time that has passed whilst on background
Thanks a lot for the answer frarees, I tried to use the function OnApplicationPause, but still time doesn't run while the app is in background:
var startTime = 0.0;
var tempo1 = 50.0;
function OnApplicationPause(pauseStatus: boolean)
{
while(true){
startTime += Time.deltaTime;
if (startTime >= tempo1) {
Debug.Log ("About to play aiff.");
var notif = new LocalNotification();
notif.alertAction = "alert";
notif.alertBody = "Playing aiff sound";
notif.applicationIconBadgeNumber = -1;
notif.soundName = notif.defaultSoundName;
NotificationServices.ScheduleLocalNotification(notif);
}
}
}
did I do something wrong? thanks in advance
Your answer
Follow this Question
Related Questions
Time based scoring 2 Answers
timer that can be reset 1 Answer
Remote Notifications - Triggered by user or not 2 Answers
Vibrate for X time on iOS? 1 Answer
How to work a real life timer? 2 Answers