views:

121

answers:

1

hi all...
i want to let my app play sound every 20 min even when the app is on background or the iphone is in sleep mode ... i used: [NSTimer scheduledTimerWithTimeInterval:delay*60.0 target:self selector:@selector(goMethod) userInfo:nil repeats:YES]; that working maybe for 1 hour and then stopped can any one help me to solve this problem ...

+1  A: 

Use UILocalNotifications.

3 notifications (ie. 13:00, 13:20 and 13:40) with repeatinterval:NSHourCalendarUnit..

Edit: Something like this:

UILocalNotification *localNotif = [[UILocalNotification alloc] init];

localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.repeatInterval = NSHourCalendarUnit; //Repeat every hour

localNotif.fireDate = [NSDate date]; //Now
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:1200]; //Now + 20 min
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:2400]; //Now + 40 min
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

[localNotif release];
Larsaronen
txt for reply but what i wont is not a notification (the notification is a alert with buttons)... i have to play a short sound (1.0 second or less) and count how many this sound has played without let the user click on the notification buttons so if you can help me with any idea i will be grateful
jissa
I belive the notification can be: alert with buttons OR sound OR badge OR any combination of these.. I suggest using UILocalNotifications with only sound! It's also the only way i can think of to do this..
Larsaronen
Check out my edited post for sample code..
Larsaronen
txt again for replay i try it and it's working but in my replay i say that i need to count how many this sound is playing (with this count i will change the sound and the timerInterval example :if the count is 4 i will play do.aiff every 20 min if the count is 60 i will play re.aiff every 35 min if the count is 120 i will play mi.aiff for 5 min etc...) so my question now is how can i catch how many the sound played... txt again for your help
jissa
Well basically you cant.. You would have to launch a message and the user have to enter youre app. The only thing you can do is figure out when to play all the sounds and add notifications.. You probably dont need to count do you?You know when the sound plays. So figuring out the count and when to schedule what sound is simple math..
Larsaronen
hi again i want to change something in my app... i want the notification to repeat every 2 hour so can you please modify this code : localNotif.repeatInterval = NSHourCalendarUnit; //Repeat every hour and i want to stop the notification from 11PM to 7AM can any one help me please and i will be grateful
jissa
You could schedule a notification for every 2 hour. i.e: add a notification with fireDate 7 AM one with 9AM and so on up until 11 PM. And set localNotif.repeatInterval = NSDayCalendarUnit; for each notification..
Larsaronen