Keane
Keane

Reputation: 45

UILocalNotification how to call certain method if application is in background

I am creating player application, which is playing audio streams through internet. I want to add alarm functionality in my app - in particular time my player begins to play audio stream, I am trying to use UILocalNotification mechanism. But I've difficulties with it when my application in background mode, I can't call 'play' method, when notification is receiced (can't without user interaction). May be it is impossible?

But I bought this application: http://itunes.apple.com/us/app/radio-alarm-clock-mp3-radio/id380271167?mt=8 And it seems like radio can start playing when local notification is received. Alarm can start playing radio when my app is in background mode. Earlier I was trying to use NSTimer for this, but when my app goes to background, timer stops. If I use beginBackgroundTaskWithExpirationHandler: it works only 10 minutes. My app has special flag in plist, what is is audio application, and can playing music in background. In this case timers are working. But if I stop playing and go to background, timer is not working. When I use \Radio Alarm Clock' application, I hear 'white noise' from dinamic, when music in not playing. May be it is the secret of this application? Can you help me with my problem? Thanks.

Upvotes: 2

Views: 1748

Answers (2)

Gianluca
Gianluca

Reputation: 133

maybe it's too late.
I had a look to the app you've mentioned at http://itunes.apple.com/us/app/radio-alarm-clock-mp3-radio/id380271167?mt=8 and yes, I think you are absolutely right, the only way to achieve that the application remains active while in background is to play a fake sound while it is in the background, which should be prohibited by Apple.

I've also seen that they don't use the remote iPod control, and this was strange at a first look.
At the end my opinion is that they do the following:

  • Avoid the call to beginReceivingRemoteControlEvents which allows to activate the iPod controls while in background (in fact they don't have it)
  • In this way, the status bar doesn't show the play icon while the app plays audio
  • When the app goes in background, it probably plays a no sound periodically (once every 10 secs for example), in this way the app remains active
  • I saw that they also avoided to manage interruptions, for example in case another app is in foreground and plays music. Again Apple should have rejected the app for that reason, cos it is against the rules to follow while in background, but maybe they didn't see it during the acceptance tests.

So my interpretation is that they have intentionally missed to activate the iPod controls, just to avoid to show the play icon in the status bar while in background. In this way, the users are unaware that the app is active and is doing something strange after they close it.

In addition you can see that the app doesn't interrupt when another app plays in foreground a sound or audio, because otherwise they risk that the app doesn't restart on time when the alarm shpould fire.
That's just my idea of how they do that, and I think this is the only way for an audio app on iOS to remain active while it is in background and is supposed to be halted (well, in case Apple doesn't see the trick).

Upvotes: 2

Popeye
Popeye

Reputation: 12113

Have you tried adding this to appdelegate.m

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    // Call your method in here.
}

if you have can you add code for us to see what your doing.

Upvotes: 0

Related Questions