I am trying to build a custom alarm application. But the challenge I am facing is getting the sound played while app is in the background.
First method, UILocalNotification,
According to the Apple programming guidelines, the right way to make an alarm is
- (void)scheduleAlarmForDate:(NSDate*)theDate
{
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];
// Create a new notification.
UILocalNotification* alarm = [[UILocalNotification alloc] init];
if (alarm)
{
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = @"alarmsound.caf";
alarm.alertBody = @"Time to wake up!";
[app scheduleLocalNotification:alarm];
}
}
Problem is,
if the user has the phone on silent, the notification sound will only be a small vibrate that doesn't suffice for a alarm.
Idealy, I want a alarm that will go off no matter the phone is on silent or normal to wake up the user.
Second way I can think of, AVAudioSession
- But when the region is near or alarm trigger comes in, audioSession cannot be initiated in the background so any audioPlayer.play() methods won't do anything.
Besides the above 2 methods that both don't work, I can't seem to find any other ways to tackle the problem at hand.
I am trying to make an App which is a location based alarm.
I can set a region and custom sound for 30 seconds on my UILocalNotification but the sound won't play if phone is silent.
I can have a locationManager monitoring my region but I can't play a sound after I get notified from the background.
I've downloaded many Apps that are capable of doing this so I know there must be way of achieving it. However, after multiple tries in various angles, I can't seem to find any good solutions. If anyone had experience with making a custom alarm, please share some tips and pointers if you can.
Thank you.