0

I am developing an alarm App. At the time while alarm is playing sound, if the app is in open (foreground) then it executes the

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

Method & alarm sound plays successfully.

But at time while playing the alarm sound if my app is in inactive/ background state then it not called the above method.

So is there is any way so that i can play alarm sound in background also. For now when my app is in background only the notification is arrive & no sound/song is playing.

Below is my Code

-(void)application:(UIApplication *)application didReceiveLocalNotification(UILocalNotification *)notification
{
        musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

        [musicPlayer setQueueWithItemCollection:collection];
        [musicPlayer play];

        [self showReminder:@"Alarm ON!"];
}

- (void)showReminder:(NSString *)text
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm"
                                                        message:text delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"Snooze",@"Ok",nil];
    [alertView show];

}

I just wanted to know how can play a song when the app is inactive/background and also show alert to stop / snooze the alarm.

Please help me.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
ANP
  • 11
  • 5

2 Answers2

0

Below is my Code,you mabey have a try!

- (void)applicationDidEnterBackground:(UIApplication *)application{
 musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

    [musicPlayer setQueueWithItemCollection:collection];
    [musicPlayer play];

    [self showReminder:@"Alarm ON!"];
 }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Gavin Hero
  • 11
  • 2