5

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.

Jacky Wang
  • 618
  • 7
  • 27
  • It is a restriction from Apple for UILocalNotification that alarm will not make a sound if your device is in silent mode. You are saying you have seen app which do so, can you name those apps so that we can try. – Vizllx May 15 '15 at 09:41
  • @Vizllx WakeMeHere Lite can wake you up with a notification and sound even when you are on silent mode. But I don't think the sound came from the notification itself. Probably played a sound using a different way at the same time as the notification. – Jacky Wang May 15 '15 at 09:52
  • You might try having an AVAudioPlayer already around and prepared to play the sound, so all you need to call is "play", and of course register your app as a music player. So at least in principle it can play when set to "silent" and in the background. – gnasher729 May 15 '15 at 09:52
  • @gnasher729, if the player is silent for over a period of time, the audio session will be stopped and app will be suspended. Some people said they played a silent sound in the background to keep the app alive and change the sound when they need to. However, that is a terrible work around as the user can take the audio session away by simply playing a song from iTunes. – Jacky Wang May 15 '15 at 09:55

1 Answers1

2

Since my reputation is not enough to comment everywhere, I would write it here as an answer. Sorry about this.

I'm also suffering with the same problem. Though I cannot give you a solution, there might be some hints to you. To me, I think it might be impossible to achieve.

I have double checked two alarm application I use: Sleep Cycle and UNIQLO Wake Up. And I found out that these two applications asked you to keep it always running on the foreground to make sure that the alarm clock will work. Especially UNIQLO Wake Up, it will show you an alert when you save the alarm clock, telling you that press Home button may cause the alarm clock not working.

There is a post asking about similar question, and the author provide some names of the applications: How do I start playing audio when in silent mode locked in iOS 6. I checked the application called WaveAlarm. Similarly, if the cellphone is in mute mode and you put it into background, only UILocalNotification will work. This means, no sound will be played in this situation. If your cellphone is still in the mute mode, but you keep the application in the foreground, it will play a sound. The volume of the sound depends on the volume you set for video playing.

So to me, I think it might impossible to play alarm sound when you put the application into background and set the cellphone into mute mode. I guess the most possible way to achieve this is to ask users to keep it in foreground, and play a media with the alarm sound.

I'll try to achieve this function in these two days. If I have some other understandings, I'll edit this post here.

Community
  • 1
  • 1
Yuting
  • 131
  • 1
  • 12
  • Thanks for sharing your findings and perspective. It's really helpful knowing there are other people trying to solve this problem. Hopefully we can work together and find a work around to this problem. Cheers – Jacky Wang May 18 '15 at 10:49
  • I had tried, and using `AVAudioPlayer` can play sound when the cellphone is in the mute mode. But again, it cannot be used when in the background. I think the best way to do this might be combine `UILocalNotification`'s sound, and `AVAudioPlayer`. And show an alert to the user, telling them not to set the cellphone into mute mode, or asked them not to press HOME button after setting the alarm clock. But I'm not sure other application use this method or not. – Yuting May 19 '15 at 01:39
  • ya. I would hate to have to tell the user to not press HOME or don't set mute though :( I don't think that makes for a good user experience. I'm hoping we can somehow find a way to hack this. I will be trying other work arounds as well. – Jacky Wang May 19 '15 at 04:11
  • Please tell me if you have some further findings. I'd also like to know that :) – Yuting May 20 '15 at 01:40
  • I used my "WakeMeHere Lite" App again and it actually works when the phone is mute and app is in the background. So there has to be a way to achieve this... – Jacky Wang May 20 '15 at 15:23
  • My development of the application has paused due to some reasons. But I'll keep on finding ways to solve this in my free time. I hope we can find a solution as soon as possible. – Yuting May 21 '15 at 08:28